使用多个标头制作 jQuery 粘性标头

发布于 2024-12-09 02:18:21 字数 1797 浏览 0 评论 0原文

我试图模仿 gMail 在经过某个部分时如何将标题粘贴到滚动顶部。

代码:

$(window).scroll(function() {
    $('.the-post').each(function() {
        var elem = $(this),
            y = $(window).scrollTop(),
            maxY = elem.children('.body').offset().top,
            header = elem.children('.header'),
            scrollHeight = 24;

        if(y >= maxY-scrollHeight) {
            $('.afloat').remove();
            header
                .clone()
                .appendTo('.post')
                .addClass('afloat');
            setSizes()
        } else $('.afloat').remove()
    })
})

我使用 .clone() 因为我不想中断页面的高度。刚刚添加课程时,会有轻微的推动,而这正是我想要避免的。

然而,它只在其中一个帖子上执行此操作。有人能看出为什么吗?

我还想在标题存在且窗口位于边界内时停止运行该函数。

更新

所以我想通了。上面代码的问题是 else 语句,它删除 div 的速度太快了。我对它进行了相当多的改进,并且希望有更多的输入,但这运行良好并且没有过载:

$(window).scroll(function() {
    var y = $(this).scrollTop();
    if(y >= maxY-24) {
        if(!isset) { 
            isset = true;
            $('.post').append(header
                .clone()
                .addClass('afloat')
            )
        } 
        $('.the-post').each(function() {
            var elem = $(this),
                maxY = elem.children('.body').offset().top,
                header = elem.children('.header');
            if(y >= maxY-24 && y <= maxY+24) {
                newtext = header.children('em').text();
                if(newtext != curtext) {
                    curtext = newtext;
                    $('.afloat').text(newtext)
                }
            }
        })
    } else {
        isset = false;
        $('.afloat').remove()
    }
})

我必须在这里重新执行逻辑。一旦适用,我立即创建了静态标头,这样我就不必保留克隆 div。

然后我遍历每个 div (.the-post),并将其设为范围方法,以防止额外的、无用的查询。

I'm trying to imitate how gMail sticks the header to the top on scroll, when going past a certain part.

The code:

$(window).scroll(function() {
    $('.the-post').each(function() {
        var elem = $(this),
            y = $(window).scrollTop(),
            maxY = elem.children('.body').offset().top,
            header = elem.children('.header'),
            scrollHeight = 24;

        if(y >= maxY-scrollHeight) {
            $('.afloat').remove();
            header
                .clone()
                .appendTo('.post')
                .addClass('afloat');
            setSizes()
        } else $('.afloat').remove()
    })
})

I'm using .clone() because I don't want to interrupt the heighth of the page. When just adding the class, there's a slight nudge and it's exactly what I want to avoid.

However, it only does it on ONE of the posts. Can someone see why?

I also want to stop running the function once the header is there and the window is within boundaries.

UPDATE

So I figured this out. The problem with the above code was the else statement, it was removing the divs too fast. I improved it quite a bit and would love some more input, but this is working well and without overload:

$(window).scroll(function() {
    var y = $(this).scrollTop();
    if(y >= maxY-24) {
        if(!isset) { 
            isset = true;
            $('.post').append(header
                .clone()
                .addClass('afloat')
            )
        } 
        $('.the-post').each(function() {
            var elem = $(this),
                maxY = elem.children('.body').offset().top,
                header = elem.children('.header');
            if(y >= maxY-24 && y <= maxY+24) {
                newtext = header.children('em').text();
                if(newtext != curtext) {
                    curtext = newtext;
                    $('.afloat').text(newtext)
                }
            }
        })
    } else {
        isset = false;
        $('.afloat').remove()
    }
})

I had to re-do the logic here. I created the static header right away once applicable, so that I do not have to keep cloning divs.

Then I run through the divs (.the-post) with each, and made it a range-method so to prevent extra, useless queries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-12-16 02:18:21

答案是:

$(window).scroll(function() {

var y = $(this).scrollTop();
if(y >= maxY-24) {
    if(!isset) { 
        isset = true;
        $('.post').append(header
            .clone()
            .addClass('afloat')
        )
    } 
    $('.the-post').each(function() {
        var elem = $(this),
            maxY = elem.children('.body').offset().top,
            header = elem.children('.header');
        if(y >= maxY-24 && y <= maxY+24) {
            newtext = header.children('em').text();
            if(newtext != curtext) {
                curtext = newtext;
                $('.afloat').text(newtext)
            }
        }
    })

} else {
    isset = false;
    $('.afloat').remove()
}

})

The answer is:

$(window).scroll(function() {

var y = $(this).scrollTop();
if(y >= maxY-24) {
    if(!isset) { 
        isset = true;
        $('.post').append(header
            .clone()
            .addClass('afloat')
        )
    } 
    $('.the-post').each(function() {
        var elem = $(this),
            maxY = elem.children('.body').offset().top,
            header = elem.children('.header');
        if(y >= maxY-24 && y <= maxY+24) {
            newtext = header.children('em').text();
            if(newtext != curtext) {
                curtext = newtext;
                $('.afloat').text(newtext)
            }
        }
    })

} else {
    isset = false;
    $('.afloat').remove()
}

})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文