簡體   English   中英

如何在Java腳本上實現淡入動畫,例如CSS上的Easy-in-out?

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

我在網站上使用Java腳本,將鼠標懸停在縮略圖上時可顯示圖像預覽。 該腳本效果很好,但我想在CSS中添加一個功能,如easy-in-out。 我對Java不太了解,但是我嘗試了幾次不同的嘗試,但是沒有運氣。 我發現了一些不錯的教程,如http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animationhttp://www.scriptiny.com/2011/01/javascript-fade-in- out / ,但是由於沒有Java的編程知識,所以我不知道如何在腳本中實現這些。 我很高興有人可以幫助我解決這個問題。

這是在html上調用函數的方式:

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

這是腳本

    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;

jQuery有一個非常好的和簡單的淡入和淡出方式,您可以檢查他們的站點來查看。

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

如果使用jQuery UI,則可以使用效果來完成所需的操作。 看到這個鏈接:

http://jqueryui.com/effect/

例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM