gpt4 book ai didi

javascript - 滚动时检测第一个顶部元素

转载 作者:行者123 更新时间:2023-11-29 18:22:44 25 4
gpt4 key购买 nike

滚动时如何检测第一个顶部元素(谁在窗口 View 中可见)

像这样...

detect first top element

我开始......

$(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");
}
}));

http://jsfiddle.net/EUztW/10/

关于javascript - 滚动时检测第一个顶部元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16837333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com