我构建了一个下拉菜单,如果单击菜单本身或正文中的任何位置,该菜单将使用 SlideUp 事件:
$('html, .currentPage').click(function() {
$('.currentMenu').slideUp('fast', function() { myFunction(); });
});
我在 SlideUp 事件回调中还有一个函数。
但是,我的问题是,无论是否发生了 SlideUp 事件,都会在 html 或 .currentPage 单击上调用该函数。
我怎样才能做到这个功能(); 仅在 SlideUp 事件运行时发生?
最佳答案
$('html, .currentPage').click(function() {
var $currentMenu = $('.currentMenu');
if($currentMenu.is(':visible')) {
$currentMenu.slideUp('fast', function() { myFunction(); });
}
});
更多
$('html, .currentPage').click(function() {
var $currentMenu = $('.currentMenu');
if($currentMenu.is(':visible')) {
$currentMenu.slideUp('fast', myFunction);
}
});
您还可以选择使用 $currentMenu.css('display') != 'none'
代替 $currentMenu.is(':visible')
。
这些选项是根据 slideUp()
的实际行为建议的,官方文档http://api.jquery.com/slideUp/中有描述。 .
关于javascript - 单击函数在事件结束前触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526170/