想就此寻求帮助。我在没有使用 html 的情况下制作了 4 个具有 2 种背景颜色(红色和绿色)的盒子。
这是我的 jsfiddle:https://jsfiddle.net/2bk1Ldg2/3/
我怎样才能找到一种方法来识别现有的背景颜色 css 以在 5 秒内交替更改它(红色到绿色和绿色到红色)。
不知道搜索此类问题的最佳查询是什么。您精彩的回答对我的学习帮助很大。非常感谢!
$(document).ready(function() {
function() {
$( ".div1, .div3" ).animate({backgroundColor: 'red'
}, 5000)};
});
你应该使用 setInterval()
重复您的代码几秒钟。因为你在 style
属性中添加了 background
元素,你可以使用 HTMLElement.style
获取名称背景颜色。
setInterval(function(){
$("div").each(function(){
this.style.background = this.style.background == "green" ? "red" : "green";
});
}, 5000);
setInterval(function(){
$("div").each(function(){
this.style.background = this.style.background == "green" ? "red" : "green";
});
}, 1000);
div { height: 50px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="background: green"></div>
<div style="background: red"></div>
<div style="background: green"></div>
<div style="background: red"></div>