jQuery延迟不起作用 [英] jQuery delay not working

查看:86
本文介绍了jQuery延迟不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('.transparent').removeClass('transparent').delay(2000).addClass('not_transparent').delay(4000)

我有一个半透明的div,然后想要将其切换为不透明.但是jQuery .delay();方法似乎在这里不起作用.我已经尝试过.fadeIn();相反,它会延迟工作,但不适用于更改类.

I have a div that is semi transparent and then want to switch it to not transparent. But the jQuery .delay(); method doesn't seem to work here. I've tried .fadeIn(); instead and that works with a delay but it doesn't work the changing classes.

推荐答案

.delay() 用于部分内容queue的内容,例如动画.一个简单的addClass不会排队.

.delay() is used for items that are part of a queue, like animations. A simple addClass is not queued.

您可以使用setTimeout.

var trans = $('.transparent').removeClass('transparent');
setTimeout(function() {
    trans.addClass('not_transparent');
}, 2000);

或者,您可以使用 .queue() 将未排队的项目添加到队列中,尽管我认为setTimeout会更好.

As an alternative, you could add the non-queued item to the queue using .queue(), though I think a setTimeout would be better.

$('.transparent').removeClass('transparent').delay(2000).queue(function(nxt) {
      $(this).addClass('not_transparent');
      nxt();
});

这篇关于jQuery延迟不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆