如果只有一个延迟我可以使用setTimeout,我如何才能实现以下目标:
$(this).css().delay().css().delay().css();
编辑:
更改的CSS值是非数字的。
发布于 2011-03-02 08:16:41
jQuery的".delay()“接口就是关于”效果队列“的。它实际上是立即返回的。
要做到这一点,唯一的方法就是使用"setTimeout()“。
有一件事可能会让事情变得更愉快,那就是将CSS更改构建到一个数组中:
var cssChanges = [
{ delay: 500, css: { backgroundColor: "green", fontSize: "32px" }},
{ delay: 1000, css: { backgroundColor: "blue", textDecoration: "underline" }},
{ delay: 750, css: { position: "relative", marginLeft: "5px" }}
];然后,您可以使用单个例程在列表中遍历所需的延迟:
function doChanges(cssChanges) {
var index = 0;
function effectChanges() {
$('whatever').css(cssChanges[index].css);
if (++index < cssChanges.length) {
setTimeout(doChanges, cssChanges[index].delay);
}
}
setTimeout(effectChanges, cssChanges[0].delay);
}如果你愿意的话,你可以把它变成一个插件,不过如果你打算这样做的话,最好是弄清楚如何让你的插件与jQuery中现有的动画队列工具一起发挥作用。
发布于 2011-03-02 08:17:47
delay() only works with animation,IIRC。
这将为您工作:)
// Delay to CSS Properties
var cssChanges = [
{
delay: 500,
css: {
color: 'red'
}},
{
delay: 1500,
css: {
color: 'blue'
}},
{
delay: 500,
css: {
color: 'yellow'
}}
];
var element = $('div'),
lastDelay = 0;
$.each(cssChanges, function(i, options) {
lastDelay += parseInt(options.delay);
setTimeout(function() {
element.css(options.css);
}, lastDelay);
});更新
你也可以把它变成一个jQuery插件。
$.fn.delayCss = function(cssChanges) {
$(this).each(function() {
var element = $(this),
lastDelay = 0;
$.each(cssChanges, function(i, options) {
lastDelay += parseInt(options.delay);
setTimeout(function() {
element.css(options.css);
}, lastDelay);
});
});
}发布于 2011-03-02 08:19:26
css不是一种效果,它是立即发生的。如果您希望在交错的时间内更改多个css,那么setTimeout就是您想要的:
var $target = $("#target");
$target.css("color", "blue");
setTimeout(function() {
$target.css("color", "red");
setTimeout(function() {
$target.css("color", "green");
}, 500);
}, 500);如果你想用css来做一些你可以用animate来做的事情(例如,数值属性,而不是上面提到的颜色),那么如果你用animate代替css,你的代码将会很好地工作。
$("#target")
.animate({paddingLeft: "50px"})
.delay(500)
.animate({paddingLeft: "25px"})
.delay(500)
.animate({paddingLeft: "0px"});https://stackoverflow.com/questions/5161988
复制相似问题