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

时间:2016-04-09 14:48:14

标签: javascript jquery html scroll scrolltop

我正在使用jquery函数element.scrollTop()来使用以下行获取页面的当前滚动位置:

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

但它总是返回前一个滚动位置的值。请检查下面的代码(与in here相同)。 正如您可以尝试自己并在代码中看到的,在每个小滚动之后,我们得到以下一系列值:

(1:代码首次运行且尚未移动时)

增量:0

cumulativeDelta:0

functionCallCount:0

currentScrollPosition:0

(delta表示滚动了多少,cumulativeDelta表示滚动总量,functionCallCount是滚动的次数,currentScrollPosition是scrolltop()返回的值)

(2:滚动一点时)

增量:-120

cumulativeDelta:-120

functionCallCount:1

currentScrollPosition:0

(请注意,currentScrollPosition仍未更新)

(3:进一步滚动时)

增量:-120

cumulativeDelta:-240

functionCallCount:2

currentScrollPosition:90.90908893868948

(这里,cumulativeDelta,它是目前为止制作的总滚动的加法,加倍,并且currentScrollPosition首次更新)

(4:滚动多一点时)

增量:-120

cumulativeDelta:-360

functionCallCount:3

currentScrollPosition:181.81817787737896

(现在,cumulativeDelta增加了三倍,而currentScrollPosition增加了一倍。因此,这是两次滚动后的值,但是更新了adter 3滚动)

我为长期问题道歉,否则很难问。我想知道为什么会发生这种情况,如果我应该以其他方式使用此函数,则可以选择此函数。

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>

2 个答案:

答案 0 :(得分:1)

问题是因为当鼠标滚轮开始运动时鼠标滚轮事件会触发。在更新发生之前,您正在点阅读scrollTop鼠标滚轮滚动完成后,您需要使用计时器来获取scrollTop 。试试这个:

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>

或者,您可以直接在窗口的滚动事件上阅读currentScrollTop位置,因为它始终与其当前位置保持同步。

答案 1 :(得分:0)

我将监听scroll事件,而不是wheel事件。 scrollTop值更新后,滚动激活。

target.onscroll = functionRef

很好的例子here