get background-image position (x y and unit)


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. //<![CDATA[
  3.  
  4. function getBackgroundPos(obj) {
  5. var pos = obj.css("background-position");
  6. if (pos == 'undefined' || pos == null) {
  7. pos = [obj.css("background-position-x"),obj.css("background-position-y")];//i hate IE!!
  8. } else {
  9. pos = pos.split(" ");
  10. }
  11. return {
  12. x: parseFloat(pos[0]),
  13. xUnit: pos[0].replace(/[0-9-.]/g, ""),
  14. y: parseFloat(pos[1]),
  15. yUnit: pos[1].replace(/[0-9-.]/g, "")
  16. };
  17. }
  18.  
  19. $(document).ready(function() {
  20. // Test:
  21. // getBackgroundPos($("body")).x
  22. // getBackgroundPos($("body")).xUnit
  23. // getBackgroundPos($("body")).y
  24. // getBackgroundPos($("body")).yUnit
  25. //
  26. });
  27.  
  28. //]]>
  29. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.