Posted By

bernhardb on 12/10/11


Tagged

jquery background position


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

Balamir
SpoodyGoon
spee66
BrockSamsom


Cross Browser CSS sprites (for hovers, clicks, active state change via background position change)


 / Published in: jQuery
 

URL: http://blog.appstack.io

Creating sprite buttons often had problems with different browsers. This is a solution to get background positions in each browser for further manipulation.

  1. function getBgPos(oObj) {
  2. var aPosXY = false;
  3.  
  4. try {
  5. aPosXY = oObj.css('background-position').replace(/px/g, '').split(' ');
  6. aPosXY[0] = parseInt(aPosXY[0]);
  7. aPosXY[1] = parseInt(aPosXY[1]);
  8. } catch(error) {
  9. var aPosX = oObj.css("background-position-x").split('px');
  10. var aPosY = oObj.css("background-position-y").split('px');
  11. aPosXY = new Array(parseInt(aPosX[0]), parseInt(aPosY[0]));
  12. }
  13. return aPosXY;
  14. }

Report this snippet  

You need to login to post a comment.