search

Home  >  Q&A  >  body text

javascript - Why can't I scroll the scroll bar again after changing speed to Math.round(speed)?

var top = document.getElementsByClassName("top")[0];

   var timer = null;
   top.onclick=function()
   {
       timer=setInterval(function () {
           var cur=document.body.scrollTop;
           var speed=(0-cur)/2;
        speed=speed>0?Math.ceil(speed):Math.floor(speed);

// speed=Math.round(speed);This sentence is a comment

           if(cur==0){
               clearInterval(timer);
           }
           else{
               document.body.scrollTop=cur+speed;

           }
       },30)

   };

}
is to change the speed from speed=speed>0?Math.ceil(speed):Math.floor(speed); to
speed=Math.round(speed); and then click the element to return When you reach the top of the page, you can return to the top of the page normally, but
when you scroll the mouse wheel, the scroll bar cannot move and keeps going up, and the page keeps flashing. Why is this? Just change the speed value, and the effect is gone

怪我咯怪我咯2780 days ago856

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-19 10:47:44

    var speed = 0.4;
    speed=speed>0?Math.ceil(speed):Math.floor(speed);
    // speed = 1;
    speed=Math.round(speed);
    // speed = 0;

    reply
    0
  • Cancelreply