JQuery 偏移和滚动顶部问题

JQuery Offset and ScrollTop Problems

本文关键字:顶部 问题 滚动 JQuery      更新时间:2023-09-26

我正在尝试根据窗口中的滚动位置固定元素位置。

我认为这就像获取固定元素应该固定的元素的偏移量一样简单,然后当 window.scrollTop 等于它时添加 CSS,但这很奇怪。

元素的偏移量似乎大于 scrollTop 最大的数字。

还有其他方法可以让它工作吗?

我希望它在滚动页脚方面具有与此相同的功能;

http://be.blackberry.com/

但是我不想克隆元素,我想检测它何时到达页面底部附近,然后更改元素底部的位置。

提前谢谢。

这应该有助于您朝着正确的方向前进:

var footer = $("#footer");
// min amount to show when not scrolled to the bottom of the page.
var minVisable = 25;
$(parent.document).scroll(function() {
    // check position
    if (window.scrollY + window.innerHeight + minVisable > $("html").height()) {
        // if at the bottom of the page, stick the footer to it
        footer.css("position","absolute").css("top", $("html").height() - footer.height());
    } else {
        // else keep top part of footer on the screen
        footer.css("position","fixed").css("top", window.innerHeight - minVisable );
    }
});