防止“鼠标输入”回调函数,直到“突出显示”效果完成
Prevent 'mouseenter' callback function untill 'highlight' effect is complete
我遇到了与以下代码片段类似的问题:
$('button').click(function(){
var p = $('<p>Lorem</p>');
$('body').prepend(p);
p.effect("highlight", {}, 3000);
});
$(document).on('mouseenter', 'p', function(){
$(this).css('background', 'blue');
});
$(document).on('mouseleave', 'p', function(){
$(this).css('background', 'white');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<button>Add</button>
正如您所注意到的,一旦第一个Lorem
附加到正文,mouseenter
就允许几分之一秒。结果,您会看到它的蓝色背景。
这会对用户体验产生负面影响。
如何防止这种情况发生?
这应该适合您:
$('button').click(function(){
var p = $('<p>Lorem</p>');
$('body').prepend(p);
p.effect("highlight", {}, 3000);
// give your new element a class
p.addClass('noHover');
// remove that class after the animation
setTimeout(function() { p.removeClass('noHover'); }, 3000);
// from my testing, you could probably stand to take the `3000` down to `2500`
// but I leave that to you
});
$(document).on('mouseenter', 'p', function(){
// check for the added class
if(!$(this).hasClass('noHover')){
//only allow the blue hover effect if the 'noHover' class has been removed
$(this).css('background', 'blue');
}
});
$(document).on('mouseleave', 'p', function(){
$(this).css('background', 'white');
});
#myBtn{ margin-top:22px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<button id="myBtn">Add</button>
相关文章:
- 检测页面上某个元素中选择(突出显示)或单击的内容
- 根据页面的位置突出显示文本中的字符
- 如何创建一个“;表单弹出框“;在chrome中右键单击时位于突出显示的单词上方
- 将js添加到wordpress中以突出显示css活动菜单
- 使用jquery突出显示类似的文本
- 在主页上时,我的整个导航都会突出显示
- 兰吉 |如何在 span 标签上使用单击事件删除突出显示的文本
- 突出显示列表的父项,但不突出显示所有子项
- 悬停时以矩形突出显示整个标签区域
- 寻找一个免费的交互式美国地图与突出显示的州
- 如何显示显示的html元素:在特定的媒体屏幕上没有
- yii2:依赖于Action的条件显示/显示字段
- Jquery/JS:通过样式显示显示选择框(无,块)
- 为什么选择显示显示值而不是标签
- 在HTML中显示显示缩进的日志的更好方法是什么
- 如何在javascript中显示显示var值的警报
- 如何在核心PHP上显示显示来自第三方网站的RSS提要的要求
- 使用PHP或JS显示/显示选中的复选框值
- Ag-grid -如何显示显示/隐藏列的selectAll选项企业设置过滤器
- 窗体显示显示在框中