Konos5
12-04-2008, 02:14 AM
Hello everyone!
I have some lines of code that actually produce a rolling text from one side of the screen to the other!
Here is the code:
<html>
<head>
<title>Animation1</title>
<style>
#para1 {position: absolute;
color : red;
font-size : 40pt}
</style>
<script language="javascript">
var x = 20, xshift = 1;
function move(elementId) {
x = x + xshift;
elementId.style.left = x;
if (x > 150) {
xshift = -1;
elementId.innerHTML = "Goodbye!";
elementId.style.color = "blue";
}
if (x < 20 ) {
xshift = 1;
elementId.innerHTML = "Hello!";
elementId.style.color = "red";
}
}
</script>
</head>
<body onLoad="intervalId = setInterval('move(para1)',10)"
onClick="clearInterval(intervalId)">
<p id="para1" >
Hello!
</p>
</body>
</html>
The problem comes in everytime the function is called!
I believe that instead of x incrementing by one everytime the function is called, it should permanently stay 21 and do nothing no matter what!
I mean, why javascript prefers to read x's previous value and doesn't just look on its global variables like it does on the "xshift" variable??
Any help would be really appreciated!
Thanks in advance!
I have some lines of code that actually produce a rolling text from one side of the screen to the other!
Here is the code:
<html>
<head>
<title>Animation1</title>
<style>
#para1 {position: absolute;
color : red;
font-size : 40pt}
</style>
<script language="javascript">
var x = 20, xshift = 1;
function move(elementId) {
x = x + xshift;
elementId.style.left = x;
if (x > 150) {
xshift = -1;
elementId.innerHTML = "Goodbye!";
elementId.style.color = "blue";
}
if (x < 20 ) {
xshift = 1;
elementId.innerHTML = "Hello!";
elementId.style.color = "red";
}
}
</script>
</head>
<body onLoad="intervalId = setInterval('move(para1)',10)"
onClick="clearInterval(intervalId)">
<p id="para1" >
Hello!
</p>
</body>
</html>
The problem comes in everytime the function is called!
I believe that instead of x incrementing by one everytime the function is called, it should permanently stay 21 and do nothing no matter what!
I mean, why javascript prefers to read x's previous value and doesn't just look on its global variables like it does on the "xshift" variable??
Any help would be really appreciated!
Thanks in advance!