效果
代码
css 样式
* {
margin: 0;
padding: 0;
}
.main {
width: 800px;
margin: 20px auto;
}
ul li {
list-style: none;
cursor: pointer;
}
.pic {
width: 100%;
position: relative;
height: 520px;
}
.pic img {
width: 100%;
height: 520px;
}
.pic .indicators {
position: absolute;
bottom: 10px;
left: 45%;
}
.pic .indicators li {
width: 20px;
height: 20px;
background: rgba(0,0,0,0.55);
border-radius: 50%;
float: left;
margin: 5px;
cursor: pointer;
}
.pic .swiper {
width: 100%;
height: 100%;
overflow: hidden;
}
.pic ul li.active {
background: rgba(0,255,255,0.55);
}
a.btn {
width: 50px;
height: 50px;
line-height: 50px;
background: rgba(0,0,0,0.55);
color: #fff;
position: absolute;
text-align: center;
text-decoration: none;
font-size: 24px;
font-weight: 600;
top: 45%;
}
a.next {
right: 0;
}
html 结构
<div class="main">
<div class="pic">
<!-- 图-->
<div class="swiper">
<img src="" id="swiper">
</div>
<!--点-->
<ul class="indicators" id="indicators">
</ul>
<!-- 切换按钮-->
<a href="javascript:;" class="btn next" onclick="next()"> > </a>
<a href="javascript:;" class="btn prev" onclick="prev()"> < </a>
</div>
</div>
JavaScript
var images = ['https://www.tanshangbiao.cn/project/images/bg.jpg','https://www.tanshangbiao.cn/project/images/bg-1.jpg','https://www.tanshangbiao.cn/project/images/bg-2.jpg'];
var num = 0;
var oSwiper = document.getElementById('swiper');
var oIndicators = document.getElementById('indicators');
/** 初始化 */
function swiperInit() {
oSwiper.src = images[num];
for (let i=0; i<images.length; i++) {
let oLi = document.createElement('li');
oLi.addEventListener('click', clickInd);
oIndicators.appendChild(oLi);
};
oIndicators.getElementsByTagName('li')[num].className = 'active';
};
swiperInit();
/** 下一张*/
function next() {
num ++ ;
if (num > images.length - 1) {
num = 0 ;
};
oSwiper.src = images[num];
ind();
};
/** 上一张 */
function prev() {
num -- ;
if (num < 0) {
num = images.length - 1;
};
oSwiper.src = images[num];
ind();
};
/** 切点 */
function ind() {
let oIndItem = oIndicators.getElementsByTagName('li');
for (let i = 0; i<oIndItem.length; i++) {
oIndItem[i].className = '';
};
oIndItem[num].className = 'active';
};
function clickInd() {
let oIndItem = oIndicators.getElementsByTagName('li');
for (let i=0; i<oIndItem.length; i++) {
oIndItem[i].onclick = () => {
num = i;
ind();
oSwiper.src = images[i];
}
}
}