Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何确保用户在滚动时,不断检查滚动的距离?_Jquery - Fatal编程技术网

Jquery 如何确保用户在滚动时,不断检查滚动的距离?

Jquery 如何确保用户在滚动时,不断检查滚动的距离?,jquery,Jquery,我使用throttle调用一个函数,在这个函数中我检查用户滚动了多远。如果用户滚动到115px以上,那么我将向主体添加一个类。除非用户在移动/平板设备上继续快速滚动,否则该类有效,只有在用户停止滚动时才添加该类。我怀疑除非用户停止滚动,否则不会检查滚动。如何确保用户在滚动时,不断检查滚动的距离 var throttled = _.throttle(myFunc, 50); $(window).on('scroll', throttled); function myFunc() { var

我使用throttle调用一个函数,在这个函数中我检查用户滚动了多远。如果用户滚动到115px以上,那么我将向主体添加一个类。除非用户在移动/平板设备上继续快速滚动,否则该类有效,只有在用户停止滚动时才添加该类。我怀疑除非用户停止滚动,否则不会检查滚动。如何确保用户在滚动时,不断检查滚动的距离

var throttled = _.throttle(myFunc, 50);
$(window).on('scroll', throttled);

function myFunc() {
   var scroll = $(window).scrollTop();
   if (scroll >= 115) {
      $('body').addClass('header-fixed');
   }

我认为您必须为滚动设置事件侦听器,如:

$(document).ready(function(){
    $(window).on('scroll', function() {
    // get the scroll value
    scrollPosition = $(this).scrollTop();
    console.log(scrollPosition);
    // Put some check for the scroll position and fix the header
    });
});