原生JS實(shí)現(xiàn)無(wú)縫輪播圖片
更新時(shí)間:2020年06月24日 16:54:58 作者:weixin_46310510
這篇文章主要為大家詳細(xì)介紹了原生JS實(shí)現(xiàn)無(wú)縫輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JS實(shí)現(xiàn)無(wú)縫輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
運(yùn)動(dòng)插件
function animove(obj,distance,speed,callback) { //調(diào)用的變量 目標(biāo)距離 速度 回調(diào)函數(shù)
clearInterval(obj.timer);
obj.timer = setInterval(function () {
let step = (distance - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (obj.offsetLeft == distance) {
clearInterval(obj.timer);
if (callback) {
callback();
}
}
obj.style.left = obj.offsetLeft + step + 'px';
},speed)
}
CSS代碼
* {
margin: 0;
padding: 0;
}
ul,li {
list-style: none;
}
.box {
width: 1226px;
height: 460px;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.pic-box {
width: 4904px;
position: absolute;
}
.pic-box > li {
float: left;
}
.point {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
.point > li {
float: left;
width: 8px;
height: 8px;
border: 1px solid blueviolet;
margin: 0 4px;
border-radius: 50%;
}
.point > .active {
background-color: orange;
}
.left-btn,
.right-btn {
width: 50px;
height: 40px;
background-color: rgba(0,0,0,.5);
text-align: center;
line-height: 40px;
font-size: 20px;
color: white;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.left-btn {
left: 0;
}
.right-btn {
right: 0;
}
HTML代碼
<div class="box"> <ul class="pic-box"> <li> <img src="lb.webp" alt=""> </li> <li> <img src="lb2.webp" alt=""> </li> <li> <img src="pic3.jpg" alt=""> </li> </ul> <ul class="point"> </ul> <div class="left-btn"><</div> <div class="right-btn">></div> </div> <script src="運(yùn)動(dòng)插件.js"></script>
js代碼
let picbox = document.querySelector('.pic-box');
let pic = document.querySelectorAll('.pic-box > li'); //由于獲取的不是動(dòng)態(tài)的 所以之后的克隆 并不會(huì)使這個(gè)變量發(fā)生改變
let point = document.querySelector('.point');
let leftbtn = document.querySelector('.left-btn');
let rightbtn = document.querySelector('.right-btn');
let carouselindex = 0;
//通過(guò)for循環(huán) 生成小圓點(diǎn) 并將圓點(diǎn)添加到ul里
for (let i = 0; i < pic.length; i ++) {
let pointli = document.createElement('li');
pointli.classList.add(i);
point.appendChild(pointli);
}
//給第一個(gè)小圓點(diǎn)默認(rèn)添加active
point.children[0].classList.add('active');
//克隆第一個(gè)圖片 深度
let kelon = picbox.children[0].cloneNode(true);
picbox.appendChild(kelon); //將圖片添加到最后位置
//獲取所以圓點(diǎn)
let pointli = document.getElementsByClassName('point')[0].getElementsByTagName('li');
//輪播方法
function carousel(index) {
let distance = (-index * pic[0].offsetWidth); //計(jì)算行走的距離 圖片的index值乘以圖片的大小
animove(picbox,distance,10); //調(diào)用運(yùn)動(dòng)函數(shù)
for (let i = 0; i < pointli.length; i ++) { //for循環(huán)移除每個(gè)小點(diǎn)的選中狀態(tài)
pointli[i].classList.remove('active');
}
if (index != pic.length) { //如果不等于pic的長(zhǎng)度 就執(zhí)行
pointli[index].classList.add('active');
} else { //如果索引值為3 說(shuō)明此時(shí)圖片為克隆的圖 而圓點(diǎn)的最大索引值為2 將第一個(gè)圓點(diǎn)設(shè)置為active即可正常顯示圓點(diǎn)狀態(tài)
point.children[0].classList.add('active');
}
}
Array.prototype.forEach.call(pointli,function (item,index) { //給每個(gè)圓點(diǎn)添加點(diǎn)擊事件
item.addEventListener('click',function () {
carouselindex = index; //將點(diǎn)擊的索引值賦值給輪播索引全局變量
carousel(carouselindex);
})
});
rightbtn.addEventListener('click',function () { //右邊點(diǎn)擊事件
carouselindex ++; //每次點(diǎn)擊全局輪播索引增加
if (carouselindex > pic.length) { //如果索引大于圖片數(shù)量 由于數(shù)量大小獲取的是靜態(tài)的 所以長(zhǎng)度不會(huì)因?yàn)榭寺∽兓兓?
picbox.style.left = "0px"; //如果大于索引說(shuō)明此時(shí)要離開(kāi)克隆的那張圖 此時(shí)迅速將left值設(shè)置為0
carouselindex = 1; //然后將索引設(shè)置為1
}
carousel(carouselindex); //這時(shí)候就在left為0的位置 過(guò)渡到索引1的位置 實(shí)現(xiàn)無(wú)縫輪播的效果
});
leftbtn.addEventListener('click',function () { //左邊點(diǎn)擊事件
carouselindex --; //減減
if (carouselindex < 0) { //如果索引值小于0
carouselindex = 2; //將索引值設(shè)置為2
picbox.style.left = "-3678px"; //將位置迅速變換為第四張圖的位置(克隆的圖)
}
carousel(carouselindex); //由克隆的圖過(guò)渡到索引為2的圖(第三張圖)
})
精彩專(zhuān)題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
通過(guò)判斷JavaScript的版本實(shí)現(xiàn)執(zhí)行不同的代碼
有時(shí)候需要根據(jù)JavaScript的版本來(lái)分別執(zhí)行一些代碼,那么就可能需要用到下面的代碼.2010-05-05
JS實(shí)現(xiàn)靜止元素自動(dòng)移動(dòng)示例
這篇文章主要介紹了JS實(shí)現(xiàn)靜止元素自動(dòng)移動(dòng)的具體實(shí)現(xiàn),需要的朋友可以參考下2014-04-04
JavaScript的模塊化:封裝(閉包),繼承(原型) 介紹
在復(fù)雜的邏輯下, JavaScript 需要被模塊化,模塊需要封裝起來(lái),只留下供外界調(diào)用的接口。閉包是 JavaScript 中實(shí)現(xiàn)模塊封裝的關(guān)鍵,也是很多初學(xué)者難以理解的要點(diǎn)2013-07-07

