【发布时间】:2023-03-19 08:43:02
【问题描述】:
这是我想要做的:
-当窗口的宽度小于 700px 并且用户点击底部的灰色条时,一个红色的菜单会向上滑动并停留在那里。
-当窗口宽度大于700px时,用户点击灰条时不会发生任何事情。
我已将 resize() 事件绑定到浏览器窗口,因此如果用户在页面加载后调整窗口大小,则会检测到更改。
这是我的密码笔:http://codepen.io/Chiz/pen/xwrpWG (在您阅读以下内容之前,请不要点击!)
以下是产生奇怪问题的方法:
1) 在点击上面的 Codepen 链接之前,将窗口大小调整到 700px 以下,然后点击我上面的 codepen(如果不确定 700px 的宽度,让它变小)
2) 单击底部的灰色条。一个红色矩形应该向上滑动并停止。 再次单击。红色矩形向后滑动。反复点击,红色矩形每次上下滑动。这是正确的行为。
3) 现在,无需重新加载或刷新 codepen 即可调整浏览器宽度。 您可以使其更宽或更窄,只要调整窗口大小即可。 再次单击灰色条。出现错误。红色矩形向下滑动 MULTIPLE 次!
有时,您调整大小的次数越多,滑动的次数就越多! :-O
我该如何解决这个问题?
//bind click event to the gray bar on page's first load
showMenuIfWidthSmallerThanSevenHundred();
//detect window resizes
$(window).resize(function() {
showMenuIfWidthSmallerThanSevenHundred();
});
function showMenuIfWidthSmallerThanSevenHundred() {
if ($(window).innerWidth() <= 700) {
$("div").on("click", function() {
/* make menu fill the entire screen that is
not occupied by the gray bar */
var nMenuHeight = $(window).height() - $(this).height();
$(".menu").height(nMenuHeight);
/* position the menu so that the bottom of the menu
touches the top of the gray bar */
$(".menu").css("bottom", $(this).height());
//make menu slide upwards / downwards
$(".menu").slideToggle();
});
}
}
div {
width: 100%;
height: 53px;
background-color: gray;
position: absolute;
bottom: 0;
}
.menu {
width: 100%;
height: 100px;
background-color: #F08080;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
<div class="menu"></div>
【问题讨论】:
-
为什么要在 resize 事件处理程序中绑定 事件处理程序。因此它被多次调用
-
如果条件为真,您将在每次调整大小时绑定一个点击事件(可以调用一百次)...
-
这意味着我应该将点击事件部分放在 showMenuIfWidthSmallerThanSevenHundred() 函数之外?也许把它放在 document.ready() 函数中?谢了!