【发布时间】:2018-11-25 11:00:15
【问题描述】:
我正在尝试在每个部分上获得随机渐变背景(4 种颜色之间)。 这是我的代码:
let colors = ["#ffd0d2","#fffdd0","#d0fffd","#d0d2ff"];
$(".main").children('section').each(function(){
let firstGradient = randomNumber(10,90);
$(this).css(
"background", "linear-gradient(141deg, "+colors[randomNumber(0,4)]+" "+firstGradient+"%, "+colors[randomNumber(0,4)]+" "+(100-firstGradient)+"%)"
);
});
function randomNumber(min,max){
return Math.floor((Math.random() * max) + min);
}
section{
display:block;
height:400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<section></section>
<section></section>
<section></section>
</div>
什么时候结果应该是这样的:
【问题讨论】:
-
@j08691 好吧,我同意,但在这种情况下并非如此。我的意思是,我可以得到 10% 和 90%,但梯度无论如何都应该起作用。 (在某些情况下,两种颜色之间没有“模糊”
-
你改变了你的问题,使我的答案有点无效......你不应该在接受答案后改变问题(或者即使在发布后如果它无效)
标签: javascript css background-color linear-gradients