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解决方案吗?

推荐答案

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

See working example →

Jquery相关问答推荐

使用jquery ui来回滑动切换

如何在 jquery 中切换 attr()

如何使用 jQuery 的 drop 事件上传从桌面拖动的文件?

无限滚动jQuery插件

如何在 jQuery 中 Select 特定的表单元素?

使用淡入淡出和追加

如何使用jQuery删除父元素

在 jQuery 中,我想删除 div 中的所有 HTML

如何正确卸载/销毁 VIDEO 元素

如何获取文档的滚动位置?

jQuery计算所有文本字段中的值的总和

使用 Javascript|jQuery 删除特定的内联样式

单击后如何禁用提交按钮?

我可以将 Nuget 保留在 jQuery 1.9.x/1.x 路径上(而不是升级到 2.x)吗?

jQuery UI 工具提示不支持 html 内容

jQuery、复选框和 .is(":checked")

如何使用 javascript 展开和折叠

如何使用 jquery 更改元素类型

HTMLCollection、NodeLists 和对象数组之间的区别

如何使用 jQuery 处理复选框的更改?