溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript制作輪播圖的方法

發(fā)布時間:2020-06-12 17:42:34 來源:億速云 閱讀:190 作者:元一 欄目:web開發(fā)

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

知識點

JavaScript制作輪播圖的方法

輪播圖:

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

要實現(xiàn)的功能

1.鼠標經(jīng)過第幾個小圓點,就要展示第幾張圖片,并且小圓點的顏色也發(fā)生變化
2.圖片自動輪播,(這需要一個定時器)
3.鼠標經(jīng)過圖片,圖片停止自動播放(這需要清除定時器)
4.鼠標離開圖片,圖片繼續(xù)自動輪播 (重新開始定時器)

JavaScript制作輪播圖的方法

代碼

引入MyTools.js庫

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>
<p id="box">
 <p id="box_content">
 <p class="box_img"><img src="casual01.jpg" alt=""></p>
 <p class="box_img"><img src="casual02.jpg" alt=""></p>
 <p class="box_img"><img src="casual03.jpg" alt=""></p>
 <p class="box_img"><img src="casual04.jpg" alt=""></p>
 <p class="box_img"><img src="casual05.jpg" alt=""></p>
 </p>
 <p 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>
 </p>
</p>
<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. 獲取需要標簽
 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. 當前可視區(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標簽
 var bottomLi = controlBottom.children[k];
 // 監(jiān)聽鼠標進入
 (function (index) {
 bottomLi.addEventListener('mouseover',function (evt) {
  // 比較當前索引和點擊指示器位置關(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 = '';
 }
 // 當前的被選中
 controlBottom.children[iNow].className = 'current';
 }

 /**
 * 點擊右側(cè)和圖片自動運動操作
 */
 function autoPlay(){
 // 1. 當前可視區(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. 鼠標進入圖片p后設(shè)置和清除定時器
 myTool.$('box').addEventListener('mouseover',function () {
 clearInterval(timerId);
 });
 myTool.$('box').addEventListener('mouseout',function () {
 timerId = setInterval(autoPlay,2000);
 });

 })();
},false);

以上就是JS怎么實現(xiàn)簡單輪播圖特效?(圖文詳解)的詳細內(nèi)容,更多請關(guān)注億速云其它相關(guān)文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI