<DIV style="position:absolute; left:0px; top:0px;width:200px;height:100px">This is a container for your contents</DIV>
Passing your mouse over this link Position 0,0 will show a container 200 pixels wide by 100 pixels high in the top left corner of this page at the X,Y co-ordinates 0,0.
In order to position the box elsewhere on the page enter a number for the left and/or top attribute.
<DIV style="position:absolute; left:200px; top:210px;width:200px;height:100px">This box is now positioned at 200px,210px</DIV>Note:
If you have scrolled down the page do not mistake the top of the window for the top of the page.
You now have the information needed to place a layer anywhere on or off your page.
Each time you click on the link Move Layer you call function moveDiv() which will move the layer 100 pixels to the right until it disappears off screen.
We need to be able to identify this DIV so we give it an ID. The ID name can be any of your choosing.
The reason we need the identity is because the script needs to know which DIV it is going to work with.
|
<script type="text/javascript"> <!-- function moveDiv1(){ divElement=document.getElementById('div1') currentPosition=divElement.offsetLeft currentPosition+=100 divElement.style.left=currentPosition+"px" } // --> </script> |
Assign the element to a variable. Get elements current position. Increase the value of currentPosition by 100. Reposition the element using the new value of currentPosition. |
|
<DIV id="div1" style="position:absolute;left:10px; top:120px;width:200px;border:1px solid black">Layer 1</DIV> <a href="#null" onclick="moveDiv1()">Move Layer 1</a><br> |
|
To automate the above you would include the setTimeout method
Now you only have to click on the link Auto Move Layer once and function moveDiv() is called every 100 milliseconds by the setTimeout method.
|
<script type="text/javascript"> <!-- function autoMove1(){ divElement=document.getElementById('div2') currentPosition=divElement.offsetLeft currentPosition+=50 divElement.style.left=currentPosition+"px" timer=setTimeout("autoMove1()",100) pageWidth=document.body.clientWidth elementWidth=divElement.offsetWidth if(currentPosition>=pageWidth-elementWidth-50){ clearTimeout(timer) divElement.style.left=pageWidth-elementWidth } } // --> </script> |
Assign the element to a variable. Get elements current position. Increase the value of currentPosition by 50. Reposition the element using the new value of currentPosition. Call function every 100 milliseconds Get width of page Get width of element If current position is greater than page width minus layer width Stop running the function |
|
<DIV id="div2" style="position:absolute; left:10px; top:150px;width:200px;border:1px solid black">Auto Move Layer</DIV> <a href="#null" onclick="autoMove1()">Auto Move Layer</a><br> |
|
An addition to the Auto Move Layer script is the method clearTimeout() to stop the layer once it has reached the edge of the page, otherwise the layer would carry on and on and on and on and .........
Click on the link Auto Move Layer 2, when the layer reaches the edge of the page it is repositioned back to zero
|
<script type="text/javascript"> <!-- function autoMove2(){ divElement=document.getElementById("div3") currentPosition=divElement.offsetLeft currentPosition+=50 divElement.style.left=currentPosition+"px" timer=setTimeout("autoMove2()",100) pageWidth=document.body.clientWidth elementWidth=divElement.offsetWidth if(currentPosition>=pageWidth-elementWidth){ clearTimeout(timer) divElement.style.left=10+"px" } } // --> </script> |
Assign the element to a variable. Get elements current position. Increase the value of currentPosition by 50. Reposition the element using the new value of currentPosition. Call function every 100 milliseconds Get width of page Get width of element If current position is greater than page width minus layer width Stop running the function Position the element at 0 pixels |
|
<DIV id="div3" style="position:absolute; left:10px; top:180px;width:200px;border:1px solid black">Auto Move Layer 2</DIV> <a href="#null" onclick="autoMove2()">Auto Move Layer 2</a> |
|
An addition to the Auto Move Layer 2 script is the method to return the layer back to its original position once it has reached the edge of the page, otherwise the layer would carry on and on and on and on and .........
This final example shows how to move the layer to the right and have it scroll back to its start position.
|
<script type="text/javascript"> <!-- function autoMove3(){ divElement=document.getElementById("div4") currentPosition=divElement.offsetLeft currentPosition+=50 divElement.style.left=currentPosition+"px" timer=setTimeout("autoMove3()",100) pageWidth=document.body.clientWidth elementWidth=divElement.offsetWidth if(currentPosition>=pageWidth-elementWidth){ clearTimeout(timer) autoMove3b() } } function autoMove3b(){ currentPosition=divElement.offsetLeft currentPosition-=50 divElement.style.left=currentPosition+"px" leftTimer=setTimeout("autoMove3b()",100) if(currentPosition<10){ divElement.style.left=10+"px" clearTimeout(leftTimer) } } // --> </script> |
Assign the element to a variable. Get elements current position. Increase value of currentPosition by 50. Reposition the element using the new value of currentPosition. Call function autoMove3 every 100 milliseconds If current position is greater than page width minus layer width Stop timer Call function autoMove3b Get elements current position. Decrease value of currentPosition by 50. Reposition the element using the new value of currentPosition. Call function autoMove3b every 100 milliseconds If currentPosition is less than 10 Position element at 10 Stop timer |
|
<DIV id="div4" style="position:absolute; left:10px; top:210px;width:200px;border:1px solid black">Auto Move Layer 3</DIV> <a href="#null" onclick="autoMove3()">Auto Move Layer 3</a> |
|
The above script uses a function to move the layer right and a second function to move the layer left.
The second function is not really necessary but for this example it makes it easier to explain what is required from the script and hopefully easier to understand how the script works.
The shorter script below does exactly the same by negating the step value when conditions are met.
<script type="text/javascript">
<!--
step=50 // pixel movement
function autoMove4(){
divElement=document.getElementById("div5")
currentPosition=divElement.offsetLeft
currentPosition+=step
divElement.style.left=currentPosition+"px"
timer=setTimeout("autoMove4()",100)
pageWidth=document.body.clientWidth
elementWidth=divElement.offsetWidth
if(currentPosition>=pageWidth-elementWidth){
step= -step // negate step value
}
if(currentPosition<10){ // if currentPosition is less than 10
clearTimeout(timer) // stop timer
divElement.style.left=10+"px" // reset to 10
step= -step
}
}
// -->
</script>
<DIV id="div5" style="position:absolute; left:10px; top:250px;width:200px;border:1px solid black">Auto Move Layer 4</DIV>
<a href="#null" onclick="autoMove4()">Auto Move Layer 4</a>
So the three items you need are:
Note;
The active examples in this page use a relative position to keep the layers within the flow of this document.
The copy & paste examples use an absolute position
Absolute positioning takes the element out of the natural flow of the document and could be shown over the top of other content within the page.
The following example demonstrates the use of the position property's, relative, absolute, and static values.
While the top and left values of the Container remain the same, (top: 100px; left:100px;) the value of the position property determines the point of origin from which those values are based.
<style>
.example{position:static; top:100px; left:100px}
</style>
<DIV>A PARAGRAPH.</DIV>
<div class=example>Container.</SPAN>