Javascript 如何在 jquery 中闪烁一个 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4450344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to blink a div in jquery?
提问by Mohan Ram
Possible Duplicate:
Blink an div with jquery
可能的重复:
用 jquery 闪烁一个 div
I need to know how to make blink of div in jquery?
我需要知道如何在 jquery 中使 div 闪烁?
回答by Gabriele Petrioli
html
html
<div class="blink">blinking text</div>
jquery
查询
function blink(selector){
$(selector).fadeOut('slow', function(){
$(this).fadeIn('slow', function(){
blink(this);
});
});
}
blink('.blink');
demo : http://www.jsfiddle.net/gaby/Vaysf/1/
演示:http: //www.jsfiddle.net/gaby/Vaysf/1/
Update(bringing the answer up-to-date)
更新(更新答案)
You do not need to use jQuery for such effects anymore. You can do it with just CSS (using CSS animations).
(compatibility table at http://caniuse.com/#feat=css-animation)
您不再需要使用 jQuery 来实现此类效果。您可以仅使用CSS(使用CSS 动画)来实现。
(http://caniuse.com/#feat=css-animation 上的兼容性表)
CSS(using the standard properties)
CSS(使用标准属性)
.blink{
animation:blink 700ms infinite alternate;
}
@keyframes blink {
from { opacity:1; }
to { opacity:0; }
};
Demo (with vendor prefixed properties) at http://jsfiddle.net/gaby/Vaysf/649/
http://jsfiddle.net/gaby/Vaysf/649/ 上的演示(带有供应商前缀的属性)
回答by David Tang
Assuming your div has id="blinkMe"
假设你的 div 有 id="blinkMe"
setInterval(function () {
var vis = $("#blinkMe").css("visibility");
vis = (!vis || vis == "visible") ? "hidden" : "visible";
$("#blinkMe").css("visibility", vis);
}, 500);
Note: used "visibility" and not "display" / .toggle()
since the latter will cause layout to shift around while the div is blinking.
注意:使用“可见性”而不是“显示”/.toggle()
因为后者会导致布局在 div 闪烁时移动。