在某个点停止固定位置滚动?
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 →
看到工作示例→
更多相关文章
- 具有嵌套元素的jQuery悬停事件
- Jquery隐藏()除一个类外所有具有特定类的元素
- jQuery -> 获取后代元素的三种方法
- JQuery操作<select>元素
- jQuery捕获被点击元素的ID
- Yii - 加载ajax表单元素的用户端验证
- 删除后重新插入后保留对元素的引用
- jquery中获取相邻元素相关的命令:next()、prev()和siblings()
- 如何选择两个h2之间的所有元素