r3plica
r3plica

Reputation: 13367

JavaScript apply CSS3 gradient

I am trying to apply a CSS3 gradient using JavaScript. 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. An example in CSS is this:

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.

Upvotes: 0

Views: 83

Answers (3)

r3plica
r3plica

Reputation: 13367

I was implementing @tinkerbot 's solution but then I found another way. 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);

Upvotes: 0

user2375017
user2375017

Reputation:

You can create a new stylesheet dynamically. Append the link to the new stylesheet to the document head - $("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);

Then you can apply the class to the element $('#exampleElement').addClass(newGradientClass);

Upvotes: 1

Tony Gustafsson
Tony Gustafsson

Reputation: 888

Add them all to a class, and add the class to the element with JavaScript instead.

jQuery:

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

Vanilla:

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

Upvotes: 1

Related Questions