当 div # 滚动到视图中时,jQuery 更改导航上的 css
我希望重新创建此网站上使用的效果: http://www.brizk.com/
网站使用一个向下滚动的大页面。当您向下滚动并传递不同的部分时,左侧的菜单导航会在相应的 div 进入视图时将 css 类更改为“当前”。
我认为这可以通过 jQuery 使用 $(window).height(); 来完成
我对 jQuery 相当陌生,我想写的是这样的(通俗地说):
- 获取浏览器窗口的高度 – 如果 div#content1 距离顶部 100 像素和/或距离底部 200 像素,则将菜单 a#link1 更改为“.current” – 否则从所有菜单 a 链接中删除 .current
...并对 4 个不同的内容 div 重复此操作。
谁能指出我正确的方向..? 谢谢。
I'm looking to recreate the effect used on this site: http://www.brizk.com/
The site uses one large page that scrolls down. As you scroll down and pass different sections the menu navigation on the left changes css class to "current" as the corresponding div comes into view.
I presume this can be done with jQuery using $(window).height();
I'm fairly new to jQuery and what I want to write is something like this (in laymans terms):
- Get height of browser window
– if div#content1 is 100px from top and/or 200px from bottom change menu a#link1 to '.current'
– else remove .current from all menu a links
... and repeat for 4 different content divs.
Can anyone point me in the right direction..?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有看代码示例(挑战自己更有趣:P),但这就是我要做的 - 此处演示。
我保存了每个元素块的位置以尽量减少 DOM 调用的次数,然后只在数组中进行搜索。帮助您了解一些变量。
脚本:
I didn't look at the code example (it's more fun to challenge myself :P) but this is how I would do it - demo here.
I saved the position of each element block to minimize the number of DOM calls, then just searched through the array. To help you understand some of the variables.
Script:
您可以使用
$(window).scrollTop();
来了解距离最顶部有多远。JS:
CSS:
HTML:
You can use
$(window).scrollTop();
to know how far you've gone from the very top.JS:
CSS:
HTML:
很好,谢谢,这帮助我理解了窗口和链接之间的关系,但我不想使用特定的像素高度,而是与保存每个链接内容的 div 相关。
我展示的原始站点使用以下内容:
现在,这只是代码的一部分,它还控制右侧出现的徽标,但我感兴趣(并困惑)的部分是变量 scrollposition 和 sectionheight 必须使菜单能够根据该部分是否在视图中更改类。
Nice, thanks that's helped me to understand the relationships between the window the links but I didn't want to use specific pixel heights but instead relate to the divs that hold the content for each link.
The original site I showed uses the following:
Now, this is only part of the code and it also controls the logo that comes in on the right, but the part that interests (and confuses) me is the variables scrollposition and sectionheight which must enable the menu to change class based on whether or not the section is in view.