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!

我有一个位置元素:固定的,因此可以按照我希望的方式对页面进行滚动。当用户向上滚动时,我希望元素在某个点停止滚动,比如当它离页面顶部250像素时,这可能吗?任何帮助或建议都是有益的,谢谢!

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才能做到这一点。我尝试获取用户所在位置的滚动或位置,但真的很困惑,是否有jquery解决方案?

13 个解决方案

#1


113

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

这里有一个我刚刚编写的jQuery快速插件,可以满足您的需要:

$.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);

See working example →

看到工作示例→

更多相关文章

  1. 具有嵌套元素的jQuery悬停事件
  2. Jquery隐藏()除一个类外所有具有特定类的元素
  3. jQuery -> 获取后代元素的三种方法
  4. JQuery操作<select>元素
  5. jQuery捕获被点击元素的ID
  6. Yii - 加载ajax表单元素的用户端验证
  7. 删除后重新插入后保留对元素的引用
  8. jquery中获取相邻元素相关的命令:next()、prev()和siblings()
  9. 如何选择两个h2之间的所有元素

随机推荐

  1. Android(安卓)wifi设计原理(源码分析)
  2. Android应用模块基本配置元素
  3. java.lang.NoClassDefFoundError错误解决
  4. Android UEventObserver
  5. Android(安卓)混淆 Unknown verification
  6. 沈大海移动开发入门课程android,j2me, 在
  7. Opencv for Android(安卓)之透视变换
  8. [Android Studio] Android studio 多渠道
  9. android单元测试----junit
  10. AndroidManifest.xml里面的activity的几