jquery .scroll 更改 css
我对 jquery 很陌生,并且在更改滚动效果的 css 样式方面遇到了一些问题。
我有一个 div,在页面加载时我想将其固定为 top:75px,但是一旦用户开始从顶部滚动超过 75px,我想将其更改为position:fixed top: 0px。
现在我只能让它在任何滚动操作上改变,而不是等到它从顶部达到 75px。
到目前为止,我的代码非常基础,因为我有点卡在这个特定的部分上。
$(window).scroll(function () {
$("#side_bar").css("position", "fixed").css("top", "0px");
});
理想情况下,我真的希望它可以滚动,直到 #side_bar 的底部接触页脚,然后在从顶部滚动回 75px 时反转整个事情,以便它变回位置:静态,顶部:75px。
提前致谢!
I'm very new to jquery and am having a bit of an issue with changing css styles for scroll effects.
I have a div which at the load of the page I want to be fixed to top:75px, but once a user starts to scroll past 75px from top, I want to change it to position:fixed top: 0px.
For now I'm only able to get it to change on any scroll action, not wait till it reaches 75px from top.
My code so far is extremely basic as Im somewhat stuck on this particular part.
$(window).scroll(function () {
$("#side_bar").css("position", "fixed").css("top", "0px");
});
Ideally I'd really like it to be scrollable until the bottom of the #side_bar touches the footer, and then reverse the whole thing when scrolling back up to 75px from the top so that it changes back to position:static, top:75px.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery Waypoints 插件可让您轻松制作粘性元素。他们通过在滚动到该元素时添加一个 Sticky 类(导致该元素被固定)来实现该示例。
The jQuery Waypoints plugin lets you easily make sticky elements. They implemented that example by adding a
sticky
class (causing the element to be fixed) when you scroll to that element.这样的东西会为你做到这一点。请注意,与您的问题不同,您实际上并不希望将其固定为 75px。如果发生这种情况,它永远不会滚出屏幕!你希望它是相对的,然后一旦它离开屏幕就固定下来。
Something like this would do it for you. Note that unlike in your question you don't actually want it fixed at 75px. If that happened it would never scroll off the screen! You want it relative and then fixed once it would go off the screen.
这是另一个处理此问题的 jquery 插件的演示。该演示使用浮动在右侧的购物车摘要,该摘要会滚动直至到达页面顶部的边缘。该插件的描述如下:
该插件用于将元素固定在页面顶部,如果元素垂直滚动到视图之外;但是,它确实允许元素随着水平滚动继续向左或向右移动。
给定一个选项 marginTop ,一旦垂直滚动到达目标位置,元素将停止垂直向上移动;但是,当页面向左或向右滚动时,元素仍会水平移动。一旦页面向下滚动超过目标位置,该元素将恢复到其在页面上的原始位置。
该插件已在 Firefox 3/4、Google Chrome 10/11、Safari 5 和 Internet Explorer 8/9 中进行了测试。
演示:http://jsfiddle.net/y3qV5/
插件和源:https://github.com/bigspotteddog/ScrollToFixed
用法:
Here is a demo of another jquery plugin that takes care of this. The demo uses a shopping cart summary floating on the right that scrolls until it reaches a margin at the top of the page. The description of the plugin is as follows:
This plugin is used to fix elements to the top of the page, if the element would have scrolled out of view, vertically; however, it does allow the element to continue to move left or right with the horizontal scroll.
Given an option marginTop, the element will stop moving vertically upward once the vertical scroll has reached the target position; but, the element will still move horizontally as the page is scrolled left or right. Once the page has been scrolled back down past the target position, the element will be restored to its original position on the page.
This plugin has been tested in Firefox 3/4, Google Chrome 10/11, Safari 5, and Internet Explorer 8/9.
Demo: http://jsfiddle.net/y3qV5/
Plugin and source: https://github.com/bigspotteddog/ScrollToFixed
Usage: