为了账号安全,请及时绑定邮箱和手机立即绑定

缓冲动画问题

       var timer;

        function shareTabToggle(iTarget){
            var ele = document.getElementById("share");
            clearInterval(timer);
            var speed = 0;
            timer = setInterval(function() {
                if (ele.offsetLeft > iTarget) {
                    speed = (iTarget - ele.offsetLeft)/20;
                } else if (ele.offsetLeft < iTarget) {
                    speed = (iTarget - ele.offsetLeft)/20;
                } else {
                    clearInterval(timer);
                    return;
                }
                speed = speed < 0 ? Math.floor(speed) : Math.ceil(speed);
                ele.style.left = ele.offsetLeft + speed +"px";
            },30);
        };

        window.onload = init;
        function init(){
            var shareTab =  document.getElementById("share");
            if(shareTab.addEventListener){
                shareTab.addEventListener('mouseover',function(event){ event.stopPropagation();shareTabToggle(0);},false);
                shareTab.addEventListener('mouseout',function(){shareTabToggle(-200);},false);
            }else if(shareTab.attachEvent){
                shareTab.attachEvent('onmouseover',function(event){ event.cancelBubble=true;shareTabToggle(0);});
                shareTab.attachEvent('onmouseout',function(){shareTabToggle(-200);});
            }else{
                shareTab.onmouseover = function(event){
                    event.stopPropagation();
                    shareTabToggle(0);
                };
                shareTab.onmouseout = function(){
                    shareTabToggle(-200);
                };
            }
        }

老师举得例子是从快到慢的, 请问如果是从慢到快 应该怎么做?  数学公式是什么?

正在回答

2 回答

已有解决方案 虽然不够好 但是好在代码简单

        var timer;
        function shareTabToggle(iTarget){
            clearInterval(timer);
            var ele = document.getElementById("share");
            var temp =  ele.offsetLeft;
            var speedLst = [];
            var speed = 0;
            while(true){
                speed = (iTarget - temp)/10;
                speed = speed > 0 ? Math.ceil(speed) : (speed < 0 ? Math.floor(speed) : speed);
                speedLst.push(speed);
                temp += speed;
                if(temp == iTarget){
                    break;
                }
            }
            var speedSize = speedLst.length-1;
            console.log(speedLst);
            timer = setInterval(function(){
                // 数组正着读就是由快到慢, 倒着读就是慢到快
                ele.style.left = ele.offsetLeft + speedLst[speedSize--] + "px";
                if(speedSize == -1){
                    clearInterval(timer);
                }
            },30);
        };
        window.onload = init;
        function init(){
            var shareTab =  document.getElementById("share");
            if(shareTab.addEventListener){
                shareTab.addEventListener('mouseover',function(event){ event.stopPropagation();shareTabToggle(0);},false);
                shareTab.addEventListener('mouseout',function(){shareTabToggle(-200);},false);
            }else if(shareTab.attachEvent){
                shareTab.attachEvent('onmouseover',function(event){ event.cancelBubble=true;shareTabToggle(0);});
                shareTab.attachEvent('onmouseout',function(){shareTabToggle(-200);});
            }else{
                shareTab.onmouseover = function(event){
                    event.stopPropagation();
                    shareTabToggle(0);
                };
                shareTab.onmouseout = function(){
                    shareTabToggle(-200);
                };
            }
        }


0 回复 有任何疑惑可以回复我~

调过来就行了,

0 回复 有任何疑惑可以回复我~
#1

嘛也不会 提问者

请问具体怎么改
2016-10-19 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
JS动画效果
  • 参与学习       113926    人
  • 解答问题       1443    个

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

进入课程

缓冲动画问题

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信