div簡單水平移動效果


  將img放置在div中,通過移動div達到移動img的效果,下面是水平移動。

  html文件:

<div style="float:left; width:1000px">
    <div style="position:absolute; left:10px; float:left; width:250px" id="fIdiv">
        <img id="firstImg" src="images/sjz1.JPG" />
    </div>
    <div style="position:absolute; left:220px; float:left; width:250px" id="sIdiv">
        <img id="secondImg" src="images/sjz2.jpg" />
    </div>
</div>

  js文件:

var i=10;
function transferImages(){
    var first = document.getElementById("fIdiv");
    var second = document.getElementById("sIdiv");
    first.style.position='absolute';
    second.style.position='absolute';
    
    var timer = setInterval(
        function(){
            if(first.style.left=='440px')
                clearInterval(timer);
        i = i+10;
        first.style.left = i+'px';
        second.style.left = (i+209)+'px';
        },60);    
}

  js文件中timer是setInterval()返回值,用於作為clearInterval()的參數,來結束循環調用,setInterval()中的function()是循環的語句。

  div的left值在正常賦值是:

<div style="position:absolute; left:10px"></div>

  因此在重新賦值的時候加上'px'單位。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM