我有一个菜单,它根据选择的锚点使用锚标记来提供不同的内容。当触发菜单项时,通过将链接上的哈希与浏览器URL中的哈希相匹配,将活动类添加到父项。
一切都在桌面上完美运行,但一旦我去手机,我就不会开火。
代码如下:
$(window).on('hashchange', function(e) {
if ($('.menu-item').hasClass('active')) {
$(".menu-item").removeClass("active");
$('[href$="' + window.location.hash + '"]').parent(this).addClass("active");
}
});
#artistNavigation {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
text-align: left;
width: 100%;
height: auto;
margin: 0 auto;
position: relative;
font-family: "Oswald", sans-serif;
box-shadow: 0 1px 0 0 #ddd;
-webkit-box-shadow: 0 1px 0 0 #ddd;
}
@media (max-width: 1000px) {
#artistNavigation {
padding-top: 50px;
width: calc(100% - 60px);
position: relative;
}
#artistNavigation.open li.menu-item {
display: block;
}
}
#artistNavigation li {
display: inline-block;
*display: inline;
zoom: 1;
text-align: left;
vertical-align: middle;
padding-right: 45px;
position: relative;
/*active state*/
}
@media (max-width: 1000px) {
#artistNavigation li {
display: block;
text-align: center;
}
#artistNavigation li.mobi {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 30px;
z-index: 99;
}
#artistNavigation li.menu-item {
display: none;
list-style-type: none;
}
#artistNavigation li.active {
display: block !important;
position: absolute;
top: 0;
left: 0;
right: 70px;
height: 30px;
z-index: 89;
display: block;
background: #fff;
}
#artistNavigation li.active a {
-webkit-transform: translateX(35px);
transform: translateX(35px);
}
}
#artistNavigation li a {
-webkit-transition: color 200ms ease;
-moz-transition: color 200ms ease;
-o-transition: color 200ms ease;
-ms-transition: color 200ms ease;
transition: color 200ms ease;
display: inline-block;
*display: inline;
zoom: 1;
padding: 17px 0;
text-align: center;
font-size: 12px;
line-height: 15px;
text-transform: uppercase;
white-space: nowrap;
cursor: pointer;
color: #b2b2b2;
position: relative;
border-bottom: solid 2px transparent;
}
@media (max-width: 1000px) {
#artistNavigation li a {
padding: 1rem 0;
}
}
#artistNavigation li a,
#artistNavigation li a:visited,
#artistNavigation li a:active,
#artistNavigation li a:link {
color: #b2b2b2;
}
#artistNavigation li:hover > a,
#artistNavigation li:focus > a,
#artistNavigation li.active a {
color: #000;
border-bottom: solid 2px #25cbd3;
}
#artistNavigation li.pdf {
position: absolute;
right: 0;
top: 12px;
padding-right: 0;
text-transform: uppercase;
font-size: 12px;
color: #25cbd3;
padding-bottom: 16px;
border-bottom: solid 2px transparent;
}
#artistNavigation li.pdf:hover {
cursor: pointer;
color: #454545;
}
@media (max-width: 1000px) {
#artistNavigation:hover,
#artistNavigation:focus {
height: auto;
}
#artistNavigation li:active {
position: absolute;
top: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="art-nav" role="navigation">
<div class="artist-nav-box">
<ul id="artistNavigation">
<li class="menu-item active"><a href="#Commercial">Commercial</a>
</li>
<li class="menu-item"><a href="#Fashion">Fashion</a>
</li>
<li class="menu-item"><a href="#Lifestyle">Lifestyle</a>
</li>
<li class="menu-item"><a href="#Portaits">Portaits</a>
</li>
<li class="menu-item"><a href="#Places">Places</a>
</li>
<li class="menu-item"><a href="#Animals">Animals</a>
</li>
<li class="menu-item"><a href="#Great-White-Shark">Great White Shark</a>
</li>
<li class="menu-item"><a href="#Panoramics">Panoramics</a>
</li>
<li class="menu-item"><a href="#Masai">Masai</a>
</li>
</ul>
</div>
</nav>
我确信这是一件我很想念的事情,请原谅sass生成的CSS。
答案 0 :(得分:0)
您可以尝试使用addEventListener
来检测哈希更改。
window.addEventListener("hashchange",function(event){
if ($('.menu-item').hasClass('active')) {
$(".menu-item").removeClass("active");
$('[href$="' + window.location.hash + '"]').parent(this).addClass("active");
}
});
详细了解浏览器是否兼容here
答案 1 :(得分:0)
原来是一个CSS问题 - 我删除了:active状态的所有实例,并将其替换为.active类。 :活动状态会将项目rhough css从相对于绝对位置的位置移动 - 因此当单击处于关闭状态时,菜单项将会移动。完全停止2个半小时:/