2

Here is my problem. I want to update the background-image gradient of my site in real-time using JQuery, but I can't find a way to update multiple fallbacks for the same property. I need multiple fallbacks of course for cross-browser support. Here is what my class looks like:

#bg_gradient
{
   background-color: #dcbebe; 

   background-image: url(images/fallback-gradient.png); 

   background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#779eb0), to(#dcbebe));

   background-image: -webkit-linear-gradient(top, #779eb0, #dcbebe);

   background-image: -moz-linear-gradient(top, #779eb0, #dcbebe);

   background-image: -ms-linear-gradient(top, #779eb0, #dcbebe);

   background-image: -o-linear-gradient(top, #779eb0, #dcbebe);
}

And then my JQuery:

$('#bg_gradient').css('background-image','url(../gradient.png)');
$('#bg_gradient').css('background-image','-webkit-linear-gradient(top, #fff, #000)');
$('#bg_gradient').css('background-image','-moz-linear-gradient(top, #fff, #000)');
...

Of course, as expected using this method, the same background-image property is just being overwritten.

How can I update multiple fallback properties dynamically? Is it possible?

UPDATE: I forgot to mention that I am generating gradient transitions using an algorithm, and have hundreds of values to dynamically update.

3 Answers 3

1

By calling attr() I can update the style attribute directly, and pass in whatever I want, for example here is what I am doing:

var wk = '-webkit-linear-gradient(top, ' + color_one + ',' + color_two + ')'
$('#bg_gradient').attr('style', 'background-image:url(../image.png);background-image:' + wk);
Sign up to request clarification or add additional context in comments.

Comments

0

As you pointed out setting the same .css for a tag will just overwrite it. A workaround (painful) would be to write some conditional logic using the jQuery.browser flags and jQuery.browser.version.

Comments

0

Please update if you discover a solution; but for now, are you opposed to using two different classes and just toggle between them?

1 Comment

I am generating transitions between gradients dynamically, so I have hundreds of values...setting the attr() directly is working for me...

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.