问题描述
对于javascript,我几乎是全新的,我知道这里已经有类似的问题了,但是我想要脚本。
如果(用户位于页面顶部)
{
(执行此功能)
}
预先致谢
这是一个干净的解决方案,如果您是JS的新手, pre> //在滚动
时调用你的函数window.onscroll = myScrollFunction;
函数myScrollFunction(){
if(getYOffset()== 0){
//如果在顶部,请执行
alert('bingo');
}
};
//帮助函数(因为它处理与firefox不同的滚动)
函数getYOffset(){
var pageY;
if(typeof(window.pageYOffset)=='number'){
pageY = window.pageYOffset;
}
else {
pageY = document.documentElement.scrollTop;
}
返回pageY;
}
I'm pretty much completely new to javascript, and I know there's already a similar question to this on here, but i would like the script as in.
if (user is at top of page){(execute this function)}
Thanks in advance
Here's a clean solution that should make sense if you're new to JS:
//call your function on scroll
window.onscroll = myScrollFunction;
function myScrollFunction(){
if(getYOffset() == 0){
//if at top, do this
alert('bingo');
}
};
//helper function (since ie handles scrolling different than firefox)
function getYOffset() {
var pageY;
if(typeof(window.pageYOffset)=='number') {
pageY=window.pageYOffset;
}
else {
pageY=document.documentElement.scrollTop;
}
return pageY;
}
这篇关于javascript - 如何检测用户何时位于网页顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!