简体   繁体   English

jquery scrolltop() 返回上一个滚动位置的值

[英]jquery scrolltop() returns value of previous scroll position

I am using jquery function element.scrollTop() to get the current scroll position of the page using following line:我正在使用 jquery 函数element.scrollTop()使用以下行获取页面的当前滚动位置:

var currentScrollPosition=  $('html').scrollTop() || $('body').scrollTop();

But it always return a value of previous scroll position.但它总是返回先前滚动位置的值。 Please check the code below (which is same as is in here ).请检查下面的代码(与此处相同)。 As you can try yourself and see in the code, after each tiny scroll, we get following series of values:正如您可以自己尝试并在代码中看到的那样,在每次微小的滚动之后,我们都会得到以下一系列值:

(1: when code is first run and no move is made yet) (1:当代码第一次运行并且还没有移动时)

delta:0增量:0

cumulativeDelta:0累积Delta:0

functionCallCount:0函数调用计数:0

currentScrollPosition:0当前滚动位置:0

(delta gives how much is scrolled, cumulativeDelta gives total amount of scroll, functionCallCount is how many times you scrolled and currentScrollPosition is value returned by scrolltop() ) (delta 给出滚动的数量,cumulativeDelta 给出滚动的总量,functionCallCount 是你滚动的次数,currentScrollPosition 是 scrolltop() 返回的值)

(2: when scrolled a little) (2:稍微滚动时)

delta:-120增量:-120

cumulativeDelta:-120累积Delta:-120

functionCallCount:1函数调用计数:1

currentScrollPosition:0当前滚动位置:0

(note here, currentScrollPosition is still not updated) (注意这里,currentScrollPosition 还没有更新)

(3: when scrolled a little further) (3:当再滚动一点时)

delta:-120增量:-120

cumulativeDelta:-240累积Delta:-240

functionCallCount:2函数调用次数:2

currentScrollPosition:90.90908893868948当前滚动位置:90.90908893868948

(here, cumulativeDelta, which is addition of total scroll made so far, is doubled and currentScrollPosition is updated for the first time) (这里,cumulativeDelta,这是迄今为止所做的总滚动的加法,加倍,并且 currentScrollPosition 是第一次更新)

(4: when scrolled a little more) (4:多滚动一点时)

delta:-120增量:-120

cumulativeDelta:-360累积Delta:-360

functionCallCount:3函数调用次数:3

currentScrollPosition:181.81817787737896当前滚动位置:181.81817787737896

(now, cumulativeDelta is tripled while currentScrollPosition is doubled. Hence, this is value after two scrolls but is updated adter 3 scrolls) (现在,cumulativeDelta 增加三倍,而 currentScrollPosition 增加一倍。因此,这是两次滚动后的值,但更新了 3 次滚动)

I apologize for long question, but would have been difficult to ask otherwise.我为长时间的问题道歉,但否则很难问。 I would like to know why is this happening and if I should use this function some other way there are any alternative to this function.我想知道为什么会发生这种情况,如果我应该以其他方式使用此功能,则可以使用此功能的任何替代方法。

 document.addEventListener("mousewheel", MouseWheelHandler); var cumulativeDelta = 0, functionCallCount = 0; function MouseWheelHandler(e) { e = window.event || e; // 'event' with old IE support var delta = e.wheelDelta || -e.detail; // get delta value cumulativeDelta += delta; functionCallCount += 1; currentScrollPosition = $('html').scrollTop() || $('body').scrollTop(); document.getElementById("info1").innerHTML = "delta:" + delta; document.getElementById("info2").innerHTML = "cumulativeDelta:" + cumulativeDelta; document.getElementById("info3").innerHTML = "functionCallCount:" + functionCallCount; document.getElementById("info4").innerHTML = "currentScrollPosition:" + currentScrollPosition; }
 body { height: 2000px; border: solid red 3px; } .normalPart { border: solid green 2px; height: 900px; } .stationary { position: fixed; top: 0px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <div class="stationary"> <div id="info1"></div> <div id="info2"></div> <div id="info3"></div> <div id="info4"></div> </div>

The issue is because the mousewheel event fires when the mousewheel movement starts.问题是因为鼠标滚轮移动开始时会触发鼠标滚轮事件。 You are reading the scrollTop at the point before the update has happened.您正在阅读更新发生之前scrollTop You need to use a timer to get the scrollTop after the mouse wheel scroll has finished.鼠标滚轮滚动完成,您需要使用计时器来获取scrollTop Try this:试试这个:

 document.addEventListener("mousewheel", MouseWheelHandler); var cumulativeDelta = 0, functionCallCount = 0, currentScrollPosition = 0; function MouseWheelHandler(e) { e = window.event || e; // 'event' with old IE support var delta = e.wheelDelta || -e.detail; // get delta value cumulativeDelta += delta; functionCallCount += 1; setTimeout(function() { currentScrollPosition = $(window).scrollTop(); document.getElementById("info4").innerHTML = "currentScrollPosition:" + currentScrollPosition; }, 200); // update currentScrollPos 200ms after event fires document.getElementById("info1").innerHTML = "delta:" + delta; document.getElementById("info2").innerHTML = "cumulativeDelta:" + cumulativeDelta; document.getElementById("info3").innerHTML = "functionCallCount:" + functionCallCount; }
 body { height: 2000px; border: solid red 3px; } .normalPart { border: solid green 2px; height: 900px; } .stationary { position: fixed; top: 0px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <div class="stationary"> <div id="info1"></div> <div id="info2"></div> <div id="info3"></div> <div id="info4"></div> </div>

Alternatively you could read the currentScrollTop position directly on the scroll event of the window as that will always be in sync with its current position.或者,您可以直接在窗口的滚动事件上读取currentScrollTop位置,因为它始终与其当前位置同步。

I would listen for the scroll event instead of wheel .我会监听scroll事件而不是wheel Scroll activates after the scrollTop value has been updated.滚动在scrollTop值更新后激活。

target.onscroll = functionRef

Great example here很好的例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM