JS實(shí)現(xiàn)小米輪播圖
更新時間:2020年09月21日 09:13:02 作者:green hand編程
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)小米輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
原生JS仿小米輪播圖(最新版易懂),供大家參考,具體內(nèi)容如下
本次內(nèi)容主要可實(shí)現(xiàn)的效果:
- 自動輪播
- 可點(diǎn)擊上一張 下一張進(jìn)行圖片的切換
- 點(diǎn)擊右下方小圓點(diǎn)進(jìn)行圖片切換
代碼部分:
H5:
<div class="wrap">
<ul class="list">
<li class="item active"><img src="img/001.jpg" alt=""></li>
<li class="item"><img src="img/002.jpg"" alt=""></li>
<li class="item"><img src="img/001.jpg" alt=""></li>
<li class="item"><img src="img/002.jpg" alt=""></li>
<li class="item"><img src="img/001.jpg" alt=""></li>
</ul>
<!-- 小圓點(diǎn) -->
<ul class="pointList">
<li class="point active" data-index="0"></li>
<li class="point" data-index="1"></li>
<li class="point"data-index="2"></li>
<li class="point"data-index="3"></li>
<li class="point"data-index="4"></li>
</ul>
<button type="button" class="btn" id="goPre"><</button>
<button type="button" class="btn" id="goNext">></button>
</div>
CSS部分:
<style>
.wrap {
width: 800px;
height: 400px;
position: relative;
}
.list {
width: 800px;
height: 400px;
list-style: none;
position: relative;
padding-left: 0px;
}
.item {
position: absolute;
width: 100%;
height: 100%;
color: white;
font-size: 50px;
opacity: 0.6;
transform: all .8s;
}
.item img{
width: 800px;
height: 400px;
}
.btn {
width: 50px;
height: 100px;
position: absolute;
top: 150px;
z-index: 100;
}
#goPre {
left: 0px;
}
#goNext {
right: 0px;
}
.item.active {
opacity: 1;
z-index: 10;
}
.pointList{
padding-left: 0;
list-style: none;
position: absolute;
right: 20px;
bottom: 20px;
z-index: 1000;
}
.point{
width: 10px;
height: 10px;
background-color: rgba(0,0,0,0.4);
border-radius: 50%;
float: left;
margin-right: 16px;
border-style: solid;
border-width: 2px;
border-color: rgba(255,255,255, 0.6);
cursor: pointer;
}
.point.active{
background-color: rgba(255,255,255,0.4);
}
</style>
JS部分
<script>
//獲取節(jié)點(diǎn)
var items = document.getElementsByClassName('item')//圖片
var goPreBtn = document.getElementById('goPre');
var goNextBtn = document.getElementById('goNext');
//獲取點(diǎn)
var points=document.getElementsByClassName('point');
var time=0;//定時器圖片跳轉(zhuǎn)參數(shù)
var index = 0; //表示第幾張圖片在展示
//可以展示第幾個點(diǎn)
var clearActive=function(){
for(var i=0;i<items.length;i++){
items[i].className='item';
points[i].className='point';
}
}
var goIndex=function(){
clearActive();
items[index].className='item active';
points[index].className='point active';
}
var goNext=function(){
if(index<4){
index++;
}else{
index=0;
}
goIndex();
}
var goPre=function(){
if(index==0){
index=4;
}else{
index--;
}
goIndex();
}
//點(diǎn)擊下一張切換圖片
goNextBtn.addEventListener('click' ,function(){
goNext();
time=0;
})
//點(diǎn)擊上一張進(jìn)行切換圖片
goPreBtn.addEventListener('click' ,function(){
goPre();
time=0;
})
for(var i=0;i<points.length;i++){
points[i].addEventListener('click',function(){
var pointIndex=this.getAttribute('data-index');
index=pointIndex;
goIndex();
time=0;
})
}
//自動輪播定時器
setInterval(function(){
time++;
if(time==20){
goNext();
time=0;
}
},100)
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript File API實(shí)現(xiàn)文件上傳預(yù)覽
這篇文章主要為大家介紹了JavaScript File API實(shí)現(xiàn)文件上傳預(yù)覽,F(xiàn)ile API將極大地方便 Web 端的文件上傳等操作,本文將介紹 File API的概況,并用兩個實(shí)例展示File API的應(yīng)用,感興趣的小伙伴們可以參考一下2016-02-02
刪除javascript所創(chuàng)建子節(jié)點(diǎn)的方法
這篇文章主要介紹了刪除javascript所創(chuàng)建子節(jié)點(diǎn)的方法,涉及javascript針對頁面節(jié)點(diǎn)元素的操作技巧,需要的朋友可以參考下2015-05-05
JavaScript實(shí)現(xiàn)猜數(shù)字小功能
本文主要介紹了JavaScript實(shí)現(xiàn)猜數(shù)字小功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
JavaScript ES5標(biāo)準(zhǔn)中新增的Array方法
這篇文章主要介紹了JavaScript ES5標(biāo)準(zhǔn)中新增的Array方法 的相關(guān)資料,需要的朋友可以參考下2016-06-06
JS實(shí)現(xiàn)指定區(qū)域的全屏顯示功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)指定區(qū)域的全屏顯示功能,涉及javascript事件響應(yīng)及頁面元素屬性動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04

