滚动然后对齐到顶部
只是想知道是否有人知道我如何重新创建我不久前看到的导航栏样式,我刚刚找到了我在上面看到的网站,但不确定他们是如何到达那里的。基本上希望它随页面滚动然后锁定到顶部...
Just wondering if anyone has an idea as to how I might re-create a nav bar style that I saw a while ago, I just found the site I saw it on, but am not sure how they might have gotten there. Basically want it to scroll with the page then lock to the top...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需在 http://lesscss.org/ 上快速“查看源代码”,您就会看到:
他们'重新绑定到窗口的
onscroll
事件,该事件在以下情况下触发窗口滚动。当菜单“锁定”到页面顶部时,docked
标志设置为 true,在该标志的同时,菜单设置为position:fixed
设置为true
。剩下的只是一些简单的“我们是否要将菜单滚动到页面之外”和“我们是否要回到我们开始的位置”位置检查逻辑。不过,您必须小心
onscroll
事件,它们可以快速连续地大量触发,因此您的处理程序需要非常快,并且应该尽可能多地进行预先计算。在 jQuery 中,它看起来几乎是一样的:
你经常会看到这种事情,比如“浮动几乎固定位置的垂直工具栏”,例如 cracked.com。
Just do a quick "view source" on http://lesscss.org/ and you'll see this:
They're binding to the
onscroll
event for the window, this event is triggered when the window scrolls. Thedocked
flag is set to true when the menu is "locked" to the top of the page, the menu is set toposition:fixed
at the same time that that flag is set totrue
. The rest is just some simple "are we about to scroll the menu off the page" and "are we about back where we started" position checking logic.You have to be careful with
onscroll
events though, they can fire a lot in rapid succession so your handler needs to be pretty quick and should precompute as much as possible.In jQuery, it would look pretty much the same:
You see this sort of thing quite often with the "floating almost fixed position vertical toolbar" things such as those on cracked.com.
mu 太短了,答案有效,我只是发布这个来给你 jquery 脚本!
mu is too short answer is working, I'm just posting this to give you the jquery script!
穆的回答让我走了很远。我尝试了replicationg lesscss.org 的方法,但遇到了浏览器调整大小和缩放的问题。我花了一段时间才弄清楚如何正确应对该问题以及如何重置初始位置(
init
),无需 jQuery 或任何其他库。在 JSFiddle 上查找预览: http://jsfiddle.net/ctietze/zeasg/< /a>
下面是详细的纯 JavaScript 代码,以防 JSFiddle 拒绝工作。
可重用的滚动然后捕捉菜单类
这是一个可重用的版本。我将滚动检查放入一个类中,因为涉及的辅助方法弄乱了我的主命名空间:
处理缩放/页面调整大小事件
Mu's answer got me far. I tried my luck with replicationg lesscss.org's approach but ran into issues on browser resizing and zooming. Took me a while to find out how to react to that properly and how to reset the initial position (
init
) without jQuery or any other library.Find a preview on JSFiddle: http://jsfiddle.net/ctietze/zeasg/
So here's the plain JavaScript code in detail, just in case JSFiddle refuses to work.
Reusable scroll-then-snap menu class
Here's a reusable version. I put the scrolling checks into a class because the helper methods involved cluttered my main namespace:
Handle zoom/page resize events
听起来像是 Jquery ScrollTop 的应用程序以及对导航栏元素的 CSS 属性的一些操作。例如,在某些滚动条件下,导航栏元素从具有计算坐标的绝对定位更改为固定定位。
http://api.jquery.com/scrollTop/
Sounds like an application of Jquery ScrollTop and some manipulation of CSS properties of the navbar element. So for example, under certain scroll conditions the navbar element is changed from absolute positioning with calculated co-ordinates to fixed positioning.
http://api.jquery.com/scrollTop/
您描述的效果通常会以某种类型的动画开始,就像 TheDeveloper 的答案一样。默认动画通常通过随着时间的推移改变元素的位置来滑动元素,或者通过改变元素的不透明度等来淡入/淡出元素。
获得“弹回”或“捕捉到”效果通常涉及缓动。所有主要框架都提供某种形式的缓动。这完全取决于个人喜好;他们中的任何一个都不会出错。
jQuery 有 缓动插件,您可以将其与
.animate() 函数,或者您可以使用 jQueryUI。
MooTools 在核心库的 FX 类中内置缓动。
Yahoo 的 YUI 还具有内置缓动。
如果您还记得它是什么网站,您可以随时再次访问它并查看其源代码以了解使用的框架和效果。
The effect you describe would usually start with some type of animation, like in TheDeveloper's answer. Default animations typically slide an element around by changing its position over time or fade an element in/out by changing its opacity, etc.
Getting the "bouce back" or "snap to" effect usually involves easing. All major frameworks have some form of easing available. It's all about personal preference; you can't really go wrong with any of them.
jQuery has easing plugins that you could use with the
.animate()
function, or you can use jQueryUI.MooTools has easing built in to the FX class of the core library.
Yahoo's YUI also has easing built in.
If you can remember what site it was, you could always visit it again and take a look at their source to see what framework and effect was used.