If I assume that the background position will always be in the format:
0px 0px; or 10px 11px;
So, *number*px *number*px.
How can I get both the x position and the y position in Javascript?
If I assume that the background position will always be in the format:
0px 0px; or 10px 11px;
So, *number*px *number*px.
How can I get both the x position and the y position in Javascript?
Given the following HTML:
And CSS:
The following JavaScript works:
JS Fiddle demo.
The above approach does require a browser that supports
window.getComputedStyle()
(obviously), andtrim()
(though that could be replaced with areplace(/^\s+|\s+$/g,'')
).To get access to the units, and numbers, independently I'd suggest creating other objects within the overall object to hold those numbers, and units:
JS Fiddle demo.
As to what would happen if there was no
background-position
set? Well, why not try that, with the following CSS:In Chromium it seems that you get the default
background-position
of0% 0%
(and corresponding numbers and units): JS Fiddle demo.References:
String.parseFloat()
.String.replace()
.String.split()
.String.trim()
.window.getComputedStyle()
.