使用lodash updatePosition不起作用

Using lodash updatePosition not working

本文关键字:不起作用 updatePosition lodash 使用      更新时间:2023-09-26

你好,我是第一次使用lodash。我的jquery代码一直在跳,我正试图在lodash函数中迁移它,但我真的不明白如何迁移。

IDK(如果这很重要的话),但代码背后的主要思想是让div向下滚动到一个位置并捕捉到某个位置,但它一直在跳跃。请建议!!

$(window).on('scroll', _.throttle(updatePosition, 100));
$(window).on('scroll', function(){
    var scroll = $(window).scrollTop();
    var sectionPosTop = document.getElementById('header-bamboo-scroll').getBoundingClientRect();
    var headerHeight = document.getElementById('header').getBoundingClientRect();

    if (scroll >= 660 && scroll <= 680 ) {
        console.log('greater than 660 and less than 680');
        $('html, body').animate({
            scrollTop: headerHeight.height
         }, 500, function() {
            $('html, body').stop();
         });
    }
 });
});

我不知道你到底想做什么,但你可以使用_.throttle来获得代码执行之间的延迟,这可以给你一个平滑的动画。您可以调整延迟时间(以毫秒为单位)以获得您想要的内容。

    var updatePosition = function() {
      var scroll = $(window).scrollTop();
      var sectionPosTop = document.getElementById('header-bamboo-scroll').getBoundingClientRect();
      var headerHeight = document.getElementById('header').getBoundingClientRect();

      if (scroll >= 660 && scroll <= 680 ) {
        console.log('greater than 660 and less than 680');
        $('html, body').animate({
          scrollTop: headerHeight.height
        }, 500, function() {
          $('html, body').stop();
        });
      }
    }
    $(window).on('scroll', _.throttle(updatePosition, 100));