Top*_*ons 14 html jquery scroll scrollto
我想检查一个元素是否滚动到顶部,偏移量为~100px.
我有一个包含5个子内容和2个类的页面来制作背景.它看起来像这样:
<div class="background1">
Content1
</div>
<div class="background2">
Content2
</div>
<div class="background1">
Content3
</div>
<div class="background2">
Content4
</div>
<div class="background1">
Content5
</div>
Run Code Online (Sandbox Code Playgroud)
现在我想检查,当其中一个类通过滚动到达顶部时
这是我最后的尝试之一:
$('.background1', '.background2').position(function(){
if($(this).position().top == 100){
alert('checkThis');
}
});
Run Code Online (Sandbox Code Playgroud)
我认为这是我现在最接近的尝试...当然这个代码是在document.ready和我的代码的最后....
TLDR:如何检查元素是否滚动到顶部(和一些偏移量)?
ade*_*neo 24
您必须侦听滚动事件,然后根据当前滚动的距离检查每个元素,例如:
$(window).on('scroll', function() {
var scrollTop = $(this).scrollTop();
$('.background1, .background2').each(function() {
var topDistance = $(this).offset().top;
if ( (topDistance+100) < scrollTop ) {
alert( $(this).text() + ' was scrolled to the top' );
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33644 次 |
| 最近记录: |