当前位置:网站首页>Case summary of rotation chart moving speed (constant speed, slow motion)

Case summary of rotation chart moving speed (constant speed, slow motion)

2022-07-19 05:12:00 Create splendid -- hand in hand with you

js The content part :


var box1 = document.querySelector(".box1");
         var box2 = document.querySelector(".box2");
         box1.onclick = function(){
             move(this,100);
         }
         box2.onclick = function(){
              slow(this,100);
         }

        
    
        // uniform
        function move(obj,target){
                obj.myInter =  setInterval(function(){
                    // The first step is to obtain the current distance to the left
                    var offsetLeft = obj.offsetLeft;
                    var num = 10;// go 10px
                    if(offsetLeft==target){
                        clearInterval(obj.myInter);// Clear timer
                    }else{
                        obj.style.left = offsetLeft + num + "px";
                    }
                },1000)
        }

    
        // Slow motion
        function slow(obj,target){
                obj.myInter =  setInterval(function(){
                    // The first step is to obtain the current distance to the left
                    var offsetLeft = obj.offsetLeft;
                    var num = (target - offsetLeft)/10;
                    num>0?num = Math.ceil(num):num=Math.floor(num);
                    
                    if(offsetLeft==target){
                        clearInterval(obj.myInter);// Clear timer
                    }else{
                        obj.style.left = offsetLeft + num + "px";
                    }
                },500)
        }

  The above is to make the rotation chart uniform and slow js effect , Extract below . In the process of programming, you can extract the functions , Just call it directly .

Extract part :

uniform

    // uniform
        function move(obj,target){
                obj.myInter =  setInterval(function(){
                    // The first step is to obtain the current distance to the left
                    var offsetLeft = obj.offsetLeft;
                    var num = 10;// go 10px
                    if(offsetLeft==target){
                        clearInterval(obj.myInter);// Clear timer
                    }else{
                        obj.style.left = offsetLeft + num + "px";
                    }
                },1000)
        }

Slow motion :

// Slow motion
        function slow(obj,target){
                obj.myInter =  setInterval(function(){
                    // The first step is to obtain the current distance to the left
                    var offsetLeft = obj.offsetLeft;
                    var num = (target - offsetLeft)/10;
                    num>0?num = Math.ceil(num):num=Math.floor(num);
                    
                    if(offsetLeft==target){
                        clearInterval(obj.myInter);// Clear timer
                    }else{
                        obj.style.left = offsetLeft + num + "px";
                    }
                },500)
        }

原网站

版权声明
本文为[Create splendid -- hand in hand with you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170503000205.html