JQuery 窗口滚动事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14283205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery window scrolling event?
提问by Jai
I have an ad in my header and a fixed ad at the bottom of my page that is always there. I want the fixed ad to appear only if the user has scrolled under the header ad. I looked into the JQuery documentation, but I'm not really sure what I should use.
我的标题中有一个广告,页面底部有一个固定广告,它总是在那里。我希望固定广告仅在用户滚动到标题广告下方时出现。我查看了 JQuery 文档,但我不确定应该使用什么。
回答by Jai
Try this: http://jsbin.com/axaler/3/edit
试试这个:http: //jsbin.com/axaler/3/edit
$(function(){
$(window).scroll(function(){
var aTop = $('.ad').height();
if($(this).scrollTop()>=aTop){
alert('header just passed.');
// instead of alert you can use to show your ad
// something like $('#footAd').slideup();
}
});
});
回答by Kyle
See jQuery.scroll(). You can bind this to the window element to get your desired event hook.
请参阅jQuery.scroll()。您可以将其绑定到 window 元素以获得所需的事件挂钩。
On scroll, then simply check your scroll position:
在滚动时,只需检查您的滚动位置:
$(window).scroll(function() {
var scrollTop = $(window).scrollTop();
if ( scrollTop > $(headerElem).offset().top ) {
// display add
}
});
回答by Jonathon Cwik
Check if the user has scrolled past the header ad, then display the footer ad.
检查用户是否已滚动过页眉广告,然后显示页脚广告。
if($(your header ad).position().top < 0) { $(your footer ad).show() }
if($(your header ad).position().top < 0) { $(your footer ad).show() }
Am I correct at what you are looking for?
我在寻找什么是正确的吗?