滚动时如何检测第一个顶部元素(谁在窗口 View 上可见)?
像这样...
我从..
开始$(window).scroll($.debounce(250,function(){
$('.box').removeClass('current').each(function() {
//if(statement) {
// $(this).addClass('current')
//}
});
}));
Playground : http://jsfiddle.net/l2aelba/EUztW/
我不知道如何检测哪个第一个框在顶部。
最佳答案
$(window).scroll($.debounce(250, function(){
var scrollTop = $(window).scrollTop();
var $first;
var firstPos;
$('.box').each(function() {
var $box = $(this).removeClass('current');
var pos = $box.offset();
if(pos.top > scrollTop && (!firstPos || pos.top < firstPos.top)){
$first = $box;
firstPos = pos;
}
});
if($first){
$first.addClass("current");
}
}));
关于javascript - 滚动时检测第一个顶部元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16837333/