riseagainst
riseagainst

Reputation: 430

How to implement fade-in-out animation on java-script like ease-in-out on css?

I have a java script that i use on my website to show a preview of an image when hovering over a thumbnail. The script works great, but i's like to add a function like ease-in-out in css. I don't know much about java, but i tried a couple of different things with no luck. I found some nice tutorials, such as http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation , http://www.scriptiny.com/2011/01/javascript-fade-in-out/ , but with no programming knowledge in java, i don't know how to implement those into this script. II'd appreciated if somebody could help me out on this one.

This is how the function gets called on html:

<a href="http://www.a.com/a.html"><img onmouseover="showImage(this.src,this,'a')" src="http://a.net/a.png" /></a>

This is the script

    var floatWidth = 150;  // set the width of the floating image
    var floatHeight = 100;  // set its height


    var midWindow = 0;
    var nContainer = "";
    var IE = false;

    if (navigator.appName == 'Microsoft Internet Explorer'){IE = true}

    function stayHome(m){        

            if (IE)
                    {
                    var currX = event.clientX;
                    var currY = event.clientY;
                    }
            else        {
                    var currX = m.pageX;
                    var currY = m.pageY;
                    }
            if (document.documentElement && document.documentElement.scrollLeft || document.documentElement && document.documentElement.scrollTop)
                    {
                    var iL = document.documentElement.scrollLeft;        
                    var iV = document.documentElement.scrollTop;
                    }
            else        {
                    var iL = document.body.scrollLeft;        
                    var iV = document.body.scrollTop;        
                    }
            if (currX > midWindow+60)
                    {
                    var msgWidth = nContainer.clientWidth;
                    if (IE){nContainer.style.left = (currX-msgWidth-10+iL)+'px'}
                    else {nContainer.style.left = (currX-msgWidth-10)+'px'}
                    }
            else        {
                    if (IE){nContainer.style.left = (currX+10+iL)+'px'}
                    else {nContainer.style.left = (currX+10)+'px'}
                    }


            if (IE){nContainer.style.top = (currY+iV-(floatHeight/2)+70)+'px'}
            else {nContainer.style.top = (currY-(floatHeight/2)+70)+'px'}


    }        

    function hideImage(){

            while (nContainer.lastChild)
                    {nContainer.removeChild(nContainer.lastChild)}
            document.getElementById('isFloat').style.display = 'none';
    }

    function showImage(isImg,currItem,currCaption){

            document.getElementById('isFloat').style.display = 'inline';
            nIMG  = document.createElement('img');
            nContainer.appendChild(nIMG);
            nIMG.setAttribute('src',isImg);
            nIMG.setAttribute('width',floatWidth);
            nIMG.setAttribute('height',floatHeight);
            nCaption = document.createElement('div');
            nCaption.style.textAlign = "center";
            nCaption.style.backgroundColor = '#EAE3C6';
            nCaption.style.padding = '5px';
            nCaption.style.color = '#000000';
            nCaption.style.fontFamily = 'Sans-serif';
            nCaption.style.fontSize = '10pt';
            nCaption.style.borderTop = "1px solid black";
            nContainer.appendChild(nCaption);
            nCaption.innerHTML = currCaption;
            currItem.onmouseout=hideImage;
    }



    function getMidWindow(){

            if (document.documentElement && document.documentElement.scrollLeft || document.documentElement && document.documentElement.scrollTop)
                    {
                    midWindow = document.documentElement.clientWidth/2;
                    }
            else        {
                    midWindow = document.body.clientWidth/2;
                    }
    }

    function initFloatImg(){

            var nBody = document.getElementsByTagName('body')[0];
            var nDiv = document.createElement('div');
            nDiv.id = "isFloat";
            nDiv.style.position = "absolute";
            nDiv.style.top = "0px";
            nDiv.style.left = "0px";
            nDiv.style.border = "1px solid black";
            nDiv.style.padding = "5px";
            nDiv.style.backgroundColor = "#ffffff"
            nBody.appendChild(nDiv);
            nContainer = document.getElementById('isFloat');
            document.onmousemove = stayHome;
            hideImage();
            if (!IE){document.captureEvents(Event.mousemove)}
            getMidWindow();
    }

    onload=initFloatImg;
    onresize=getMidWindow;

Upvotes: 0

Views: 720

Answers (2)

h3nr7
h3nr7

Reputation: 86

Jquery has a very good and simple way of doing fadeIn and fadeOut, you can check their site to see.

http://api.jquery.com/fadeIn/

Upvotes: 1

Piercy
Piercy

Reputation: 809

if you use jQuery UI you can use the effects to do what you require. See this link:

http://jqueryui.com/effect/

example:

jQuery('#someDiv').show('blind');

Upvotes: 0

Related Questions