更改页面滚动上的活动菜单项?

更改页面滚动上的活动菜单项?,第1张

更改页面滚动上的活动菜单项?

通过绑定到容器的滚动事件(通常是窗口)来完成。

快速示例

// Cache selectorsvar topMenu = $("#top-menu"),    topMenuHeight = topMenu.outerHeight()+15,    // All list items    menuItems = topMenu.find("a"),    // Anchors corresponding to menu items    scrollItems = menuItems.map(function(){      var item = $($(this).attr("href"));      if (item.length) { return item; }    });// Bind to scroll$(window).scroll(function(){   // Get container scroll position   var fromTop = $(this).scrollTop()+topMenuHeight;   // Get id of current scroll item   var cur = scrollItems.map(function(){     if ($(this).offset().top < fromTop)       return this;   });   // Get the id of the current element   cur = cur[cur.length-1];   var id = cur && cur.length ? cur[0].id : "";   // Set/remove active class   menuItems     .parent().removeClass("active")     .end().filter("[href='#"+id+"']").parent().addClass("active");});​


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5675774.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存