Jquery/CSS:如何在mouseenter事件上应用延迟1秒的addClass
Jquery/CSS: How to apply addClass with a delay of 1 sec on mouseenter event.
这是我的addClass代码,它正在工作。但当我尝试使用延迟或setTimeout函数时,它不起作用。
我不想在CSS中使用webkit属性。
如果有人早些时候遇到这个问题,请告知。
$(document).ready(function(){
$('#table_Id tr td').mouseenter(function(){
$(this).parent('tr').addClass('blueBgColor');
});
});
使用setTimeout
,它会在指定的延迟后调用函数或执行代码段。
$(document).ready(function() {
$('#table_Id tr td').mouseenter(function() {
var self = this; //Cache this in a variable
setTimeout(function() {
$(self).parent('tr').addClass('blueBgColor');
}, 1000); //Here delay in milliseconds
});
});
EDIT:bind
的使用
$(document).ready(function() {
$('#table_Id tr td').mouseenter(function() {
setTimeout(function() {
$(this).parent('tr').addClass('blueBgColor');
}.bind(this), 1000); //Here delay in milliseconds
});
});
嘿,我为你的问题创建了一个jsfiddle。。。
代码:-
$(document).ready(function() {
$('#table_Id tr td').mouseenter(function() {
var td = $(this);
setTimeout(function() {
td.parent('tr').addClass('blueBgColor');
}, 1000);
});
});
工作示例:-
http://jsfiddle.net/L7S37/14/
感谢
$(document).ready(function(){
var myTimeout;
$('#table_Id tr td').on("mouseenter", function(){
var that = $(this);
myTimeout = setTimeout(function () {
that.parent('tr').addClass('blueBgColor');
}, 1000);
});
// Also if you need, you can do:
$('#table_Id tr td').on("mouseleave", function(){
clearTimeout(myTimeout);
});
});
OR:
$(document).ready(function(){
var myTimeout;
// use event.target
$('#table_Id tr td').on("mouseenter", function (e) {
myTimeout = setTimeout(function () {
$(e.target).parent('tr').addClass('blueBgColor');
}, 1000);
});
});
如果不存在toggle类,您可能需要使用它,否则在每个鼠标输入上,该类都会被反复添加:
$('#table_Id tr td').mouseenter(function(){
$td = $(this);
setTimeout(function(){$td.toggleClass('addedClass',true)},'1000');
});
js fiddle示例
试试这个
$('#table_Id tr td').mouseenter(function(){
var $this = $(this);
setTimeout(function () {
$this.parent('tr').addClass('blueBgColor');
}, 1000);
});
试试这个
$('#table_Id tr td').off('mouseenter').on('mouseenter', function() {
var element = this;
setTimeout(function() {
$(element).parent('tr').addClass('blueBgColor');
}, 1000); /*delay for 1 sec*/
});
相关文章:
- 如何减少在移动网络应用程序上播放声音的延迟
- 在javascript for循环的迭代之间应用延迟
- 在菜单下拉条上应用2秒延迟
- 余烬.为真正的大应用程序延迟加载模板和 JavaScript
- 延迟加载角度应用程序不会加载指令
- 应用脚本而不加载延迟
- 在 ng 模糊上应用延迟
- 如何避免Spotify应用程序中同步web服务调用的延迟
- 在jQuery的每次迭代之间应用延迟's.each()方法
- 从javascript监控web应用程序的UI延迟
- JavaScript/CSS:向DOM添加元素和应用其CSS规则之间的延迟
- GWT.延迟启动应用程序
- 如何在jquerymobile中应用延迟加载
- angularjs将加载应用程序延迟到指定条件
- 如何在谷歌应用引擎中返回延迟任务的数据
- 使用webkit运行iOS应用程序时延迟选择
- 将延迟应用于动画jquery
- jQuery.css():属性中是否存在固有延迟's对元素的应用程序
- 如何在bxslider中的每个幻灯片之间应用不同的延迟
- Jquery/CSS:如何在mouseenter事件上应用延迟1秒的addClass