Make “scrollLeft” / “scrollTop” changes not trigger scroll event listener

前端 未结 5 652
刺人心
刺人心 2020-11-30 10:02

Currently my program is in a spot where it both listens for the user to scroll a certain element, but also, at times, automatically scrolls this element by itself. (Not a gr

5条回答
  •  孤城傲影
    2020-11-30 10:19

    You could try storing the offset top of your element and matching it against the scrollTop when you step inside onScroll:

    function onScroll(){
        var scrollTop = (document.documentElement.scrollTop || document.body.scrollTop),
        offsetTop = $('selector').offset().top;
        if(offsetTop > scrollTop){
          //user has not scrolled to element so do auto scrolling
        }
    }
    

提交回复
热议问题