<style>
* {
padding: 0;
margin: 0;
}
.box {
position: relative;
margin: 100px auto;
overflow: hidden;
width: 600px;
}
.img-box {
display: flex;
width: 3000px;
list-style: none;
}
.img-item {
width: 600px;
background-color: #02ffff;
height: 600px;
line-height: 600px;
text-align: center;
}
.prev, .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0,0,0,.5);
padding: 10px;
border-radius: 50%;
color: #ccc;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
.num-box {
display: flex;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 20px;
}
.num-item {
margin-right: 20px;
background-color: rgba(0,0,0,.3);
padding: 10px;
border-radius: 50%;
}
style>
head>
<body>
<div class="box">
<ul class="img-box">
<li class="img-item">1li>
<li class="img-item">2li>
<li class="img-item">3li>
<li class="img-item">4li>
<li class="img-item">5li>
ul>
<div class="prev">
<span><span>
div>
<div class="next">
<span>>span>
div>
<div class="num-box">
<div class="num-item" data-index="0">1div>
<div class="num-item" data-index="1">2div>
<div class="num-item" data-index="2">3div>
<div class="num-item" data-index="3">4div>
<div class="num-item" data-index="4">5div>
div>
div>
body>
<script>
const imgBox = document.querySelector('.img-box')
const nextBtn = document.querySelector('.next')
const prevBtn = document.querySelector('.prev')
const numBox = document.querySelector('.num-box')
const numList = document.querySelectorAll('.num-item')
let currentIdx = 0
let timer = null
numList[currentIdx].style.backgroundColor = '#ccc'
nextBtn.onclick = nextFun
prevBtn.onclick = prevFun
numBox.addEventListener('click', numClick)
imgBox.addEventListener('mouseenter', stopAutoPlay)
imgBox.addEventListener('mouseleave', autoPlay)
autoPlay();
function nextFun () {
imgBox.style.transition = '1s'
if (currentIdx === 4) {
imgBox.style.transition = '0s'
currentIdx = 0
} else {
++currentIdx
}
imgBox.style.transform = `translateX(${-600 * currentIdx}px)`
}
function prevFun () {
imgBox.style.transition = '1s'
if (currentIdx === 0) {
imgBox.style.transition = '0s'
currentIdx = 4
} else {
--currentIdx
}
imgBox.style.transform = `translateX(${-600 * currentIdx}px)`
}
function numClick(evt) {
imgBox.style.transition = '1s'
numList[currentIdx].style.backgroundColor = ''
const idx = evt.target.dataset.index
if (!idx) return
currentIdx = idx - 0
numList[idx].style.backgroundColor = '#ccc'
imgBox.style.transform = `translateX(${-600 * idx}px)`
}
function autoPlay() {
timer = setInterval(nextFun, 1000);
}
function stopAutoPlay() {
clearInterval(timer);
}
script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148