<!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>
<style type="text/css">
#showtext{position:absolute;top:0px;left:0px;}
</style>
<script type="text/javascript">
function setTimeMove()
{
setInterval("moveText()",10);
}
function moveText()
{
var switchdirection=false;
var showtext=document.getElementById("showtext");
var currentleft=showtext.offsetLeft;
var newlocation;
if (switchdirection==false)
{
newlocation=currentleft+2;
if (currentleft>=800)
{
switchdirection=true;
}
}
else
{
newlocation=currentleft-2;
if (currentleft<=0)
{
switchdirection=false;
}
}
showtext.style.left=newlocation+"px";
}
</script>
</head>
<body onload="setTimeMove()">
<div id="showtext">元素的内容</div>
</body>
</html>上面这段代码,期望出现的是div中的文字从左到右来回浮动,但是运行后却是从左至右单向浮动,不能返回。是哪里的错误呢?
解决方案 »
- 修改extjs4.1的example里的tree/check-tree例子,希望可以读取指定json节点下的数据,一直不成功,求教
- 问大家一个简单的JS定时器问题
- 求助:一个简单的javascript缓动问题
- 大家帮我看看啊。我实在找不除原因
- 动态生成列的问题
- 如何用正则表达式取<form></form>中的内容
- if...if以及if..else
- 用ScriptX打印的奇怪问题!
- 谁给我推荐一个好的在线编辑器,马上100分。
- javascript 函数参数为整数,函数体内的List却不能正常使用
- 超链接中的href可以加脚本void吗
- JS输出中文乱码!?(模板里正常,生成静态页面就乱码!)
{
newlocation=currentleft+2;
if (currentleft>=800)
{
switchdirection=true;
}
}这段永远成立执行,一直向右走
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#showtext{position:absolute;top:0px;left:0px;}
</style>
<script type="text/javascript">
function setTimeMove()
{
switchdirection=false;
setInterval("moveText()",10);
}
function moveText()
{
//var switchdirection=false;
var showtext=document.getElementById("showtext");
var currentleft=showtext.offsetLeft;
var newlocation;
if (switchdirection==false)
{
newlocation=currentleft+2;
if (currentleft>=800)
{
switchdirection=true;
}
}
else
{
newlocation=currentleft-2;
if (currentleft<=0)
{
switchdirection=false;
}
}
showtext.style.left=newlocation+"px";
}
</script>
</head>
<body onload="setTimeMove()">
<div id="showtext">元素的内容</div>
</body>
</html>
定义到函数外面