<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
width: 300px;
height: 40px;
margin:100px auto;
/* overflow: hidden; */
border: 1px solid rebeccapurple;
}
.box>ul {
margin: 0;
padding: 0;
}
.box>ul>li {
list-style-type: none;
width: 300px;
height: 40px;
line-height: 40px;
}
.count {
font-size: 24px;
}
</style>
</head>
<body>
<div class="box">
<ul id="box">
<li>158****546已购买1个月</li>
<li>158****546已购买2个月</li>
<li>158****546已购买3个月</li>
<li>158****546已购买4个月</li>
<li>158****546已购买5个月</li>
<li>158****546已购买1个月</li>
</ul>
</div>
<!-- <script src="./slider2.js"></script> -->
<script>
window.onload = function() { //自执行函数
sliderBox();
}
function sliderBox() {
var list = document.getElementById('box'), //获取 ul
newPosition = 0, //这里以ul作为位移目标,newPosition为ul每次的位置
offset = -40; //每次要上移的数值(先默认为上移)
if (!(this instanceof sliderBox)) {
return new sliderBox().init();
}
this.init = function() {
setInterval(animate, 1000);
};
function animate() {
newPosition += offset;
list.style.transition = "transform 0.6s";
list.style.transform = "translateY(" + newPosition + "px)";
if (newPosition < -160) {
setTimeout(function() {
newPosition = 0;
list.style.transition = "";
list.style.transform = "translateY(0)";
}, 600);
}
}
}
</script>
</body>
</html>