我有一个简单的菜单,但我似乎无法让主动导航工作。我认为它可能与基于URL /关于的URL的代码有关,而我在链接中使用完整的URL(即。http://helloarchie.blue/about)?
HTML:
<ul id="naviga">
<li><a href="http://helloarchie.blue/">Home</a>
</li>
<li><a href="http://helloarchie.blue/category/motherhood/">Motherhood</a>
</li>
<li><a href="http://helloarchie.blue/category/fashion-friday/">Fashion</a>
</li>
<li><a href="http://helloarchie.blue/category/reviews/">Reviews</a>
</li>
<li><a href="http://helloarchie.blue/category/blogging-tips/">Blogging</a>
</li>
<li><a href="http://helloarchie.blue/category/lifestyle/">Lifestyle</a>
</li>
<li><a href="http://helloarchie.blue/about/">About</a>
</li>
<li><a href="http://helloarchie.blue/contact-me/">Contact</a>
</li>
</ul>
CSS:
#naviga li {
display: inline;
padding: 0 2%;
text-align: center;
font-family:"brandon-grotesque", "HelveticaNeue", "Helvetica Neue", Helvetica, serif;
text-transform: uppercase;
letter-spacing: 3px;
font-size: 0.75em;
}
#naviga li a {
position: relative;
}
#naviga li a:hover {
color: #585858;
font-weight: bold;
}
#naviga li a::before {
content:'';
position: absolute;
background: #f6a889;
height: 2px;
margin-top: -17px;
margin-left: -2px;
top: 0;
left: 0;
width: 0%;
-webkit-transition: all 0.65s ease;
-moz-transition: all 0.65s ease;
-o-transition: all 0.65s ease;
transition: all 0.65s ease;
}
#naviga li a:hover::before {
width: 100%;
}
#naviga li a .active {
content:'';
position: absolute;
background: #f6a889;
height: 2px;
margin-top: -16px;
margin-left: -2px;
top: 0;
left: 0;
width: 100%;
}
JS:
$(document).ready(function () {
url = window.location.pathname;
url = url.substring(1);
if (url == '') {
url = 'home';
}
$('#naviga li.active').removeClass('active');
$('#naviga li').each(function () {
if ($(this).hasClass(url)) {
$(this).addClass('active');
}
});
});
答案 0 :(得分:0)
你几乎就在那里,只是一些小问题:
<li>
CSS类设置active
的样式。但是,您的CSS意味着您要在<a>
。<li>
的样式
<li>
元素可能具有的类名进行匹配。但是,在您的HTML中,<li>
元素都没有为其分配类。简而言之:为每个<li>
提供相应的类名(例如,'home')并更新您的CSS,以便<li>
进行样式设置(或相应地更新JS)。