jQuery:单击时,防止鼠标输入事件
jQuery: On click, prevent mouseenter event
我有两个单独的动画正在发生,一个在mouseenter
click
上触发。问题是,当它们都被激活时,它会创建一个跳跃的动画。
你可以在这里明白我的意思:JSFiddle
如果激活click
事件,是否可以防止发生mouseenter
事件?
爪哇语
$('header h1').mouseenter(function() {
$('header:not(.open)').delay(500).animate({
'padding-bottom': '40px'
}, 150, function() {
//function complete
});
});
$('header h1').mouseleave(function() {
$('header:not(.open)').animate({
'padding-bottom': '20px'
}, 150, function() {
//function complete
});
});
$('header h1').click(function() {
if ($('header').hasClass('open')) {
$('header p').fadeOut(100);
$('header').removeClass('open').animate({
'padding-bottom': '20px'
}, 300, function() {
//animation complete
});
}
else {
$('header').addClass('open').animate({
'padding-bottom': '150px'
}, 300, function() {
$('header p').fadeIn();
});
}
});
这样做似乎更容易:
$('header').on({
mouseenter: function(e) {
if (!$(this).is('.open')) {
$(this).stop(true, true).delay(500).animate({'padding-bottom': '40px' }, 150, function() {
//function complete
});
}
},
mouseleave: function(e) {
if (!$(this).is('.open')) {
$(this).stop(true, true).animate({'padding-bottom': '20px'}, 150, function() {
//function complete
});
}
},
click: function() {
if ($(this).is('.open')) {
$(this).find('p').fadeOut(100).end().removeClass('open').stop(true, true).animate({'padding-bottom': '20px'}, 300, function() {
//animation complete
});
}else{
$(this).addClass('open').stop(true, true).animate({'padding-bottom': '150px'}, 300, function() {
$('p', this).fadeIn();
});
}
}
});
不,你不能。特别是因为这些事件是相互基于的:要单击元素,您需要用鼠标输入它:您无法停用单击时已经发生的enter
事件。而且您显然不应该在进入时停用未来的click
事件。
跳转动画问题的解决方案应该是stop()
方法,该方法结束对选定元素的运行动画。
延迟后检查open
类怎么样?
从
$('header h1').mouseenter(function() {
$('header:not(.open)').delay(500).animate({
'padding-bottom': '40px'
}, 150, function() {
//function complete
});
});
自
$('header h1').mouseenter(function() {
$.delay(500);
$('header:not(.open)').animate({
'padding-bottom': '40px'
}, 150, function() {
//function complete
});
});
显然,您可以根据自己的喜好微调延迟,但想法是这样的:
如果他们在 x
毫秒之前未单击,请增加填充。
听起来你需要.stop()
.
将此添加到click
事件的开头:
$('header').stop(true)
true 的第一个参数是 clearQueue
,这将清除动画队列。
看到这个 jsFiddle.
相关文章:
- 鼠标输入在 YUI 的 chrome 中不起作用
- 使用鼠标输入图表时禁用自动点悬停
- 鼠标输入和鼠标离开不起作用
- 鼠标输入鼠标离开没有按照我认为应该的方式工作
- 鼠标输入悬停分区显示/隐藏
- 清除间隔在鼠标输入上不起作用
- 鼠标输入事件上的jQuery延迟出现问题
- jQuery-鼠标输入闪烁图像
- 鼠标输入事件未触发
- 临时取消绑定鼠标输入从元素
- 悬停(),鼠标输入(),鼠标悬停()等来回跳跃
- 如何在鼠标输入时对字体大小进行动画处理
- 在单击时对元素进行动画处理,并在该元素和其他元素上单击鼠标输入/鼠标离开
- JS:鼠标输入的触摸等效项
- 防止“鼠标输入”回调函数,直到“突出显示”效果完成
- FF 鼠标离开/鼠标输入事件
- jQuery在鼠标输入时更改css,但仅输入内容而不是全部
- 如何使鼠标输入功能具有淡入淡出效果并保持在那里
- jQuery:单击时,防止鼠标输入事件
- 鼠标输入/鼠标离开产生闪烁效果