根据滚动位置更改css类

时间:2013-09-19 11:35:59

标签: javascript css scroll

我有以下HTML

<header class="fixed">...</header>
<div id="content">
    <div id="sidemenu" class="fixed">...</div>
    <div id="scroll">...</div>
</div>
<footer class="fixed">...</footer>

CSS和规则中有各种定位规则

.fixed {
    position: fixed;
}

这实现了仅具有id滚动移动的div的期望效果。但是,这可能会使页脚无法看到。

我想要做的是,一旦滚动div的底部到达页脚的底部,将position: fixed更改为position: absolute,然后一切都会向上滚动显示页脚。

但我不知道该怎么做。我在看waypoints,但我有点过头了。

2 个答案:

答案 0 :(得分:1)

这样的事情可以解决你的问题。试试这个:

$(window).scroll(function () {
    var scrollBottom = $(document).height() - $(window).height() - $(window).scrollTop();

    if (scrollBottom <= $("footer").height()) {
        $("footer").css("position", "absolute");
        } 
        else {
            $("footer").css("position", "fixed");
        }
});

答案 1 :(得分:0)

为此,您将有'onScroll'事件。并检查滚动div的坐标何时与页脚的坐标匹配。一旦匹配,在处理程序的实现中将位置更改为“绝对”。

相关问题