在某个点停止固定位置滚动?

问题描述:

我有一个元素的位置:固定的,所以滚动与页面,我想如何。当用户向上滚动时,我想让元素在某一点停止滚动,例如当它距离页面顶部250px时,这是否可能?任何帮助或建议将是有帮助的谢谢!

I have an element that is position:fixed and so scrolls with the page how i want it to however. when the user scrolls up I want the element to stop scrolling at a certain point, say when it is 250px from the top of the page, is this possible? Any help or advice would be helpful thanks!

我有一种感觉,我需要使用jquery这样做。我尝试获取用户的滚动或位置,但很困惑,是否有任何jquery解决方案?

I had a feeling that I would need to use jquery to do so. I tried getting the scrolling or location of the where the user is but got really confused, is there any jquery solutions?

一个快速的jQuery插件我只是写了,可以做你所需要的:

Here's a quick jQuery plugin I just wrote that can do what you require:

$.fn.followTo = function (pos) {
    var $this = this,
        $window = $(window);

    $window.scroll(function (e) {
        if ($window.scrollTop() > pos) {
            $this.css({
                position: 'absolute',
                top: pos
            });
        } else {
            $this.css({
                position: 'fixed',
                top: 0
            });
        }
    });
};

$('#yourDiv').followTo(250);

查看工作示例→