Flying text sinks to the bottom, does not rise to the top?

A

aler45dcom

I write a flying text script,but it sinks at the bottom and it stays
there forever.

How to change it so that it flies in the webpage?

<html>
<head>
<title>Flying text</title>
<script type="text/javascript">
<!--
var dom, finalx=700, finaly=540;
function initText() {
dom=document.getElementById("theText").style;
var x=dom.left;
var y=dom.top;
x=x.match(/\d+/);
y=y.match(/\d+/);
moveText(x,y);
}
function moveText(x,y) {
if(x!=finalx)
if (x>finalx) x--;
else x++;
if(y!=finaly)
if (y>finaly) y--;
else y++;
if( (x!=finalx) || (y!=finaly)) {
dom.left=x+"px";
dom.top=y+"px";
}
setTimeout("moveText(" + x + "," + y + ")", 1);
}
//-->
</script>
</head>
<body onload="initText()" >
<p>
<span id="theText" style="position: absolute; left: 100px; top:
100px; font: bold 20pt 'Times Roman'; color: blue;">
Hello!
</span>
</p>
</body>
</html>
 
S

Stephen Chalmers

I write a flying text script,but it sinks at the bottom and it stays
there forever.

How to change it so that it flies in the webpage?

You need to store the current x and y direction, increment the co-ordinates
accordingly and change directions when defined limits are reached.

<html>
<head>
<title>Flying text</title>
<script type="text/javascript">

var dom, finalY=screen.availHeight||screen.height,
finalX=screen.availWidth||screen.width, xDir=1, yDir=1;
finalY=Math.round(finalY*0.7);

function initText()
{
if( document.getElementById &&
(dom=document.getElementById("theText").style) )
{
var x=dom.left;
var y=dom.top;
x=x.match(/\d/);
y=y.match(/\d/);
moveText(x,y);
}
}

function moveText(x,y)
{

if( (xDir==1 && x>=finalX) || xDir==-1 && x<=0)
xDir=-xDir;

x+=xDir;

if( (yDir==1 && y>=finalY) || yDir==-1 && y<=0)
yDir=-yDir;

y+=yDir;

dom.left=x+"px";
dom.top=y+"px";

setTimeout("moveText(" + x + "," + y + ")", 50);
}

</script>
</head>

<body onload="initText()" >

<div id="theText" style="position: absolute; left: 100px; top:
100px; font: bold 20pt 'Times Roman'; color: blue;">Hello!</div>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top