js运动-匀速运动、透明度、多物体的运动、多属性运动

1、匀速运动

例子1:分享到

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #div1{
                width: 100px;
                height: 200px;
                background: olive;
                position: absolute;
                left: -100px;
                top: 100px;
            }
            #div2{
                width: 30px;
                height: 60px;
                background: olivedrab;
                position: absolute;
                top: 70px;
                right: -30px;
                text-align: center;
                color: #fff;
                display: table;
            }
            #div2 p{
                display: table-cell;
                vertical-align: middle;
            }
        </style>
    </head>
    <body>
        <div id="div1">
            <div id="div2">
                <p>分享到</p>
            </div>
        </div>
        <script type="text/javascript">
            var oDiv1=document.getElementById("div1");
            var oDiv2=document.getElementById("div2");
            var iTimer=null;

            oDiv1.onmouseover=function(){
                startMove(this,0,10)
            }

            oDiv1.onmouseout=function(){
                startMove(this,-100,-10)
            }

            function startMove(obj,target,ispeed){
                clearInterval(iTimer);//先清定时器,防止连续多次点击,速度叠加
                iTimer=setInterval(function(){
                    if (obj.offsetLeft==target) {//运动停止的条件
                        clearInterval(iTimer);
                    }else{
                        obj.style.left=obj.offsetLeft+ispeed+'px';//开始运动
                    }
                },30)
            }

        </script>
    </body>
</html>

2、透明度

例子2:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #img1{
                opacity: 0.3;
                filter: alpha(opacity=30);
            }
        </style>
    </head>
    <body>
        <img src="img/1.jpg" id="img1" />
        <script type="text/javascript">
            var oImg=document.getElementById("img1");
            var iTimer=null;

            oImg.onmouseover=function(){
                startMove2(this,100,10);
            }

            oImg.onmouseout=function(){
                startMove2(this,30,-10);
            }

            function startMove2(obj,target,ispeed){
                clearInterval(iTimer);
                var iCur=0;//定义一个代表透明度的变量

                iTimer=setInterval(function(){
                    iCur=Math.round(css(obj,'opacity')*100);  //非标准ie同样能取到opacity的值,同时解决小数精度问题

                    if (iCur==target) {  
                        clearInterval(iTimer);
                    }else{
                        obj.style.opacity=( iCur+ispeed )/100;  //分别给opacity和filter赋值
                        obj.style.filter='alpha(opacity='+(iCur+ispeed)+')';
                    }
                },30)
            }

            function css(obj,attr){
                if (obj.currentStyle) {
                    return obj.currentStyle;
                }else{
                    return getComputedStyle(obj,false)[attr];
                }
            }

        </script>
    </body>
</html>

3、多物体的运动

例子3:前面例子1和例子2的结合

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #div1{
                width: 100px;
                height: 200px;
                background: olive;
                position: absolute;
                left: -100px;
                top: 100px;
            }
            #div2{
                width: 30px;
                height: 60px;
                background: olivedrab;
                position: absolute;
                top: 70px;
                right: -30px;
                text-align: center;
                color: #fff;
                display: table;
            }
            #div2 p{
                display: table-cell;
                vertical-align: middle;
            }
            #img1{
                margin-left: 200px;
                opacity: 0.3;
                filter: alpha(opacity=30);
            }
        </style>
    </head>
    <body>
        <div id="div1">
            <div id="div2">
                <p>分享到</p>
            </div>
        </div>
        <img src="img/1.jpg" id="img1" />

        <script type="text/javascript">

            var oDiv1=document.getElementById("div1");
            var oDiv2=document.getElementById("div2");
            var oImg=document.getElementById("img1");
            //var iTimer=null;

            oDiv1.onmouseover=function(){
                startMove(this,'left',0,10)
            }

            oDiv1.onmouseout=function(){
                startMove(this,'left',-100,-10)
            }

            oImg.onmouseover=function(){
                startMove(this,'opacity',100,10);
            }

            oImg.onmouseout=function(){
                startMove(this,'opacity',30,-10);
            }

            function startMove(obj,attr,target,ispeed){
                clearInterval(obj.iTimer);  //用obj.iTimer代替iTimer防止多个运动物体共用一个定时器而相互影响
                var iCur=0;

                obj.iTimer=setInterval(function(){
                    if (attr=='opacity') {
                        iCur=Math.round(css(obj,'opacity')*100);
                    }else{
                        iCur=parseInt( css(obj,attr) ); //css函数取出来带单位,所以要去掉单位
                    }

                    if (iCur==target) {  
                        clearInterval(obj.iTimer);
                    }else{
                        if (attr=='opacity') {
                            obj.style.opacity=( iCur+ispeed )/100;
                            obj.style.filter='alpha(opacity='+(iCur+ispeed)+')';
                        }else{
                            obj.style[attr]=iCur+ispeed+'px';
                        }

                    }
                },30)
            }

            function css(obj,attr){
                if (obj.currentStyle) {
                    return obj.currentStyle;
                }else{
                    return getComputedStyle(obj,false)[attr];
                }
            }
        </script>
    </body>
</html>

4、多属性同时运动

例子4:(注意物体的多个属性并非同时到达目标点)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #div1{
                width: 100px;
                height: 100px;
                background: olive;
                position: absolute;
                left: 100px;
                top: 100px;
            }
        </style>
    </head>
    <body>
        <div id="div1"></div>

        <script type="text/javascript">
            var oDiv1=document.getElementById("div1");

            oDiv1.onclick=function(){
                startMove(this,{
                    width:200,
                    height:300
                },10);
            }

            function startMove(obj,json,ispeed){
                clearInterval(obj.iTimer);
                var iCur=0;

                obj.iTimer=setInterval(function(){
                    var iBtn=true;

                    for (var attr in json) {

                        if (attr=='opacity') {
                            iCur=Math.round(css(obj,'opacity')*100);
                        }else{
                            iCur=parseInt( css(obj,attr) );
                        }

                        var target=json[attr];

                        if (iCur!=target) {  
                            iBtn=false;
                            if (attr=='opacity') {
                                obj.style.opacity=( iCur+ispeed )/100;
                                obj.style.filter='alpha(opacity='+(iCur+ispeed)+')';
                            }else{
                                obj.style[attr]=iCur+ispeed+'px';
                            }
                        }   

                    }

                    if (iBtn) {
                        clearInterval(obj.iTimer);
                    }

                },30)
            }

            function css(obj,attr){
                if (obj.currentStyle) {
                    return obj.currentStyle;
                }else{
                    return getComputedStyle(obj,false)[attr];
                }
            }
        </script>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值