我已经制作了一个有两种状态的菜单 - 大小大于或等于1025px,下拉子菜单通过悬停在它们上面打开,这很好,另一方面,尺寸小于1025px,子菜单打开点击它们。问题在于后一种情况,当以小于1025px的大小调整浏览器大小时,悬停功能仍然占优势,并且只有在刷新浏览器后,点击功能才会生效,一切正常,直到我再次调整浏览器大小1025px大小,然后再次调整大小小于1025px大小。 我将不胜感激。 谢谢, CP
$(function() {
var isMobile = false;
$(window).resize(function() {
if ($(window).width() >= 1025) {
isMobile = false;
$('nav li').hover(
function() {
$('ul', this).stop().slideToggle(150);
});
} else {
isMobile = true;
$("nav li").click(function() {
$('ul', this).stop().slideToggle(150);
});
};
});
$(window).resize(); // Trigger window resize to check on load
});
@import url(https://fonts.googleapis.com/css?family=Roboto:400,700,500);
/* main Styles */
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
background: #fafafa;
font-family: "Roboto", sans-serif;
font-size: 14px;
margin: 0;
}
a {
text-decoration: none;
}
.container {
width: 1000px;
margin: auto;
}
h1 {
text-align: center;
margin-top: 150px;
}
/* Navigation Styles */
nav {
background: #2ba0db;
}
nav ul {
font-size: 0;
margin: 0;
padding: 0;
display: block;
}
nav ul li {
display: block;
width: 100%;
}
nav ul li a {
color: #fff;
display: block;
font-size: 14px;
padding: 15px 14px;
transition: 0.08s linear;
}
nav ul li:hover {
background: #126d9b;
}
nav ul li ul {
display: none;
border-bottom: 5px solid #2ba0db;
}
nav ul li ul li {
border-top: 1px solid #444;
display: block;
}
nav ul li ul li:first-child {
border-top: none;
}
nav ul li ul li a {
background: #373737;
display: block;
padding: 10px 14px;
}
nav ul li ul li a:hover {
background: #126d9b;
}
nav .fa.fa-angle-down {
margin-left: 6px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div class="container">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li> <a href="#">Categories<i class='fa fa-angle-down'></i></a>
<ul>
<li><a href="#">Category One</a></li>
<li><a href="#">Category Two</a></li>
<li><a href="#">Category Three</a></li>
</ul>
</li>
<li> <a href="#">Services<i class='fa fa-angle-down'></i></a>
<ul>
<li><a href="#">Service One</a></li>
<li><a href="#">Service Two</a></li>
<li><a href="#">Service Three</a></li>
<li><a href="#">Service Four</a></li>
<li><a href="#">Service Five</a></li>
<li><a href="#">Service Six</a></li>
</ul>
</li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
</nav>
在浏览器调整大小时,您永远不会删除旧的事件处理程序,而是每次只添加更多事件处理程序。当其中一个事件发生时,它会执行所有处理程序。
不是在.resize()
处理程序中绑定事件处理程序,而是将它们绑定一次并让它们检查isMobile
变量以决定是否执行任何操作。
$(function() {
var isMobile = false;
$('nav li').hover(function() {
if (!isMobile) {
$('ul', this).stop().slideToggle(150);
}
}).click(function() {
if (isMobile) {
$('ul', this).stop().slideToggle(150);
}
});
$(window).resize(function() {
isMobile = $(window).width() < 1025;
});
$(window).resize(); // Trigger window resize to check on load
});
@import url(https://fonts.googleapis.com/css?family=Roboto:400,700,500);
/* main Styles */
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
background: #fafafa;
font-family: "Roboto", sans-serif;
font-size: 14px;
margin: 0;
}
a {
text-decoration: none;
}
.container {
width: 1000px;
margin: auto;
}
h1 {
text-align: center;
margin-top: 150px;
}
/* Navigation Styles */
nav {
background: #2ba0db;
}
nav ul {
font-size: 0;
margin: 0;
padding: 0;
display: block;
}
nav ul li {
display: block;
width: 100%;
}
nav ul li a {
color: #fff;
display: block;
font-size: 14px;
padding: 15px 14px;
transition: 0.08s linear;
}
nav ul li:hover {
background: #126d9b;
}
nav ul li ul {
display: none;
border-bottom: 5px solid #2ba0db;
}
nav ul li ul li {
border-top: 1px solid #444;
display: block;
}
nav ul li ul li:first-child {
border-top: none;
}
nav ul li ul li a {
background: #373737;
display: block;
padding: 10px 14px;
}
nav ul li ul li a:hover {
background: #126d9b;
}
nav .fa.fa-angle-down {
margin-left: 6px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div class="container">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li> <a href="#">Categories<i class='fa fa-angle-down'></i></a>
<ul>
<li><a href="#">Category One</a></li>
<li><a href="#">Category Two</a></li>
<li><a href="#">Category Three</a></li>
</ul>
</li>
<li> <a href="#">Services<i class='fa fa-angle-down'></i></a>
<ul>
<li><a href="#">Service One</a></li>
<li><a href="#">Service Two</a></li>
<li><a href="#">Service Three</a></li>
<li><a href="#">Service Four</a></li>
<li><a href="#">Service Five</a></li>
<li><a href="#">Service Six</a></li>
</ul>
</li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
</nav>