scroll up and down a div on button click using jquery

前端 未结 7 1459
梦毁少年i
梦毁少年i 2020-12-02 15:14

I am trying to add a feature to scroll up and down a div based on button click. I was able to do the scroll down part easily, but got stuck wit the scroll up part and one mo

7条回答
  •  臣服心动
    2020-12-02 15:19

    To solve your other problem, where you need to set scrolled if the user scrolls manually, you'd have to attach a handler to the window scroll event. Generally this is a bad idea as the handler will fire a lot, a common technique is to set a timeout, like so:

    var timer = 0;
    $(window).scroll(function() {
      if (timer) {
        clearTimeout(timer);
      }
      timer = setTimeout(function() {
        scrolled = $(window).scrollTop();
      }, 250);
    });
    

提交回复
热议问题