将 div 保持在视口内或滚动时任意 div
我正在尝试找到一个脚本脚本(或创建一个脚本),它可以使垂直导航栏“粘在”我的视口内。虽然这可能不是黑魔法(可以使用位置:固定CSS),但这种方法存在一个问题:如果导航栏长于视口高度,则访问者将无法看到整个导航。所以我希望导航栏不要将视口留在其顶部,但也不要将父容器留在其底部。它应该立即跟随滚动事件,而不需要花哨/弹跳动画。
这可能吗?谷歌没有透露任何对我有用的信息。你可能从slashdot的文章评论导航器或google视频的播放器框知道这种效果。
更新:本质上是对 jQuery 插件的重写 scrollFollow。如果它缺少动画也没有问题,因为我不会使用它们。但它应该能够保留在定义的父容器内而不需要剪切。 cookie 处理也不需要。
I'm trying to find a scriptaculous script (or create one) which keeps a vertical navigation bar "sticky" inside my viewport. While this may not be black magic (one could use the position:fixed css) there is a problem with this approach: If the navigation bar is longer than the viewport height the visitor won't be able to ever see the whole navigation. So I want that the navigation bar to not leave the viewport at its top but neither leave the parent container at its bottom. It should follow scroll events instantly without fancy/bouncing animations.
Is that possible? Google revealed nothing useful to me. You may know this kind of effect from slashdot's article comment navigator or google video's player box.
Update: Essentially that would be a rewrite of the jQuery plugin scrollFollow. It would be no problem if it lacked animations as I won't use them. But it should be able to stay within a defined parent container without clipping. The cookie handling is neither needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这确实很旧,但在尝试找到自己的解决方案时偶然发现了
这个链接显示了如何在真正的轻便时尚。基本上你需要的只是下面的代码(当然还有加载的 jQuery)
如果你不希望它有动画效果,只需像这样替换动画部分
:
I know this is really old but stumbled across this whilst trying to find my own solution
This link shows how to do it in a really lightweight fashion. Essentially all you need is the following code (along with jQuery loaded of course)
If you don't want it to animate simply replace the animation sections like so:
with
我知道这个问题很老了,但它出现在我的 Google 搜索中,让 div 停留在页面上进行滚动。这个解决方案工作得很好,并且在我的情况下不允许页面无限滚动,就像上面@Chris 的解决方案一样。
它需要在浮动 div 上方添加一个空 div
作为锚点。
在这个例子中,div需要有
id="sticky"
解决方案:http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html
JavaScript:
CSS:
I know this issue is old but it came up in my Google search for making a div stay on the page for scrolling. This solution works quite well and didn't allow the page to have an infinite scroll in my situation like the solution from @Chris above.
It requires adding an empty div
<div id="sticky-anchor"></div>
above the floating div to serve as an anchor.In this example, the div needs to have
id="sticky"
Solution: http://blog.yjl.im/2010/01/stick-div-at-top-after-scrolling.html
Javascript:
CSS: