javascript - 通过<a></a> </div>内的<div>突出显示当前菜单项

时间:2014-08-14 22:04:33

标签: javascript jquery css highlight

我想通过javascript使用div class INSIDE来突出显示当前菜单项。在第一次尝试中,我刚刚添加了课程&#34; top-bar-underline&#34;到a-tag,但由于菜单项是&#34; display:block;&#34;这样下划线超出了菜单项的字体,我将下划线放在a-tag内的div-container中。 对于悬停效果,这非常合适。

问题:

如何添加课程&#34; top-bar-underline-current&#34;上课&#34;顶栏 - 下划线&#34;对于当前活动菜单项?


我目前的代码如下:

html菜单:

<ul class="right">                
                <li>
                    <a href="{{ site.url }}"><div class="top-bar-underline">LATEST</div></a>
                </li>
                <li>
                    <a href="{{ site.url }}/pages/about/"><div class="top-bar-underline">ABOUT</div></a>
                </li>

</ul>

js突出显示菜单中的当前a:

<script type="text/javascript">
var url = window.location.href;
$('.top-bar-section li:not(.has-form) a:not(.button)').filter(function() {
    return this.href == url;
}).addClass('top-bar-underline-current');
</script>

的CSS:

.top-bar-underline {
position: relative;
color: #ff3296;
}
.top-bar-underline:after {
display: block;
position: absolute;
top: 70%;
left: 0;
width: 100%;
height: 2px;
background: rgba($body-font-color,0.9);
content: '';
opacity: 0;
-webkit-transition: opacity 0.2s, -webkit-transform 0.2s;
-moz-transition: opacity 0.2s, -moz-transform 0.2s;
transition: opacity 0.2s, transform 0.2s;
-webkit-transform: translateY(10px);
-moz-transform: translateY(10px);
transform: translateY(10px);
}
.top-bar-underline:hover {
color: #98004a;

}
.top-bar-underline:hover:after,
.top-bar-underline:focus:after {
 width: 100%;
 opacity: 1;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
transform: translateY(0px);
}
.top-bar-underline-current {
position: relative;
}
.top-bar-underline-current:after {
position: absolute;
top: 70%;
left: 0;
width: 100%;
height: 2px;
background: rgba($body-font-color,0.9);
content: '';
opacity: 1;
-webkit-transition: opacity 0.2s, -webkit-transform 0.2s;
-moz-transition: opacity 0.2s, -moz-transform 0.2s;
transition: opacity 0.2s, transform 0.2s;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
transform: translateY(0px);
}
.top-bar-underline-current:hover:after,
.top-bar-underline-current:focus:after {
width: 100%;
opacity: 1;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
transform: translateY(0px);
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

var url = window.location.href;
$('.top-bar-section li:not(.has-form) a:not(.button)').filter(function() {
    return this.href == url;
}).find('.top-bar-underline').addClass('top-bar-underline-current');

我认为这就是你需要的......