根据当前分区更改链接样式

Change Link Styling Based on Current Div

本文关键字:链接 样式 分区      更新时间:2023-09-26

我有一个HTML页面,它有一个固定位置的顶级导航菜单,其中包含指向不同部分的链接。当用户到达相应的部分时,有没有办法改变我的链接的颜色(通过点击链接本身或向下滚动到该部分)?

这就是我的HTML的样子,基本上是:

<div id="topNav">
     <ul>
          <li><a href="#contact">Contact</a></li>
          <li><a href="#web_design">Web Design</a></li>
          <li><a href="#home">Home</a></li>
     </ul>
</div>
<div id="home">
     <img src="images/dog.jpg" class="bg" />
</div>
<div id="web_design">
     <img class="titleImage" src="images/web_design.jpg" />
</div>
<div id="contact">
     <img class="titleImage" src="images/contact.jpg" />
</div>

这就是CSS:

#topNav {
    width: 100%;
    height: 60px;
    position: fixed;
    top: 20px;
}

我想为我的列表项目使用一个"选定"的类,并可能将其应用于与用户当前部分相对应的任何链接,并执行以下操作:

#topNav li.selected a {
    color: #cbcacc;
}

非常感谢您的帮助!

我认为您需要一个名为scrollspy的项。网上有一些免费的东西:

  • 这个jsFiddle是一个轻量级的例子
  • jQuery插件注册表中的一些jQuery scrollspy插件
  • Bootstrap scrollspy javascript插件

你也可以用关键词scrollspy搜索更多内容。

这是上面填写的代码:

JS-

// Cache selectors
var lastId,
    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 click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
  var href = $(this).attr("href"),
      offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
  $('html, body').stop().animate({ 
      scrollTop: offsetTop
  }, 300);
  e.preventDefault();
});
// 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 : "";
   if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }                   
});

HTML

<div id="topNav">
     <ul>
          <li><a href="#contact">Contact</a></li>
          <li><a href="#web_design">Web Design</a></li>
          <li><a href="#home">Home</a></li>
     </ul>
</div>
<div class="spacer"></div>
<div id="home">
     <img src="images/dog.jpg" class="bg" />
</div>
<div id="web_design">
     <img class="titleImage" src="images/web_design.jpg" />
</div>
<div id="contact">
     <img class="titleImage" src="images/contact.jpg" />
</div>

CSS

#topNav {
    width: 100%;
    height: 60px;
    position: fixed;
    top: 20px;
}
div.spacer {
    height: 80px;
}
#topNav li.selected a {
    color: #cbcacc;
}

CSS3有一个有趣的选择器:target,你可以根据目标设置链接样式,你可以做一些类似的事情

:target{color:#ff0000;}

尝试谷歌css3目标选择器