单击打开和关闭时切换下拉 div 的更好方法?
我目前正在使用以下代码切换下拉 div 菜单:
$(function() {
function toggleMenu(show) {
$(".dropdownInfo").toggle(show);
$('.userNavBar').css('background-color', show ? '#444' : '#333');
$('.upperBar').css('border-top-color', show ? '#ff556f' : '#333');
};
$('.userNavBar').click(function(e) {
toggleMenu(true);
e.stopPropagation();
});
$("body").click(function(e) {
toggleMenu(false);
});
});
是否有更快/更好/更有效的方法来切换 div?这看起来是一个非常大的代码块..
I'm currently toggling a dropdown div menu with the following code:
$(function() {
function toggleMenu(show) {
$(".dropdownInfo").toggle(show);
$('.userNavBar').css('background-color', show ? '#444' : '#333');
$('.upperBar').css('border-top-color', show ? '#ff556f' : '#333');
};
$('.userNavBar').click(function(e) {
toggleMenu(true);
e.stopPropagation();
});
$("body").click(function(e) {
toggleMenu(false);
});
});
Is there a faster/better/more efficient way to toggle the div? This seems like a very large chunk of code..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在样式表中使用更多 CSS,并减少内联 CSS。然后你就可以切换一个班级,并站在周围看起来...优雅。另一件事:您可以仅绑定一个
click
事件侦听器,并在事件处理程序中检查是否应该打开或关闭菜单。CSS
JavaScript
另外,请不要混合使用单引号和双引号。选择一个,并保持一致。
Use more CSS in a stylesheet, and less inline. Then you can just toggle a class, and stand around looking ...classy. One other thing: you can bind just a single
click
event listener, and in the event handler, check to see whether or not you should open or close the menu.CSS
JavaScript
Also, please don't mix single- and double-quotes. Pick one, and be consistent.
你可以尝试切换持续时间
you can try toggle with duration