在 Javascript 中使用 setTimeout 移动文本不起作用
Moving text using setTimeout in Javascript not working
你好,我正在做一个教程,文本"Yoo"应该向右移动,但它没有。泰
<!DOCTYPE html>
<html>
<head>
<script>
var timer, x_pos=0, txt;
function _timer() {
txt = document.getElementById("txt");
x_pos = x_pos+1;
txt.style.left = x;
timer = setTimeout(_timer, 50);
}
</script>
</head>
<body onload="_timer()">
<h1 id="txt" style="position:absolute; left:0"> Yooo </h1>
</body>
</html>
变量
x 从未定义,您应该在px
中提供left
txt.style.left = x_pos + "px";
<!DOCTYPE html>
<html>
<head>
<script>
var timer, x_pos=0, txt;
function _timer() {
txt = document.getElementById("txt");
x_pos = x_pos+1;
txt.style.left = x_pos + "px";
timer = setTimeout(_timer, 50);
}
</script>
</head>
<body onload="_timer()">
<h1 id="txt" style="position:absolute; left:0"> Yooo </h1>
</body>
</html>
变量x从未定义。尝试:
txt.style.left = x_pos;
而不是
txt.style.left = x;
因此,您的最终代码是
<!DOCTYPE html>
<html>
<head>
<script>
var timer, x_pos=0, txt;
function _timer() {
txt = document.getElementById("txt");
x_pos = x_pos+1;
txt.style.left = x_pos;
timer = setTimeout(_timer, 50);
}
</script>
</head>
<body onload="_timer()">
<h1 id="txt" style="position:absolute; left:0"> Yooo </h1>
</body>
</html>
你忘记添加单位:)
txt.style.left = x+'px';
相关文章:
- 在不移动内部文本的情况下缩放元素的效果
- 在下拉导航中将文本向左移动
- 当文本不断变化时,如何避免在按钮内移动文本
- 使用jquery在正方形中移动文本
- 在 Javascript 中使用 setTimeout 移动文本不起作用
- JQuery 滑块移动文本
- 移动文本元素与弧拉斐尔JS
- 移动文本框动画
- jQuery移动文本/搜索更改事件的行为不同
- jquery移动文本区域调整大小的回调
- Dojo移动文本框
- 如何使用js在输入中移动文本
- 从右向左移动文本,但是从左开始
- 当一个单词被动画时移动文本
- HTML Div移动文本到下一行
- 如何在转换中移动文本标签
- jQuery通过动画在画布上移动文本
- KineticJS移动文本路径
- 在canvas元素中移动文本并使其居中
- 我想移动文本(重复)使用javascript