js动画效果练习(1)

家住魔仙堡 提交于 2019-12-05 03:52:10

 

<div id="div1">
    <span id="share">分享</span>
</div>

 

 

1.速度动画

<style>
    #div1{
        width:200px;
        height:200px;
        background-color:red;
        position: relative;
        left: -200px;
        top:0;
    }
    #share{
        width:20px;;
        height:50px;
        background-color: blue;
        color: white;
        position: absolute;
        left: 200px;
        top:75px;
    }
</style>
<script>
    window.onload=function(){
        var oDiv=document.getElementById("div1");
        oDiv.onmouseover=function(){
            startMove(0);
        };
        oDiv.onmouseout=function(){
            startMove(-200);
        }
    };
    var timer=null;
    function startMove(iTarget){
        clearInterval(timer);
        var oDiv=document.getElementById("div1");
        timer=setInterval(function(){
            var speed=0;
            if(oDiv.offsetLeft<iTarget){
                speed=2;
            }else{
                speed=-2;
            }
            if(oDiv.offsetLeft==iTarget){
                clearInterval(timer);
            }else{
                oDiv.style.left=oDiv.offsetLeft + speed + 'px';
            }
        },30);
    }
</script>

 

效果如下:

初始:,鼠标移入后开始向右滑动:,鼠标移出后开始向左滑动隐藏:

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!