JS實現(xiàn)普通輪播圖特效
本文實例為大家分享了JS實現(xiàn)輪播圖特效的具體代碼,供大家參考,具體內(nèi)容如下
知識點

輪播圖思想:
① 建立一個全局變量索引,始終標(biāo)記當(dāng)前顯示圖片。
② 根據(jù)當(dāng)前圖片的數(shù)量,動態(tài)創(chuàng)建下方的●圖片指示器。
③ 輪播圖的初始狀態(tài)為第一張圖片在中間,剩余所有圖片均放在即將顯示圖片位置。
④ 當(dāng)點擊>的時候,當(dāng)前圖片調(diào)用動畫移動函數(shù)進行左移,與此同時新的一張圖片調(diào)用動畫函數(shù)移入到div中,而會將下一張展示的圖片移動到div右側(cè)。
⑤ 需要進行邊界判斷,如果當(dāng)前的圖片大于圖片數(shù)量或者小于等于0,重新給索引賦值。
⑥ 當(dāng)點擊圖片指示器的時候,首先判定點擊的與索引的位置關(guān)系,然后進行動畫移動。
⑦ 給div添加定時器,自動移動圖片。當(dāng)鼠標(biāo)進入div,刪除定時器,當(dāng)鼠標(biāo)移出div,設(shè)置定時器。
運行效果
1.自動輪播
2.點擊左右切換圖片
3.點擊下方圖片指示器切換圖片

代碼
1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="1.css" rel="external nofollow" > </head> <body> <div id="box"> <div id="box_content"> <div class="box_img"><img src="casual01.jpg" alt=""></div> <div class="box_img"><img src="casual02.jpg" alt=""></div> <div class="box_img"><img src="casual03.jpg" alt=""></div> <div class="box_img"><img src="casual04.jpg" alt=""></div> <div class="box_img"><img src="casual05.jpg" alt=""></div> </div> <div id="box_control"> <a href="javascript:;" class="box_control_left"><i><</i></a> <a href="javascript:;" class="box_control_right"><i>></i></a> <ul> </ul> </div> </div> <script src="../JavaScript學(xué)習(xí)/00MyTools/MyTools.js"></script> <script src="1.js"></script> </body> </html>
2.css
*{margin: 0;padding: 0;}
a{
color: #999;
text-decoration: none;
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, .4);
}
a:hover{
color: #f8b62b;
}
i{
font-size: 50px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#box{
height: 482px;
width: 830px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
overflow: hidden;
}
#box_content{
height: 100%;
width: 100%;
cursor: pointer;
}
#box_content img{
position: absolute;
vertical-align: top;
height: 100%;
width: 100%;
/*left: 830px;*/
}
.box_img{
width: 100%;
height: 100%;
position: absolute;}
.box_control_right{
position: absolute;
right: 0;
}
.box_control_left{
position: absolute;
left: 0;
}
ul{
position: absolute;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
display: flex;
justify-content:space-evenly;
}
ul>li{
list-style: none;
width: 16px;
height: 16px;
background-color: #fff;
margin: 0 3px;
border-radius: 50%;
cursor: pointer;
}
ul>li.current{
background-color: darkorange;
}
3.js
window.addEventListener('load',function (ev) {
// 輪播圖
(function () {
// 1. 獲取需要標(biāo)簽
var boxContent = myTool.$('box_content');
var contentImg = boxContent.children;
var boxControl = myTool.$('box_control');
var controlBottom = boxControl.children[2];
// 2. 全局索引
var iNow = 0;
// 3. 根據(jù)圖片個數(shù)動態(tài)添加下方圖片指示器
for (var i = 0; i < contentImg.length; i++) {
var li = document.createElement('li');
controlBottom.insertBefore(li,controlBottom.children[0]);
}
// 4. 讓第一個圖片指示器選中
controlBottom.children[0].className = 'current';
// 5. 讓除了第一張圖片以外所有圖片進入待顯示區(qū)域
var scrollImgWidth = boxContent.offsetWidth;
for (var j = 1; j < contentImg.length; j++) {
contentImg[j].style.left = scrollImgWidth + 'px';
}
// 6. 處理左右兩側(cè)點擊
var cPrev = boxControl.children[0];
var cNext = boxControl.children[1];
// 6.1 點擊左邊
cPrev.addEventListener('click',function (evt) {
// 1. 當(dāng)前可視區(qū)域圖片快速右移
// 2. 上一張幻燈片出現(xiàn)在可視區(qū)域左側(cè)
// 3. 讓這張幻燈片做動畫進入
myTool.slowMoving(contentImg[iNow],{'left':scrollImgWidth},null);
iNow--;
// 邊界處理
if (iNow < 0){
iNow = contentImg.length - 1;
}
contentImg[iNow].style.left = -scrollImgWidth + 'px';
myTool.slowMoving(contentImg[iNow],{'left':0},null);
// 切換索引
changeIndex();
},false);
// 6.2 點擊右邊
cNext.addEventListener('click',function (evt) {
autoPlay();
},false);
// 7. 下側(cè)圖片指示器操作
for (var k = 0; k < controlBottom.children.length; k++) {
// 取出單個li標(biāo)簽
var bottomLi = controlBottom.children[k];
// 監(jiān)聽鼠標(biāo)進入
(function (index) {
bottomLi.addEventListener('mouseover',function (evt) {
// 比較當(dāng)前索引和點擊指示器位置關(guān)系
if (index > iNow){
myTool.slowMoving(contentImg[iNow],{'left':-scrollImgWidth},null);
contentImg[index].style.left = scrollImgWidth + 'px';
}else if(index < iNow){
myTool.slowMoving(contentImg[iNow],{'left':scrollImgWidth},null);
contentImg[index].style.left = -scrollImgWidth + 'px';
}
iNow = index;
myTool.slowMoving(contentImg[iNow],{'left':0});
// 切換索引
changeIndex();
},false);
})(k)
}
/**
* 切換索引操作
*/
function changeIndex() {
for (var i = 0; i < controlBottom.children.length; i++) {
controlBottom.children[i].className = '';
}
// 當(dāng)前的被選中
controlBottom.children[iNow].className = 'current';
}
/**
* 點擊右側(cè)和圖片自動運動操作
*/
function autoPlay(){
// 1. 當(dāng)前可視區(qū)域圖片快速左移
// 2. 下一張圖片出現(xiàn)在可視區(qū)域右側(cè)
// 3. 讓這張圖片做動畫進入
myTool.slowMoving(contentImg[iNow],{'left':-scrollImgWidth},null);
iNow++;
// 邊界處理
if (iNow >= contentImg.length) {
iNow = 0;
}
contentImg[iNow].style.left = scrollImgWidth + 'px';
myTool.slowMoving(contentImg[iNow], {"left": 0},null);
// 切換索引
changeIndex();
}
// 8. 設(shè)置定時器
var timerId = setInterval(autoPlay,2000);
// 9. 鼠標(biāo)進入圖片div后設(shè)置和清除定時器
myTool.$('box').addEventListener('mouseover',function () {
clearInterval(timerId);
});
myTool.$('box').addEventListener('mouseout',function () {
timerId = setInterval(autoPlay,2000);
});
})();
},false);
更多關(guān)于輪播圖效果的專題,請點擊下方鏈接查看學(xué)習(xí)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 監(jiān)聽textarea中按鍵事件
不同的瀏覽器中的事件監(jiān)聽機制是不同的,以onKeyPress事件為例。2009-10-10
前端uniapp封裝網(wǎng)絡(luò)請求以及實際應(yīng)用教程
這篇文章主要給大家介紹了關(guān)于前端uniapp封裝網(wǎng)絡(luò)請求以及實際應(yīng)用的相關(guān)資料,在uniapp中進行網(wǎng)絡(luò)測試請求可以通過封裝網(wǎng)絡(luò)請求來實現(xiàn),文中給出了詳細(xì)的代碼實例,需要的朋友可以參考下2024-01-01
javascript中setTimeout和setInterval的unref()和ref()用法示例
本文通過一個小例子想大家講解了setTimeout和setInterval的unref()和ref()用法和使用環(huán)境,代碼很簡潔,有需要的小伙伴自己參考下吧。2014-11-11

