• 2016-06-01 抖动


    1.点击图片实现抖动

        <script src="js/domoveandgetstyle.js"></script>
        <script>
            window.onload=function(){
                var img = document.getElementById("img");
                var arr=[];//20 -20 18 -18 ...0
                var num=0;//操作数组,一般要用到的下标
                var timer=null;//取消定时器用
                var postion=getStyle(img,'left');//获取img的当前位置
                for(var i=20; i>0; i-=2){
                    arr.push(i,-i);
                }
                //alert(arr)//20 -20 18 -18 ...0
                img.onclick=function(){//点击图片触发定时器
                    timer=setInterval(function(){
                        img.style.left=postion+arr[num]+'px';
                        num++;
                        if(num==arr.length){//如果到数组的末尾,清除定时器
                            clearInterval(timer);
                        }
                    },50);
                }
            }
        </script>
    </head>
    <body>
    <img id="img" src="images/photo_04.jpg" style=" 400px ; height: 400px;position:absolute;left: 300px" />
    </body>
    

     2. domoveandgetstyle.js 

    /**
     * Created by ckang on 2016/6/1.
     */
    function doMove(obj,attr,stepLength,target,animationVelocity){
        stepLength=(getStyle(obj,attr)<target?stepLength:-stepLength);//如果obj所处的位置小于目标位置,则步长取正数,反之取负数
        clearInterval(obj.timer);
        odiv.timer=setInterval(function(){
            var speed =getStyle(obj,attr)+stepLength;//步长
            if(speed>target && stepLength>0 || speed<target && stepLength<0){
                speed=target;
            }
            obj.style[attr]=speed+'px';//每隔animationVelocity秒 移动stepLength px
            if(speed==target){//停止移动
                clearInterval(obj.timer);
            }
        },animationVelocity);
    }
    
    function getStyle(obj,attr){//为什么要parseInt(),因为obj.currentStyle[attr] 拿到的是30px -->30 拿到元素的属性的值 如位置等信息
        return parseInt(obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr]) ;
    }
  • 相关阅读:
    MathType输入框怎么调整
    几何画板中去除画出的线段的教程
    MathType怎么编辑半开半闭区间
    几何画板给月牙图形填充颜色的技巧
    MathType调整矩阵分隔线粗细的方法
    帮你深入理解OAuth2.0协议
    phalapi
    Spring松耦合实例
    让前端工程师糟心的正则表达式到底是什么?
    composer安装
  • 原文地址:https://www.cnblogs.com/bravolove/p/5551109.html
Copyright © 2020-2023  润新知