js实现导航菜单栏随着屏幕的滚动进行滚动的效果

 1   $(window).scroll(function () {
 2             var $nav = $(".floatingMenu ul li"),
 3                 length = $nav.length-1,//获取导航菜单 ul li 的个数
 4                 item = new Array(),//新建一个数组
 5                 sTop = $(window).scrollTop();//获取偏移的高度
 6             for (var i = 0; i < length; i++) {
 7                 if (i == 0) {
 8                     item[i + 1] = $(".blockList>div:first-child").offset().top;
 9                     if (sTop >= item[i + 1]) {
10                         $nav.eq(i).addClass("selected").siblings("li").removeClass("selected");//选中的样式selected
11                     }
12                 } else {
13                     item[i + 1] = $(".blockList>div:nth-child(" + (i + 1) + ")").offset().top;
14                     if (sTop >= item[i + 1] - 100) {
15                         $nav.eq(i).addClass("selected").siblings("li").removeClass("selected");
16                     }
17                 }
18             }
19         })

 

转载于:https://www.cnblogs.com/liuhui-03/p/5886774.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个纵向滚动导航栏,可以使用 HTML、CSS 和 JavaScript 来完成。下面是一些步骤和示例代码,可以帮助你实现这个功能。 1. 首先,我们需要创建一个 HTML 结构来定义导航栏和它的菜单项。可以使用 `<ul>` 和 `<li>` 标签来创建一个有序列表,然后在列表项中添加链接。 ```html <nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> <li><a href="#section4">Section 4</a></li> </ul> </nav> ``` 2. 接下来,我们需要使用 CSS 来设置导航栏的样式。可以给 `<nav>` 元素设置一个固定的高度和宽度,并设置其 `overflow-y` 属性为 `scroll`,以便在内容溢出时显示滚动条。然后,可以设置 `<li>` 元素的样式,例如背景颜色、字体大小和间距。 ```css nav { height: 400px; width: 150px; overflow-y: scroll; } li { background-color: #f6f6f6; font-size: 16px; margin-bottom: 10px; padding: 10px; } ``` 3. 现在,我们需要使用 JavaScript 来添加一些交互性。我们可以使用 `window.addEventListener()` 方法来监听滚动事件,并在导航栏中高亮显示当前所在的节。 ```javascript window.addEventListener('scroll', function() { var sections = document.querySelectorAll('section'); var navHeight = document.querySelector('nav').offsetHeight; var currentSectionIndex = 0; for (var i = 0; i < sections.length; i++) { if (sections[i].offsetTop - navHeight <= window.scrollY) { currentSectionIndex = i; } } var activeLink = document.querySelector('nav li.active'); if (activeLink) { activeLink.classList.remove('active'); } var newActiveLink = document.querySelectorAll('nav li')[currentSectionIndex]; newActiveLink.classList.add('active'); }); ``` 在这段代码中,我们首先获取所有的节,并获取导航栏的高度。然后,我们遍历所有的节,如果某个节的顶部位置小于或等于滚动条的位置加上导航栏的高度,就将当前节的索引设置为 `currentSectionIndex`。最后,我们移除当前激活的菜单项的 `active` 类,然后将新的激活菜单项添加 `active` 类。 4. 最后,我们可以使用 CSS 来设置渐变效果。我们可以给 `.active` 类添加一个渐变背景色,使其从浅色逐渐变成深色。这样,用户就可以很容易地看出当前所在的节。 ```css li.active { background: linear-gradient(to bottom, #f6f6f6, #ccc); } ``` 这样,我们就完成了一个纵向滚动导航栏,并添加了滚动渐变效果。完整的 HTML、CSS 和 JavaScript 代码如下所示: ```html <!DOCTYPE html> <html> <head> <style> nav { height: 400px; width: 150px; overflow-y: scroll; } li { background-color: #f6f6f6; font-size: 16px; margin-bottom: 10px; padding: 10px; } li.active { background: linear-gradient(to bottom, #f6f6f6, #ccc); } </style> </head> <body> <nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> <li><a href="#section4">Section 4</a></li> </ul> </nav> <section id="section1">Section 1</section> <section id="section2">Section 2</section> <section id="section3">Section 3</section> <section id="section4">Section 4</section> <script> window.addEventListener('scroll', function() { var sections = document.querySelectorAll('section'); var navHeight = document.querySelector('nav').offsetHeight; var currentSectionIndex = 0; for (var i = 0; i < sections.length; i++) { if (sections[i].offsetTop - navHeight <= window.scrollY) { currentSectionIndex = i; } } var activeLink = document.querySelector('nav li.active'); if (activeLink) { activeLink.classList.remove('active'); } var newActiveLink = document.querySelectorAll('nav li')[currentSectionIndex]; newActiveLink.classList.add('active'); }); </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值