ScrollTop为什么始终为0?

huangapple go评论68阅读模式
英文:

Why ScrollTop is always 0?

问题

我想知道scrollTop是什么,以及如何使用jQuery。但是,它总是显示为0。

这是我的jQuery代码:

var scrollTop = $(window).scrollTop()

//滚动时
window.onscroll = function (e) {
    console.log(scrollTop);
}

如果需要的话,我也可以添加HTML和CSS代码。

我尝试使用JavaScript:

var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;

//滚动时
window.onscroll = function (e) {
    console.log(scrollTop);
}

但是,结果相同。

如果答案不是用jQuery编写的,那会更好。

英文:

I want to know what is scrollTop and using jQuery. But, it's always showing 0.

here is my jQuery code:

var scrollTop = $(window).scrollTop()

//onscroll
window.onscroll = function (e) {
    console.log(scrollTop);
}

If need, I can also add HTML and CSS codes.

I try to use JavaScript:

var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;

//onscroll
window.onscroll = function (e) {
    console.log(scrollTop);
}

But, same.

If answer isn't writed in jQuery it's better.

答案1

得分: 0

scrollTop 初始值将被设置为0,然后每次记录它时,它都将使用该值。

如果您想要一个新的 scrollTop 值,需要重新计算它。将计算放在绑定到 window.onscroll 的函数内部。

英文:

scrollTop will initially get set to 0, then every time you log it, it's going to use that value.

If you want a new value for scrollTop, it needs to be recalculated. Put the calculation inside of your function bound to window.onscroll.

答案2

得分: 0

scrollTop的值在onscroll事件处理程序中没有被动态更新。相反,在脚本最初运行时只分配一次。
也许可以这样?

使用jQuery:

$(window).scroll(function () {
  var scrollTop = $(window).scrollTop();
  console.log(scrollTop);
});

纯JS:

window.addEventListener('scroll', function () {
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  console.log(scrollTop);
});
英文:

The value of scrollTop is not being updated dynamically in the onscroll event handler. Instead, it is only assigned once when the script is initially run.
Maybe this?

Using jQuery:

$(window).scroll(function () {
  var scrollTop = $(window).scrollTop();
  console.log(scrollTop);
});

Plain JS:

window.addEventListener('scroll', function () {
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
  console.log(scrollTop);
});

答案3

得分: 0

你的 scrollTop 变量被初始化,但在 onscroll 函数内没有更新,这就是为什么它保持不变的原因。解释:你的 onscroll 函数在每次移动鼠标时都会被调用,并且它的状态(位置:e.pageY)存储在 onscroll 函数的变量 e 中。

以下是使用 jQuery 在滚动时获取光标/指针位置的方法:

$(window).on('scroll', function(e) {
  var current_position_from_top = e.pageY;
  console.log('垂直像素位置:', current_position_from_top);
});
英文:

Your scrollTop variable is initialized but it is not updated inside the onscroll function, that's why it remains unchanged. Explanation: your onscroll function is called each time you move your cursor, and its state (positions:e.pageY) is stored in the variable e of your onscroll function.

Here is a way to get the cursor/pointer position onScroll using jQuery:

$(window).on('scroll', function(e) {
  var current_position_from_top = e.pageY;
  console.log('Vertical position in pixel:', current_position_from_top);
});

huangapple
  • 本文由 发表于 2023年7月11日 00:22:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定