开发者

How to find the background-position / background-image etc for multiple backgrounds using jQuery in Safari

开发者 https://www.devze.com 2023-03-21 12:26 出处:网络
Maybe I\'m missing something here. I\'m trying to get the current background info for an element (div) with multiple backgrounds, set using the longhand background-repeat, background-position and bac

Maybe I'm missing something here.

I'm trying to get the current background info for an element (div) with multiple backgrounds, set using the longhand background-repeat, background-position and background-image.

Using jQuery, I simply do var bPos = $('#element').css('background-position') to get the set of current positions, which will give me something like '0 0, 100px 100px, left bottom' in Firefox and Chromium (currently versions 5 and 12 respectively), but in Safari (version 5 on OS-X) it just returns the first value pair ('0 0'). These CSS values are set in an external stylesheet.

Does anyone have any idea as to why this is, and how to go about it to get the full set of value pairs in Safari (using the shorthand background property doesn't do it either)?

Edit:

Here is the CSS that is used in an external style sheet:

#page{
    background-repeat: repeat-x, repeat-x, repeat-x, repeat-x, repeat-x;
    background-image: url('../images/line.png'), url('../images/line.png'), url('../images/line.png'), url('../images/line.png'), url('../images/line.png');
   开发者_如何转开发 background-position: 0 798px, 0 653px, 0 125px, 0 88px, 0 78px;
}

Edit:

Ok, I made a test case for this on jsFiddle and have reported it as a jQuery bug since it seems to be reproducible behaviour (although I accept that it might be a browser implementation problem rather than actually a jQuery bug).

Still, if anyone has any ideas...

Edit:

The jQuery ticket has been closed - I take this to mean that it is a Safari bug - I'm not quite sure how to go further from here, but I've posted a bug report with Apple.

Edit:

As pointed out by Sindre Sorhus, this appears to have been corrected in Safari 5.1.


Since it's fixed in 5.1, I guess it's not a big deal anymore, but I wander if as a workaround you could initially set the background from js by

$('#page').attr('style', '...');

and then read it from there... kinda stupid and pain in the ass, yeah.


Use Jquery Background Position plugin. http://www.webinventif.fr/jquery-background-position-mettez-du-mouvement-dans-votre-background/


You can try to use different classes. That would be easiler.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号