无限循环跑马灯
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
margin: 0 auto;
width: 1000px;
border: 1px solid red;
overflow:hidden;
padding-left: 20px;
position: relative;
height: 50px;
}
.text {
white-space:nowrap;
position: absolute;
left: 200px;
}
.pwd {
white-space:nowrap;
position: absolute;
left: 1020px;
}
</style>
</head>
<body>
<div class="box">
<span class="text">
文字表达力是一个人的显性综合能力,也是外在的现实竞争力。在现代社会,每个人从事的职业或许不同,但自身的能力和水平都程度不同地需要用文字表达出来。
</span>
<span class="pwd">
文字表达力是一个人的显性综合能力,也是外在的现实竞争力。在现代社会,每个人从事的职业或许不同,但自身的能力和水平都程度不同地需要用文字表达出来。
</span>
</div>
<script>
var box = document.getElementsByClassName('box')[0];
var text = document.getElementsByClassName('text')[0];
var pwd = document.getElementsByClassName('pwd')[0];
var boxWidth = document.getElementsByClassName('box')[0].offsetWidth;
var textWidth = document.getElementsByClassName('text')[0].offsetWidth;
var pwdWidth = document.getElementsByClassName('pwd')[0].offsetWidth;
window.onload=function checkScrollLeft(){
if(boxWidth >= textWidth){ return false}
toScrollLeft()
}
function toScrollLeft(){
let a = text.offsetLeft + textWidth;
if(a > 0){
text.style.left = text.offsetLeft - 1 + 'px';
setTimeout('toScrollLeft()', 18);
}
else{
text.style.left = 1020+ "px";
toScrollLeftPwd();
}
}
function toScrollLeftPwd(){
let a = pwd.offsetLeft + pwdWidth;
if(a > 0){
pwd.style.left = pwd.offsetLeft - 1 + 'px';
setTimeout('toScrollLeftPwd()', 18);
}
else{
pwd.style.left = 1020 + "px";
toScrollLeft();
}
}
</script>
</body>
</html>
Talk is cheap,show me the code