好吧,这里是我的尝试,我会尽量注释代码尽可能:d 让我们假设你有4个格在HTML:
<div data-color-start=#000" data-color-end="#fff" id="wrapper">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
那么我们就需要jQuery的代码计算的div的数量,计算,然后将被添加到我们的div的背景颜色:
(你在潜水前,这可以优化)
// returns an array (so we can use easily in getColors)
function parseHex(hexCode) {
var returned;
if(hexCode.length === '4') {
returned[0] = parseInt(hexCode[1], 16); // transforsm in a normal number, lie you would use in rgb()
returned[1] = parseInt(hexCode[2], 16);
returned[2] = parseInt(hexCode[3], 16);
}
else {
//i suppose this is the case like #
returned[0] = parseInt(hexCode.substring(1,3), 16); //same thing like above but we substring'ed the hexCode
returned[1] = parseInt(hexCode.substring(3,5), 16);
returned[2] = parseInt(hexCode.substring(5), 16);
}
return returned; // so we return the array
}
// gets the colors based on the parameters, returns array
function getColors(startColor, endColor, number) {
var startArr = parseHex(startColor);
var endArr = parseHex(endColor);
var ratioOne, ratioTwo, ratioThree;
var i=0;
var colors; // the returned array
var temp; //used as temporary variable in for loop
var one, two, three; // this will store bool values
// let's say we have rgb(100,0,20) to rgb(10,200,10)
// we have for RED to decrease the value of our gradient
// for GREEN to increase it
// for BLUE to decrease it
one = startArr[0] < endArr[0]; //so true if we have to increase it
two = startArr[1] < endArr[1];
three = startArr[2] < endArr[2];
// now basically we will find the ratio of each R, G, B, and make the gradient based on it
ratioOne = Math.floor((Math.abs((startArr[0] - endArr[0]))+1)/(number-1));
ratioTwo = Math.floor((Math.abs((startArr[1] - endArr[1]))+1)/(number-1));
ratioThree = Math.floor((Math.abs((startArr[2] - endArr[2]))+1)/(number-1));
// store the first gradient
colors[0] = 'rgba(' + startArr[0] + ',' + startArr[1] + ',' + startArr[2] + ')';
for(i=1; i<(number-1); i+=1) {
colors[i] = 'rgba(';
if(one) {
temp = startArr[0] + ratioOne*i;
}
else {
temp = startArr[0] - ratioOne*i;
}
colors[i] += temp;
colors[i] += ',';
if(two) {
temp = startArr[1] + ratioTwo*i;
}
else {
temp = startArr[1] - ratioTwo*i;
}
// we modify the temp variable each time, so this is not a problem
colors[i] += temp;
colors[i] += ',';
if(three) {
temp = startArr[2] + ratioThree*i;
}
else {
temp = startArr[2] - ratioThree*i;
}
colors[i] += temp;
colors[i] += ')';
}
// i will be just one more than the i which our for loop ended
colors[i] = 'rgba(' + endArr[0] + ',' + endArr[1] + ',' + endArr[2] + ')';
return colors; // we now have all the colors from our gradient
}
var wrapper = $('#wrapper'); // store this, will be used later
var startColor = wrapper.data('color-start'); // remember the HTML ?
var endColor = wrapper.data('color-end');
var elements = wrapper.find('.child'); // now store the links from the header
var elLength = elements.length; // get the number of links in the menu
var colors = getColors(startColor, endColor, elLength);
for(var i=0; i<elLength; i+=1) {
elements[i].style('background-color', colors[i]);
}
因此,您在html中存储第一种颜色和最后一种颜色,然后js代码将内联样式添加到每个链接/ div /无论背景颜色(通过rgb颜色)。你向我们展示的梯度是通过数学函数完成的(例如f(x)= x;或者像这样的smth)。这就是为什么你可以用ratioOne
,ratioTwo
和ratioThree
我希望我没有犯任何错误,并希望你能明白这个想法。正如你已经知道的,这不能通过SASS完成,只有JS(因为你想写更少的代码,呃?)
调整OP以更好地问问题,因为SCSS不能访问DOM。 – bmoneruxui 2015-01-26 19:37:54
谢谢@rageandqq调整 – bmoneruxui 2015-01-26 19:44:05
为什么不在菜单容器中只有一个渐变,并且没有任何菜单项的背景? – 2015-01-26 20:20:14