Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>
- Test: Bewegliches HTML
- </title>
- <meta name="author" content="Eiffel" />
- <script type="text/javascript">
- var moveAmountVertical;
- var moveAmountHorizontal;
- function init() {
- moving = document.getElementById("moving");
- moving.style.position = "absolute";
- moving.style.top = 50 + "px";
- moving.style.left = 50 + "px";
- moving.style.color = "#ff0000";
- moving.style.backgroundColor = "#0000ff";
- moving.style.fontWeight = "bold";
- moveDirection = "forward";
- }
- function changeColour() {
- moving.style.color = "#0000ff";
- moving.style.backgroundColor = "#ff0000";
- window.setTimeout("changeColour2()", 100);
- }
- function changeColour2() {
- moving.style.color = "#ff0000";
- moving.style.backgroundColor = "#0000ff";
- window.setTimeout("changeColour()", 100);
- }
- function move() {
- // An Wendepunkten Richtung umschalten
- if ((moving.style.left == "50px") && (moving.style.top == "50px")) {
- moveDirection = "forward";
- moveAmountVertical = 1;
- moveAmountHorizontal = 1;
- }
- if ((moving.style.left == "500px") && (moving.style.top == "300px")) {
- moveDirection = "backward";
- moveAmountVertical = 1;
- moveAmountHorizontal = 1;
- }
- // Bewegungsrichtung vorwärts
- if (moveDirection == "forward") {
- // horizontale Bewegung bis zum Punkt left=500px
- if (moving.style.left != "500px") {
- // neue Position festlegen
- moving.style.left = 50 + parseInt(moveAmountHorizontal) + "px";
- // moveAmountHorizontal um 1 erhöhen
- moveAmountHorizontal++;
- }
- // vertikale Bewegung bis zum Punkt top=300px
- else if (moving.style.top != "300px") {
- // neue Position festlegen
- moving.style.top = 50 + parseInt(moveAmountVertical) + "px";
- // moveAmountVertical um 1 erhöhen
- moveAmountVertical++;
- }
- }
- // Bewegungsrichtung rückwärts
- if (moveDirection == "backward") {
- // vertikale Bewegung bis zum Punkt top=50px
- if (moving.style.top != "50px") {
- // neue Position festlegen
- moving.style.top = 300 - parseInt(moveAmountVertical) + "px";
- // moveAmountVertical um 1 verringern
- moveAmountVertical++;
- }
- // horizontale Bewegung bis zum Punkt left=50px
- else if (moving.style.left != "50px") {
- // neue Position festlegen
- moving.style.left = 500 - parseInt(moveAmountHorizontal) + "px";
- // moveAmountHorizontal um 1 verringern
- moveAmountHorizontal++;
- }
- }
- // alle 10 Millisekunden wiederholen
- window.setTimeout("move()", 10);
- }
- </script>
- </head>
- <body onLoad="init()">
- <p id="moving">Test</p>
- <input id="btnColor" type="button" value="Farbe" onClick="changeColour()" />
- <input id="btnMove" type="button" value="Bewegen" onClick="move()" />
- <noscript>
- Bitte JavaScript einschalten
- </noscript>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement