1

Lets say I have the following CSS:

._gradient {
    background-image: linear-gradient(top, #6AF 16px, #0032C8 24px);
    background-image: -o-linear-gradient(top, #6AF 16px, #0032C8 24px);
    background-image: -moz-linear-gradient(top, #6AF 16px, #0032C8 24px);
    background-image: -webkit-linear-gradient(top, #6AF 16px, #0032C8 24px);
    background-image: -ms-linear-gradient(top, #6AF 16px, #0032C8 24px);
    background-image: -webkit-gradient(linear,left top,left bottom,color-stop(16px, #6AF),color-stop(24px, #0032C8));
}

Is there any way simple with Javascript or jQuery to read the values for the gradient position / direction / color?

obviously I could using Regular Expressions... the problem would lie in 'background-image' value returning a different value dependent on which browser was accessing the site: jsFiddle example

Basically now this led me to try just with the linear-gradient() value (aiming to do browser detect and apply the CSS with jQuery) but this returned the value 'none': jsFiddle example 2

Bare in mind my actual aim is to get these values dynamically with the script, not set them (although I'm willing for the solution to require me to do this).

EDIT FOR CLARIFICATION I am looking for a way to read the values in a gradient, even if gradients are not supported. using $.cssHooks is not the answer due to the fact that if the property is nou supported, it returns the value 'none' which has no benefit what so ever.

4
  • 2
    see here: stackoverflow.com/questions/5929707/… Commented Mar 13, 2012 at 15:13
  • Doesn't work due to issue in example 2, like I said, I am trying to get the color , position and direction. If the data is available to cssHooks, it will also be returned by $(elem).css('background-image') however you can't 'get' unsupported CSS values and the having to regex for the different types of gradient supported in different browsers seems a little extreme. Commented Mar 13, 2012 at 15:29
  • i guess that's not possible without using regexp, and YES, you will have to check if the value is 'none', jump to the next possible type.. here a possible work-around that may work: <div class="_gradient" linear-gradient="blah|blah"></div>​ then read the attr from the element $('._gradient').html($('._gradient').attr('linear-gradient'));​ or even better use an attribute for every value you need, top, rgba, etc.. i hope that helps ! Commented Mar 13, 2012 at 16:12
  • 1
    it'd have to be data-linear-gradient="" to be HTML5 compliant but I was thinking along the same lines, then using jQuery to apply the gradient and PHP to generate the image file if no gradient was supported... Can't wait for the plain linear-gradient to be supported (in 50 years time...) Commented Mar 13, 2012 at 16:24

2 Answers 2

0

Does this roughly do what you want? http://jsfiddle.net/2wQed/2/

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately no, if an unsupported value is present it displays the name of the background image but ideally I need the position, color and direction values.
Ah ok. I know you don't want to do it that way but I can't see a simpler way without regex'ing the value in that case unfortunately...
Problem there is that in browsers that support no type of CSS gradient, it returns the value 'none' ... I think a solution might be to name the gradient file something like FFFFFF_10_CCCCCC_40_FF0000_100 but that's a complete nightmare to contemplate!
although not the correct answer, this is the closest anybody got to a correct answer and the actual answer to the question is "you can't do it"
0

You may want to look at this...

http://api.jquery.com/jQuery.cssHooks/

found in here, not sure if it works, but maybe it worths tryin

1 Comment

That was already suggested in the comments and again, it doesn't work for what I am trying to do.

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.