JavaScript在屏幕上滚动文本
我对JavaScript有点陌生 我编写了以下代码来在屏幕上滚动一些文本JavaScript在屏幕上滚动文本,javascript,Javascript,我对JavaScript有点陌生 我编写了以下代码来在屏幕上滚动一些文本 <html> <head> <script type="text/javascript"> function move(){ document.getElementById('id1').style.right=(document.getElementById('id1').style.right-10)
<html>
<head>
<script type="text/javascript">
function move(){
document.getElementById('id1').style.right=(document.getElementById('id1').style.right-10)+'px';
setTimeout(move,20);
}
</script>
</head>
<body>
<table>
<tr>
<td>
<div id='id1' class="divclass" style="right:1100px; position:absolute">
Here is text!
</div>
</td>
</tr>
<tr>
<td>
<input type="button" onclick="move()" value="move">
</td>
</tr>
</table>
</body>
</html>
函数move(){
document.getElementById('id1').style.right=(document.getElementById('id1').style.right-10)+'px';
设置超时(移动,20);
}
这里是文本!
但它不起作用。我不知道错误在哪里。右边的css样式实际上不是一个数字,而是一个后跟一个单位的数字。因此,(1100px-10)+‘px’实际上是‘Nanpx’。这是因为您试图从字符串值中减去。使用parseInt函数解析1100px中的1100个。然后做你的数学题。然后添加您的单位。我个人更喜欢在外部保留“x”值,例如:
<html>
<head>
<script type="text/javascript">
var intX = 1100;
function move(){
var objDiv = document.getElementById('id1');
if ( objDiv != null ) {
objDiv.style.left = (intX -= 3).toString() + 'px';
} //if
if ( intX > 0 ) { setTimeout(move,20); }
return true;
}
</script>
</head>
<body>
<div id="id1" class="divclass" style="left:1100px; position:absolute; white-space:nowrap;">Here is text!</div>
<input type="button" onclick="move()" value="move">
</body>
</html>
var intX=1100;
函数move(){
var objDiv=document.getElementById('id1');
if(objDiv!=null){
objDiv.style.left=(intX-=3.toString()+'px';
}//如果
如果(intX>0){setTimeout(move,20);}
返回true;
}
这里是文本!
请用复选标记将其标记为正确答案!别忘了注册!