0

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.

3 Answers 3

1

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';
Sign up to request clarification or add additional context in comments.

4 Comments

that would be find, except that the colour is random :(
Random you say... hm... I guess you could add the class as inline CSS (style element). You can also set these prefixes with JavaScript if you want to... -webkit-linear-gradient becomes webkitLinearGradient I think.
Those statements aren't equivalent. The non-jQuery version would be document.getElementById('my-element').classList.add('mygradient').
Quentin, classList is nicer, but the browser support may not be good enough. It's more like addClass() though, correct.
1

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);

2 Comments

would this be ok to use with 100+ elements?
if you want all elements to have same gradient at once you could just give all elements a common base class, and use $('.ElementCommonClass').addClass(newGradientClass);
0

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);

1 Comment

sleak! might rethink my own system!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.