延迟jquery css更改 [英] delaying jquery css changes

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

问题描述

我写了一个代码,使一个div橙色的边框,然后一两秒更改为黑色,然而实际发生的是直接到黑色,任何帮助吗?感谢!



代码:

  $('#newMessage1') .css('border','2px solid#ffa800')。delay(100).css('border','2px solid#000000'); 
code>函数仅适用于添加到 fx 队列中的函数(函数 fadeIn slideDown )。 css 不是这些函数之一(尝试延迟任何其他非 - fx - 队列感知函数,它不会工作。)



docs


在1.4版本中添加了jQuery,.delay允许我们延迟
执行队列中的函数。它可以与标准效果队列或自定义队列一起使用
。只有
个队列中的后续事件被延迟;例如这不会
延迟没有使用
效果队列的.show()或.hide()的无参数形式。


jQuery delay 不能替代原生JS setTimeout 这种情况可能是走的路。例如,您可以执行此类操作(工作示例此处):

  $('#newMessage1')。css('border','2px solid#ffa800'); 

setTimeout(function(){
$(#newMessage1)。css('border','2px solid#000000');
},1000);


I have written a code which makes a border of a div orange then after a second or two changes it to black, however what is actually happening is that it goes straight to black, any help please? thanks!

Code:

$('#newMessage1').css('border','2px solid #ffa800').delay(100).css('border','2px solid #000000');

解决方案

The jQuery delay function only works on functions added to the fx queue (functions like fadeIn or slideDown). css is not one of these functions (try to delay any other non-fx-queue aware function and it won't work either).

From the docs:

Added to jQuery in version 1.4, the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.

jQuery delay is not a substitute for the native JS setTimeout, which in this case would probably be the way to go. You could, for example, do something like this (working example here):

$('#newMessage1').css('border','2px solid #ffa800');

setTimeout(function() { 
    $("#newMessage1").css('border','2px solid #000000'); 
}, 1000);

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

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