<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0%;
padding: 0%;
list-style: none;
}
.max {
width: 1400px;
height: 515px;
margin: auto;
position: relative;
overflow: hidden;
}
ul {
display: flex;
width: 8400px;
/* width: 7000px; */
position: absolute;
left: 0px;
/* height: 730px; */
}
ul li {
width: 1400px;
}
img {
width: 100%;
}
.min {
position: absolute;
bottom: 8px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.max .min>div {
width: 10px;
height: 10px;
border-radius: 50%;
border: 1px solid rgb(19, 16, 16);
margin: 0px 10px;
}
.blu {
background-color: blue;
}
</style>
</head>
<body>
<div class="max">
<ul>
<li><img src="./26623103960ec0e55846c7.jpg" alt=""></li>
<li><img src="./46565237261d50f4269719.jpg" alt=""></li>
<li><img src="./51894802361d50eeb12316.jpg" alt=""></li>
<li><img src="./51912058060a32c55f0d23.jpg" alt=""></li>
<li><img src="./91990417062a143b9b51d3.jpg" alt=""></li>
<li><img src="./26623103960ec0e55846c7.jpg" alt=""></li>
</ul>
<div class="min">
<div class="blu"></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<button>上</button>
<button>下</button>
<div></div>
</body>
</html>
<script>
var btn = document.querySelectorAll('button');
var uls = document.querySelector('ul');
var max = document.querySelector('.max');
var divs = document.querySelectorAll('.min>div');
var index = 0;
var b = true;
function scroll(flag) {
b = false;
if (index == uls.children.length - 1) {
index = 0;
uls.style.left = 0 + 'px';
}
if (flag) {
index++;
} else {
index--;
}
if (index < 0) {
index = 4;
uls.style.left = -7000 + 'px';
}
for (var i = 0; i < divs.length; i++) {
divs[i].className = '';
}
if (index == 5) {
divs[0].className = 'blu';
} else {
divs[index].className = 'blu';
}
console.log(index);
animation(uls, -index * uls.children[0].offsetWidth, flag);
}
// var flag = false;
function animation(obj, target, flag) {
clearInterval()
obj.time = setInterval(function () {
var cur = obj.offsetLeft;//获取当前元素的off
//判断滚动方向
if (flag) {
cur -= 25;
} else {
cur += 25;
}
if (flag ? cur > target : cur < target) {
obj.style.left = cur + 'px';
} else {
b = true;
clearInterval(obj.time);
obj.style.left = target + 'px';
}
}, 10)
}
btn[1].addEventListener('click', function () {
if (b) {
scroll(true);
}
});
btn[0].addEventListener('click', function () {
if (b) {
scroll(false);
}
})
// uls.time = setInterval(function () {
// if (b) {
// scroll(true);
// }
// }, 2000)
function zidong() {
if (b) {
scroll(true);
}
}
//鼠标以上清除定时器
var test = setInterval(zidong, 2000)
max.addEventListener('mousemove', function () {
clearInterval(test);
this.style.cursor = 'pointer';
})
max.addEventListener('mouseout', function () {
test = setInterval(zidong, 2000);
})
//小圆点
for (var j = 0; j < divs.length; j++) {
divs[j].setAttribute('qq', j);
divs[j].addEventListener('click', function () {
for (var k = 0; k < divs.length; k++) {
divs[k].className = '';
}
var qq = this.getAttribute('qq');
console.log(qq);
index = qq - 1;
if (b) {
scroll(true);
}
this.className = 'blu';
})
}
</script>
js轮播原生
最新推荐文章于 2024-11-04 14:35:19 发布