现在它是只执行一次.我想让它相隔2秒在执行.
还有现在是文字要了尽头了就又返回到原来位置了.我想让文字到了尽头了就不让它返回到原来的位置.该怎么写脚本?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var xpos = 50;
var ypos = 100;
function move(){
var elem = document.getElementById("move");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
if(xpos == 300 && ypos == 200){
return true;
}
if(xpos < 300){
var desc = Math.ceil((300 - xpos)/15);
xpos = xpos + desc;
}
if(xpos > 300){
var desc = Math.ceil((xpos - 300)/15);
xpos = xpos - desc;
}
if(ypos < 200){
var desc = Math.ceil((200 - ypos)/10);
ypos = ypos + desc;
}
if(ypos > 200){
var desc = Math.ceil((ypos - 200)/10);
ypos = ypos - desc;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
setTimeout("move()",10);
}
window.onload = move;
</script>
<style type="text/css">
</style>
</head>
<body>
<p id="move">移动</p>
</body>
</html>
还有现在是文字要了尽头了就又返回到原来位置了.我想让文字到了尽头了就不让它返回到原来的位置.该怎么写脚本?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var xpos = 50;
var ypos = 100;
function move(){
var elem = document.getElementById("move");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
if(xpos == 300 && ypos == 200){
return true;
}
if(xpos < 300){
var desc = Math.ceil((300 - xpos)/15);
xpos = xpos + desc;
}
if(xpos > 300){
var desc = Math.ceil((xpos - 300)/15);
xpos = xpos - desc;
}
if(ypos < 200){
var desc = Math.ceil((200 - ypos)/10);
ypos = ypos + desc;
}
if(ypos > 200){
var desc = Math.ceil((ypos - 200)/10);
ypos = ypos - desc;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
setTimeout("move()",10);
}
window.onload = move;
</script>
<style type="text/css">
</style>
</head>
<body>
<p id="move">移动</p>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var xpos = 50;
var ypos = 100;
var elem;
window.onload = function(){
elem = document.getElementById("move");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
move();
};
function move(){ if(xpos == 300 && ypos == 200){
return true;
}
if(xpos < 300){
var desc = Math.ceil((300 - xpos)/15);
xpos = xpos + desc;
}
if(xpos > 300){
var desc = Math.ceil((xpos - 300)/15);
xpos = xpos - desc;
}
if(ypos < 200){
var desc = Math.ceil((200 - ypos)/10);
ypos = ypos + desc;
}
if(ypos > 200){
var desc = Math.ceil((ypos - 200)/10);
ypos = ypos - desc;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
setTimeout("move()",50);
}
</script>
<style type="text/css">
</style>
</head>
<body>
<p id="move">移动</p>
</body>
</html>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var xpos;
var ypos;
var timmer;
function init(){
xpos = 50;
ypos = 100;
var elem = document.getElementById("move");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
timmer = setInterval("move()",10);
}
function move(){
var elem = document.getElementById("move");
if(xpos == 300 && ypos == 200){
clearInterval(timmer);
setTimeout("init()",2000);
return true;
}
if(xpos < 300){
var desc = Math.ceil((300 - xpos)/15);
xpos = xpos + desc;
}
if(xpos > 300){
var desc = Math.ceil((xpos - 300)/15);
xpos = xpos - desc;
}
if(ypos < 200){
var desc = Math.ceil((200 - ypos)/10);
ypos = ypos + desc;
}
if(ypos > 200){
var desc = Math.ceil((ypos - 200)/10);
ypos = ypos - desc;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
}
window.onload = init;
</script>
<style type="text/css">
</style>
</head>
<body>
<p id="move">移动</p>
</body>
</html>
clearInterval(timmer);
setTimeout("init()",2000);
这两个为什么要放在if里
var xpos;
var ypos;
var timmer;
function init(){
//初始化各参数,使文字回归原始位置
xpos = 50;
ypos = 100;
var elem = document.getElementById("move");
elem.style.position = "absolute";
elem.style.left = "50px";
elem.style.top = "100px";
timmer = setInterval("move()",10); //开始每10毫秒执行一次move()
}
function move(){
var elem = document.getElementById("move");
if(xpos == 300 && ypos == 200){ //如果已到达目的地
clearInterval(timmer); //停止 “每10毫秒执行一次move()” 的timmer,即不再执行move()
setTimeout("init()",2000); //设置定时,2000毫秒后执行init()
return true;
}
if(xpos < 300){
var desc = Math.ceil((300 - xpos)/15);
xpos = xpos + desc;
}
if(xpos > 300){
var desc = Math.ceil((xpos - 300)/15);
xpos = xpos - desc;
}
if(ypos < 200){
var desc = Math.ceil((200 - ypos)/10);
ypos = ypos + desc;
}
if(ypos > 200){
var desc = Math.ceil((ypos - 200)/10);
ypos = ypos - desc;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
}
window.onload = init;