Jump to content

Changing Background Position


garyblackpool
 Share

Recommended Posts

hi,U have been trying to change the background position property but mozilla brower does not support style.backgroundPositionX but mozilla does not except that. So I have now changed my function to this but it does now work. Could someone help thanks.gary

var x = 10; //Starting Location - leftvar y = 10; //Starting Location - topvar dest_x = 400; //Ending Location - leftvar dest_y = 200; //Ending Location - topintervalX = Math.ceil(dest_x/dest_y);intervalY = Math.ceil(dest_y/dest_x)function moveimg() {if(x > dest_x) {x -= intervalX;} else if(x < dest_x) {x += intervalX}if(y > dest_y) {y -= intervalY;} else if(y < dest_y) {y += intervalY}// Just in case we're closer to the destination than the motion intervalif(Math.abs(dest_x - x) < intervalX) x = dest_x;if(Math.abs(dest_y - y) < intervalY) y = dest_y;if (x != dest_x || y != dest_y) {// Repeat the operationdocument.getElementById("landscape").style.backgroundPosition = 'x+'px' 'y+'px";window.setTimeout(moveimg,300);}}</script>

Link to comment
Share on other sites

document.getElementById("landscape").style.backgroundPosition = 'x+'px' 'y+'px";
is it these mismatching quotation marks?plus, would you need to have a comma inbetween the x and y coordinates? Or maybe parantheses around them? Edited by thescientist
Link to comment
Share on other sites

I thought i had it working but i dont (thats why this is edited). I way i got it to working firefox was:

document.getElementById("landscape").style.backgroundPosition = x+'px';document.getElementById("landscape").style.backgroundPosition = y+'px';

But that just adds the x and y destination together.Cheers

Edited by garyblack
Link to comment
Share on other sites

hi,I now have the function working usingdocument.getElementById("landscape").style.backgroundPosition = x+'%' +y+'%';but i have a new problem which is the function itself., if i move an image it will work fine but with the background it does not. If Y is set 100, and X set 200, when it gets to Y it just stops. I think the problem is with thisif (x != dest_x || y != dest_y) which i have changed to && and !=. Is it that or a problem with using background percentages?Thanks

Link to comment
Share on other sites

hi,I now have the function working usingdocument.getElementById("landscape").style.backgroundPosition = x+'%' +y+'%';but i have a new problem which is the function itself., if i move an image it will work fine but with the background it does not. If Y is set 100, and X set 200, when it gets to Y it just stops. I think the problem is with thisif (x != dest_x || y != dest_y) which i have changed to && and !=. Is it that or a problem with using background percentages?Thanks
You need to put a space between the first value and the second. And if you use percentages rather than pixels, it's not going to get where you want it to.document.getElementById("landscape").style.backgroundPosition = x+'px ' +y+'px'; Edited by Ingolme
Link to comment
Share on other sites

You need to put a space between the first value and the second. And if you use percentages rather than pixels, it's not going to get where you want it to.document.getElementById("landscape").style.backgroundPosition = x+'px ' +y+'px';
Oh whoop that works. I thought tried that. Thanks.Do you know what it would be just to effect the Y-axis,Thanks you very muchGary
Link to comment
Share on other sites

Oh whoop that works. I thought tried that. Thanks.Do you know what it would be just to effect the Y-axis,Thanks you very muchGary
You have to leave the X value constant, like this:document.getElementById("landscape").style.backgroundPosition = '5px ' +y+'px';But you really should keep track of both X and Y values before applying the backgroundPosition property. Maybe you want an extra variable constX that contains an unchanged value of X.
Link to comment
Share on other sites

You have to leave the X value constant, like this:document.getElementById("landscape").style.backgroundPosition = '5px ' +y+'px';But you really should keep track of both X and Y values before applying the backgroundPosition property. Maybe you want an extra variable constX that contains an unchanged value of X.
Oh cheers thanksgary
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...