JQuery窗口滚动事件?

39 jquery scroll

我的标题中有一个广告,页面底部有一个固定的广告,它始终存在.我希望仅当用户在标题广告下滚动时才会显示固定广告.我查看了JQuery文档,但我不确定应该使用什么.

Jai*_*Jai 91

试试这个: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();
    }
  });
});
Run Code Online (Sandbox Code Playgroud)


Kyl*_*yle 23

请参阅jQuery.scroll().您可以将其绑定到window元素以获取所需的事件挂钩.

在滚动时,只需检查您的滚动位置:

$(window).scroll(function() {
  var scrollTop = $(window).scrollTop();
  if ( scrollTop > $(headerElem).offset().top ) { 
    // display add
  }
});
Run Code Online (Sandbox Code Playgroud)