获取滚动的“距离" [英] getting `distance` of scroll

查看:30
本文介绍了获取滚动的“距离"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

usinf javascript 是否可以计算出距离,或者窗口向下滚动的像素数?

usinf javascript is it possible to work out the distance, or how far in pixels a window has been scrolled down?

问候菲尔

推荐答案

这将用于获取元素到文档顶部的距离:document.documentElement.scrollTop

This will work to get the distance of an element from the top of the document: document.documentElement.scrollTop

您需要确保元素可滚动.

You need to make sure that the element is scrollable.

来自 Mozilla MDN:

From Mozilla MDN:

如果元素无法滚动(例如它没有溢出或元素不可滚动),scrollTop 设置为 0

If the element can't be scrolled (e.g. it has no overflow or if the element is non-scrollable), scrollTop is set to 0

这篇关于获取滚动的“距离"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆