本文實(shí)例為大家分享了JS實(shí)現(xiàn)輪播圖特效的具體代碼,供大家參考,具體內(nèi)容如下
專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)古塔免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
知識(shí)點(diǎn)

輪播圖:
① 建立一個(gè)全局變量索引,始終標(biāo)記當(dāng)前顯示圖片。
② 根據(jù)當(dāng)前圖片的數(shù)量,動(dòng)態(tài)創(chuàng)建下方的●圖片指示器。
③ 輪播圖的初始狀態(tài)為第一張圖片在中間,剩余所有圖片均放在即將顯示圖片位置。
④ 當(dāng)點(diǎn)擊>的時(shí)候,當(dāng)前圖片調(diào)用動(dòng)畫移動(dòng)函數(shù)進(jìn)行左移,與此同時(shí)新的一張圖片調(diào)用動(dòng)畫函數(shù)移入到p中,而會(huì)將下一張展示的圖片移動(dòng)到p右側(cè)。
⑤ 需要進(jìn)行邊界判斷,如果當(dāng)前的圖片大于圖片數(shù)量或者小于等于0,重新給索引賦值。
⑥ 當(dāng)點(diǎn)擊圖片指示器的時(shí)候,首先判定點(diǎn)擊的與索引的位置關(guān)系,然后進(jìn)行動(dòng)畫移動(dòng)。
⑦ 給p添加定時(shí)器,自動(dòng)移動(dòng)圖片。當(dāng)鼠標(biāo)進(jìn)入p,刪除定時(shí)器,當(dāng)鼠標(biāo)移出p,設(shè)置定時(shí)器。
要實(shí)現(xiàn)的功能
1.鼠標(biāo)經(jīng)過第幾個(gè)小圓點(diǎn),就要展示第幾張圖片,并且小圓點(diǎn)的顏色也發(fā)生變化
2.圖片自動(dòng)輪播,(這需要一個(gè)定時(shí)器)
3.鼠標(biāo)經(jīng)過圖片,圖片停止自動(dòng)播放(這需要清除定時(shí)器)
4.鼠標(biāo)離開圖片,圖片繼續(xù)自動(dòng)輪播 (重新開始定時(shí)器)

代碼
引入MyTools.js庫(kù)
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. 獲取需要標(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ù)圖片個(gè)數(shù)動(dòng)態(tài)添加下方圖片指示器
for (var i = 0; i < contentImg.length; i++) {
var li = document.createElement('li');
controlBottom.insertBefore(li,controlBottom.children[0]);
}
// 4. 讓第一個(gè)圖片指示器選中
controlBottom.children[0].className = 'current';
// 5. 讓除了第一張圖片以外所有圖片進(jìn)入待顯示區(qū)域
var scrollImgWidth = boxContent.offsetWidth;
for (var j = 1; j < contentImg.length; j++) {
contentImg[j].style.left = scrollImgWidth + 'px';
}
// 6. 處理左右兩側(cè)點(diǎn)擊
var cPrev = boxControl.children[0];
var cNext = boxControl.children[1];
// 6.1 點(diǎn)擊左邊
cPrev.addEventListener('click',function (evt) {
// 1. 當(dāng)前可視區(qū)域圖片快速右移
// 2. 上一張幻燈片出現(xiàn)在可視區(qū)域左側(cè)
// 3. 讓這張幻燈片做動(dòng)畫進(jìn)入
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 點(diǎn)擊右邊
cNext.addEventListener('click',function (evt) {
autoPlay();
},false);
// 7. 下側(cè)圖片指示器操作
for (var k = 0; k < controlBottom.children.length; k++) {
// 取出單個(gè)li標(biāo)簽
var bottomLi = controlBottom.children[k];
// 監(jiān)聽鼠標(biāo)進(jìn)入
(function (index) {
bottomLi.addEventListener('mouseover',function (evt) {
// 比較當(dāng)前索引和點(diǎn)擊指示器位置關(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';
}
/**
* 點(diǎn)擊右側(cè)和圖片自動(dòng)運(yùn)動(dòng)操作
*/
function autoPlay(){
// 1. 當(dāng)前可視區(qū)域圖片快速左移
// 2. 下一張圖片出現(xiàn)在可視區(qū)域右側(cè)
// 3. 讓這張圖片做動(dòng)畫進(jìn)入
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è)置定時(shí)器
var timerId = setInterval(autoPlay,2000);
// 9. 鼠標(biāo)進(jìn)入圖片p后設(shè)置和清除定時(shí)器
myTool.$('box').addEventListener('mouseover',function () {
clearInterval(timerId);
});
myTool.$('box').addEventListener('mouseout',function () {
timerId = setInterval(autoPlay,2000);
});
})();
},false);以上就是JS怎么實(shí)現(xiàn)簡(jiǎn)單輪播圖特效?(圖文詳解)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!
文章名稱:JavaScript制作輪播圖的方法
網(wǎng)頁網(wǎng)址:http://www.chinadenli.net/article30/igpjso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、服務(wù)器托管、虛擬主機(jī)、網(wǎng)站維護(hù)、小程序開發(fā)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)