Dom 实现基础轮播图
<!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>
</head>
<style>
body {
display: flex;
}
div {
width: 500px;
height: 500px;
border: 1px solid red;
margin: auto;
position: relative;
}
img {
width: 500px;
}
ul {
position: absolute;
display: flex;
width: 100px;
justify-content: space-between;
bottom: 10px;
left: 140px;
}
li {
list-style: none;
width: 20px;
height: 20px;
background-color: white;
border-radius: 10px;
}
</style>
<body>
<input type="button" value="-" class="-">
<div>
<img src="./1.jpg" alt="">
<ul></ul>
</div>
<input type="button" value="+" class="+">
<script>
let arr = ['./1.jpg', './2.jpg', './3.png'];
let divEle = document.querySelector('div')
let imgEle = document.querySelector('img')
let bodyEle = document.querySelector('body')
divEle.appendChild(imgEle)
let i = 0, timer = null;
dot()
function run() {
i++
if (i > arr.length - 1) {
i = 0;
}
imgEle.setAttribute('src', arr[i])
foo()
}
function jian() {
i--;
if (i < 0) {
i = arr.length - 1;
}
imgEle.setAttribute('src', arr[i])
}
// 生成圆点
function dot() {
for (let i = 0; i < arr.length; i++) {
let liELe = document.createElement('li')
document.querySelector('ul').appendChild(liELe)
}
}
// 圆点变色
function foo() {
let LIEle = document.querySelectorAll('li')
for (let index = 0; index < LIEle.length; index++) { // 每一个小圆圈
if (i != index) {
LIEle[index].style.background = 'rgba(255,255,255,0.3)';
}
}
LIEle[i].style.background = '#fff';
}
let LiEle = document.querySelectorAll('li')
for (let index = 0; index < LiEle.length; index++) { // 每一个小圆圈
LiEle[index].onclick = function () {
i = index - 1;
run();
}
}
time = setInterval(run, 2000)
bodyEle.addEventListener('click', function (e) {
if (e.target.className == '-') {
jian()
} else if (e.target.className == '+') {
run()
}
}, false)
</script>
</body>
</html>

浙公网安备 33010602011771号