简体   繁体   English

JavaScript应用CSS3渐变

[英]JavaScript apply CSS3 gradient

I am trying to apply a CSS3 gradient using JavaScript. 我正在尝试使用JavaScript应用CSS3渐变。 I have an array of random colours and I select one of these colours and then apply it to a gradient. 我有一组随机颜色,我选择了这些颜色之一,然后将其应用于渐变。 The problem is, that because the CSS3 background property doesn't have vendor prefixes, I can't seem to set them all. 问题是,因为CSS3背景属性没有供应商前缀,所以我似乎无法全部设置它们。 An example in CSS is this: CSS中的一个示例是这样的:

background: #3C60EF; /* Old browsers */
background: -webkit-linear-gradient(left top, #3C60EF, #133de5); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom right, #3C60EF, #133de5); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom right, #3C60EF, #133de5); /* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom right, #3C60EF, #133de5); /* Standard syntax (must be last) */

So, as you can see, I can't apply all them to the element. 因此,如您所见,我无法将所有元素都应用于元素。 I need to either figure out what browser I am using or find a way of adding all these. 我需要弄清楚我正在使用什么浏览器,或者找到一种添加所有这些浏览器的方法。

Any help would be appreciated. 任何帮助,将不胜感激。

Add them all to a class, and add the class to the element with JavaScript instead. 将它们全部添加到类中,然后使用JavaScript将类添加到元素中。

jQuery: jQuery的:

$('#my-element').addClass('mygradient');

Vanilla: 香草:

document.getElementById('my-element').className = 'mygradient';

You can create a new stylesheet dynamically. 您可以动态创建一个新的样式表。 Append the link to the new stylesheet to the document head - $("head").append("<style id='dynamicStylesheet'></style>"); 将指向新样式表的链接附加到文档头- $("head").append("<style id='dynamicStylesheet'></style>");

Then set the content of the stylesheet (create your gradient with your random colour) like this. 然后像这样设置样式表的内容(用您的随机颜色创建渐变)。

     var newGradientClassText = ".newGradientClass { "+
"background: #3C60EF; /* Old browsers */ " +
        "background: -webkit-linear-gradient(left top, " + randomColor + ", " + SecondrandomColor + "); /* For Safari 5.1 to 6.0 */ " + 
       " background: -o-linear-gradient(bottom right, " + randomColor + "," + SecondrandomColor + "); /* For Opera 11.1 to 12.0 */ " +
        "background: -moz-linear-gradient(bottom right, " + randomColor + ", " + SecondrandomColor + "); /* For Firefox 3.6 to 15 */ " +
       " background: linear-gradient(to bottom right, " + randomColor + ", " + SecondrandomColor + "); /* Standard syntax (must be last) */" +
"}";

Then set the text of the stylsheet $("#dynamicStylesheet").text(newGradientClassText); 然后设置样式表$("#dynamicStylesheet").text(newGradientClassText);

Then you can apply the class to the element $('#exampleElement').addClass(newGradientClass); 然后,您可以将该类应用于元素$('#exampleElement').addClass(newGradientClass);

I was implementing @tinkerbot 's solution but then I found another way. 我当时正在实现@tinkerbot的解决方案,但是后来我找到了另一种方法。 I created a class like this: 我创建了一个像这样的类:

var self = this;

var _baseColour = '#3C60EF';

var _shadeColour = function (hex, lum) {
    // validate hex string
    hex = String(hex).replace(/[^0-9a-f]/gi, '');
    if (hex.length < 6) {
        hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
    }
    lum = lum || 0;

    // convert to decimal and change luminosity
    var rgb = "#", c, i;
    for (i = 0; i < 3; i++) {
        c = parseInt(hex.substr(i * 2, 2), 16);
        c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
        rgb += ("00" + c).substr(c.length);
    }

    return rgb;
};

function _getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
};

function _getRandomColour(colour) {
    var percent = _getRandomInt(-100, 100) / 100;

    colour = colour || _baseColour;

    return _shadeColour(colour, percent);
};

self.generateCSS = function (colour) {
    var colour1 = _getRandomColour(colour),
        colour2 = _shadeColour(colour1, -.1); // Shade 10% darker

    var rule = 'background: ' + colour1 + '; /* Old browsers */ ' +
                'background: -webkit-linear-gradient(left top, ' + colour1 + ', ' + colour2 + '); /* For Safari 5.1 to 6.0 */' +
                'background: -o-linear-gradient(bottom right, ' + colour1 + ', ' + colour2 + '); /* For Opera 11.1 to 12.0 */' +
                'background: -moz-linear-gradient(bottom right, ' + colour1 + ', ' + colour2 + '); /* For Firefox 3.6 to 15 */' +
                'background: linear-gradient(to bottom right, ' + colour1 + ' , ' + colour2 + '); /* Standard syntax (must be last) */';

    return rule;
};

and in my loop through the elements I just did this: 在元素循环中,我只是这样做了:

var colour = attr.colouredTile;
var css = controller.generateCSS(colour);

element.setAttribute('style', css);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM