为什么字符串在一定间隔内不移动?
Why does the string not moving within the certain interval?
我正在尝试编写一个简单的代码,每 20 毫秒移动 1 像素的直线(smt 就像一个自动收报机)。此外,使用 .offsetWidth 属性 希望它在到达浏览器 window 的边缘后消失。不幸的是,我卡住了,我不知道为什么它根本不动。请帮我找出错误,或者我走错了方向?谢谢!
<html>
<head>
<title>Blablabla</title>
<meta charset ="utf-8">
<!-- <script type="text/javascript" src="line.js"></script> -->
</head>
<body onload="interval()">
<div id="line">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</div>
<script type="text/javascript">
function interval() {
var line = setInterval(function(){document.getElementById("line").style.left = scroll.offsetWidth}, 20);
}
</script>
</body>
</html>
line
必须是 absolute
定位的元素,这样 left
css 属性 会影响
- 我假设您在将
interval
中的 left
属性 分配为 element.offsetWidth
does not return unit
with the value[Ref] 时没有提到 +px(unit)
看看下面的示例,它的注释应该可以帮助您理解、查看演示并尝试代码
//set starting offset
var leftOffset = 0;
//assign element to variable
var element = document.getElementById("line");
//css.left requires position.absolute
element.style.position = 'absolute';
//calculate width of element, in display.block it's 100%
element.style.display = 'inline-block';
//assign interval id to a variable for cancelation
var interval = setInterval(function(){
//increase offset by 1
leftOffset = leftOffset + 1;
//check if element is sticked to screen.right
if(leftOffset + element.offsetWidth < document.body.offsetWidth) {
//not sticked
//add pixels
element.style.left = leftOffset + 'px';
} else {
//sticked
//remove interval
clearInterval(interval);
//hide element
element.style.display = 'none';
}
}, 20);
<div id="line">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</div>
这是我的镜头:
<html>
<head>
<title>Blablabla</title>
<meta charset ="utf-8">
<!-- <script type="text/javascript" src="line.js"></script> -->
</head>
<body>
<div id="line">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</div>
<script type="text/javascript">
number = 5; //should be increased each time
setInterval(function(){var d = document.getElementById('line');
d.style.position = "absolute";
number++; //increase number
d.style.left = number; //move the div according to the number
}, 200); //notice that you can increase/decrease 200 according to your will
</script>
</body>
</html>
我正在尝试编写一个简单的代码,每 20 毫秒移动 1 像素的直线(smt 就像一个自动收报机)。此外,使用 .offsetWidth 属性 希望它在到达浏览器 window 的边缘后消失。不幸的是,我卡住了,我不知道为什么它根本不动。请帮我找出错误,或者我走错了方向?谢谢!
<html>
<head>
<title>Blablabla</title>
<meta charset ="utf-8">
<!-- <script type="text/javascript" src="line.js"></script> -->
</head>
<body onload="interval()">
<div id="line">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</div>
<script type="text/javascript">
function interval() {
var line = setInterval(function(){document.getElementById("line").style.left = scroll.offsetWidth}, 20);
}
</script>
</body>
</html>
line
必须是absolute
定位的元素,这样left
css 属性 会影响- 我假设您在将
interval
中的left
属性 分配为element.offsetWidth
does not returnunit
with the value[Ref] 时没有提到
+px(unit)
看看下面的示例,它的注释应该可以帮助您理解、查看演示并尝试代码
//set starting offset
var leftOffset = 0;
//assign element to variable
var element = document.getElementById("line");
//css.left requires position.absolute
element.style.position = 'absolute';
//calculate width of element, in display.block it's 100%
element.style.display = 'inline-block';
//assign interval id to a variable for cancelation
var interval = setInterval(function(){
//increase offset by 1
leftOffset = leftOffset + 1;
//check if element is sticked to screen.right
if(leftOffset + element.offsetWidth < document.body.offsetWidth) {
//not sticked
//add pixels
element.style.left = leftOffset + 'px';
} else {
//sticked
//remove interval
clearInterval(interval);
//hide element
element.style.display = 'none';
}
}, 20);
<div id="line">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</div>
这是我的镜头:
<html>
<head>
<title>Blablabla</title>
<meta charset ="utf-8">
<!-- <script type="text/javascript" src="line.js"></script> -->
</head>
<body>
<div id="line">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</div>
<script type="text/javascript">
number = 5; //should be increased each time
setInterval(function(){var d = document.getElementById('line');
d.style.position = "absolute";
number++; //increase number
d.style.left = number; //move the div according to the number
}, 200); //notice that you can increase/decrease 200 according to your will
</script>
</body>
</html>