根据当前分区更改链接样式
Change Link Styling Based on Current Div
我有一个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目标选择器
相关文章:
- Javascript+动态菜单+当前链接样式(CSS)+基于PHP的网站
- 使用JavaScript添加单词样式,但避免链接
- Jquery 当前页面链接样式
- 基于Blob的'链接样式表'与标准'style'标签
- 你能用detectmobilebrowser链接样式表吗
- 更改页面Javascript时保持链接样式
- 使用javascript链接样式
- JavaScript样式警报,在用户导航离开页面之前带有链接
- HTML “链接”(样式表)禁用属性
- CSS 导航链接样式:删除其他元素的样式,并在将鼠标悬停在选定元素上时删除双边框
- 单击更改链接样式
- 根据当前分区更改链接样式
- 是否可以在特定的DIV中设置超链接样式?
- Rails 4:在 Ajax 调用时更改链接样式
- 如何将链接样式表添加到document.write
- 我可以得到链接样式表的文本来源
- 从多种不同的链接样式/格式解析youtube视频ID
- 使用jquery更改链接样式和属性
- 修复了引导折叠中的尴尬过渡,并删除了 .well 中访问过的链接样式
- 在单击javascript时设置超链接样式