随机延迟之后添加类 [英] Adding class after random delay

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

问题描述

我一直在努力的X量延迟后添加类DIV的。我想这样做的原因是为了让CSS动画做对我来说(不断淡入淡出的呼吸效应)。

I have been trying to add classes to DIV's after X-amount delay. The reason I want to do this is to let CSS do animations for me (constantly fading in and out for a 'breathing' effect).

这code的结果是,所有的DIV的同时开始,所以基本上它不补充一点,我想随机延迟 - >

The result of this code is that ALL the DIV's start at the same time, so basically it doesnt add the random delay that I want ->

$('.project').each(function() {
var number = 1000 + Math.floor(Math.random() * 6000);
$(this).delay(number).addClass('fading');});

这code(后约200屏幕上的信息)的作品:

This code (after about 200 on-screen messages) works:

$('.project').each(function() {
var number = 1000 + Math.floor(Math.random() * 6000);
alert(number);
$(this).delay(number).addClass('fading');});

帮助将不胜AP preciated:]谢谢

Help would be greatly appreciated :] Thanks!

推荐答案

延迟适用于动画和特效,它不影响 addClass ,你可以尝试的setTimeout 方法来代替。

delay works on animations and effects, it doesn't affect addClass, you could try a setTimeout method instead.

 var $this = $(this);
 setTimeout(function(){$this.addClass('fading');}, number);

这篇关于随机延迟之后添加类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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