Jquery div strobe

And*_*ara 0 javascript jquery

我想有一个像jquery的频闪灯一样的div表演.每500毫秒基本上将背景颜色从黑色更改为白色.

我怎样才能做到这一点?

<div id="strobe"></div>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Mar*_*llo 5

这个setInterval()功能是你的朋友.

你不需要使用JQuery,你可以在纯粹的javascript中做到这一点 - 你就是这样做的:

var elem = document.getElementById("strobe");
var strobeBackground = function() {
   (elem.style.backgroundColor == "white") ? elem.style.backgroundColor = "black" : elem.style.backgroundColor = "white";
}

setInterval(strobeBackground, 500);
Run Code Online (Sandbox Code Playgroud)

但是,如果您想在jQuery中执行此操作,请访问:http://jsfiddle.net/Ru9xt/2/

HTML将如下所示:

 <div id="strobe" class="white">Hello</div>
Run Code Online (Sandbox Code Playgroud)

CSS看起来像这样:

.white {
    background-color: white;
}
.black {
    background-color: black;
}
Run Code Online (Sandbox Code Playgroud)

JS就在这里:

setInterval(function () {
        $("#strobe").toggleClass('black');
    }, 500);
Run Code Online (Sandbox Code Playgroud)