在某个点停止固定位置滚动? [英] Stopping fixed position scrolling at a certain point?

查看:147
本文介绍了在某个点停止固定位置滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素的位置:固定的,所以滚动与页面,我想如何。当用户向上滚动时,我想让元素在某一点停止滚动,例如当它距离页面顶部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);

查看工作示例→

这篇关于在某个点停止固定位置滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆