views:

108

answers:

2

Hey, I'm making a simple user friendly css editor using Prototype. Anyway, I was wondering whether I could get the background-position x/y values on their own?

I wish I could do something like this but sadly it doesn't work: element.getStyle('background-position-x);

I guess this is because position comes in different forms like 'center' rather than just being generic.

+1  A: 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<style type="text/css">
<!--
body {
    background-image: url(00.jpg);
    background-repeat: no-repeat;
    background-position: top left;
}
-->
</style>
<script type="text/javascript">
function getStyle(obj,att){
for(var i=0;i<att.length;i++){
if(window.getComputedStyle){
obj[att[i]]=window.getComputedStyle(obj,null)[att[i]];
}
else if(obj.currentStyle){
obj[att[i]]=obj.currentStyle[att[i]];
}
}
}

onload=function(){
var obj = document.getElementsByTagName('body')[0];
var att=['backgroundImage','backgroundRepeat','backgroundPosition']
getStyle(obj,att)
for(var i=0;i<att.length;i++) {alert(obj[att[i]])}
}
</script>
</head>
<body>

</body>
</html>
Noam Smadja
+1  A: 

Hey, this seemed to work pretty well. Even when the position is set to 'center center' prototype will translate this into percentages! Very handy!

var temp = $('header').getStyle('background-position');
var split = temp.split(" ");
$('header_horizontal_position').value = split[0];
$('header_vertical_position').value = split[1];

Anyway, thanks for the help guys.

ro