當懸停在鏈接上時,滑動線的顏色不會改變,但我希望它與上面鏈接的顏色相匹配。我怎樣才能做到這一點?在懸停時滑動底線,顏色變化
$(function() {
$("#example-one").append("<div id='slider'></div>");
var $slider = $("#slider");
$slider
.width($(".current_page").width())
.css("left", $(".current_page a").position().left)
.data("origLeft", $slider.position().left)
.data("origWidth", $slider.width());
$("#example-one div").find("a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$slider.stop().animate({
left: leftPos,
width: newWidth
});
}, function() {
$slider.stop().animate({
left: $slider.data("origLeft"),
width: $slider.data("origWidth")
});
});
});
* {
margin: 0;
padding: 0;
}
body {
font-family: Arrial Narrow;
background: #2F2626;
}
#nav {
margin: 15px;
}
#example-one {
list-style: none;
position: relative;
word-spacing: 15px;
}
#example-one div {
display: inline-block;
}
#example-one a {
color: white;
font-size: 18px;
float: left;
text-decoration: none;
text-transform: uppercase;
}
#slider {
position: absolute;
bottom: -1px;
left: 0;
height: 1px;
background: red;
}
.current_page a {
color: red !important;
}
.blue a:hover {
color: blue !important;
}
.orange a:hover {
color: orange !important;
}
.yellow a:hover {
color: yellow !important;
}
.lime a:hover {
color: lime !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nav">
<div id="example-one">
<div class="current_page"><a href="#">Home</a></div>
<div class="blue"><a href="#">About</a></div>
<div class="orange"><a href="#">Service</a></div>
<div class="yellow"><a href="#">Portfolio</a></div>
<div class="lime"><a href="#">Contact</a></div>
</div>
</div>
不錯!你也可以添加'transition:background .3s linear;':http://jsfiddle.net/GJQA5/1/ – dfsq