Javascript background positioning.

GooS

Azrael
Reaction score
154
Hi, some help required.

I want the background position to be movable. so when I click the button I want the background poisiont to move 1px upwards and here's what I got.

(This is a shoretened version without all the rest that isn't about this function)
Code:
<html>
<head>
<style type="text/css">
#mainwindow
{ 
background-image: url('smiley.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 60px 100px;
height: 500px;
width: 500px;
}
</style>
<script language="javascript">
function move(where){
if(where == '1'){
currentY = document.getElementById('mainwindow').style.backgroundPositionY;
newY = parseInt(currentY); /*Added parseInt if by any chance the Y was returning with the suffix px */
newY = newY - 1; /*Setting to new Y value*/
document.getElementById('mainwindow').style.backgroundPositionY = newY; /* Applying new Y value to the background*/
}
}
</script>
</head>
<body>

<img src="smiley.gif" onClick="move('1')">
<div id="mainwindow"></div>
</body>
</html>

Now this does absolutely nothing, and I found very little about getting the backgroundPosition value, only a whole lot about setting it.

So I ask of you, possibilities?, have I done something very stupid :D?
 

TFlan

I could change this in my User CP.
Reaction score
64
Code:
currentY = document.getElementById('mainwindow').style.top;
document.getElementById('mainwindow').style.top = newY;
 

GooS

Azrael
Reaction score
154
Code:
currentY = document.getElementById('mainwindow').style.top;
document.getElementById('mainwindow').style.top = newY;

Doesn't move the background, it moves the div relative to whatever it's inside, so no that wont work, I need to move the background.
 

TFlan

I could change this in my User CP.
Reaction score
64
Code:
<div id="mainwindow" style="position:absolute;top:0px;left:0px;z-index:2"></div>

Will move freely.
 

GooS

Azrael
Reaction score
154
top and left are not background position attributes, they are div attributes, they will move the div not the background, I want to move the background within a static div, when i press a button I want the background to move 1px upwards NOT the div.

Getting what I mean with the diffrence, ill show you with pictures

background!.gif


Watch it closely, the background WITHIN the div mainwindow has moved but the div remains at the same place. So I can't use top and/or left attribute as they move a position: absolut; DIV not BACKGROUND.


Sry if I sound irritated, it's just to clarify.

/GooS
 

TFlan

I could change this in my User CP.
Reaction score
64
Ah my bad.

I do believe the backgroundPositionX/Y only works in IE?

But u forgot the px.

Code:
document.getElementById('mainwindow').style.backgroundPositionY = newY + "px";
 

GooS

Azrael
Reaction score
154
Ah my bad.

I do believe the backgroundPositionX/Y only works in IE?

But u forgot the px.

Code:

Code:
document.getElementById('mainwindow').style.backgroundPositionY = newY + "px";

Ahh only in IE, damn that sucks, ah well, the px part i knew about, i tried with and without just to see if the effect was any diffrent. well ill have to think of something else then.

Thx
 

TFlan

I could change this in my User CP.
Reaction score
64
Hmm, try this:

Code:
var ids = new Array();
	ids['mainwindow'] = new Array();
		ids['mainwindow']['curX'] = 0;
		ids['mainwindow']['curY'] = 0;
		
function moveImage(goX, goY, id) {
	ids[id]['curX'] += goX;
	ids[id]['curY'] += goY;
	document.getElementById(id).style.backgroundPosition = ids[id]['curX'] + "px " + ids[id]['curY'] + "px";
}

The reason I did the array, is so you can have multiple areas that you can move.

I know background-position is supported by both browsers, this should work. Give it a shot.

Code:
moveImage(<X-DIR>, <Y-DIR>, 'ID_NAME');

Example:
Code:
moveImage(1, 1, 'mainwindow');

Moves the background image of mainwindow 1px to the right and down 1px.


EDIT:::
Sorry, made a fatal flaw in the first version of the script. It's fine now.
 

GooS

Azrael
Reaction score
154
I think I love you, works like a charm, thx alot m8. +rep for fantastic help :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top