原生js實(shí)現(xiàn)輪播圖特效
輪播圖也稱為焦點(diǎn)圖,是網(wǎng)頁中比較常見的網(wǎng)頁特效。
功能需求:
1.鼠標(biāo)經(jīng)過輪播圖模塊,左右按鈕顯示,離開隱藏左右按鈕。
2.點(diǎn)擊右側(cè)按鈕一次,圖片往左播放一張,以此類推,左側(cè)按鈕同理。
3.圖片播放的同時(shí),下面小圓圈模塊跟隨一起變化。
4.點(diǎn)擊小圓圈,可以播放相應(yīng)圖片。
5.鼠標(biāo)不經(jīng)過輪播圖,輪播圖也會(huì)自動(dòng)播放圖片。
6.鼠標(biāo)經(jīng)過,輪播圖模塊, 自動(dòng)播放停止。
此文章的代碼為一個(gè)完整的輪播圖的實(shí)現(xiàn)代碼,復(fù)制即可運(yùn)行
實(shí)現(xiàn)效果由于大小問題無法上傳,可自行觀看淘寶或京東首頁輪播圖效果
html文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="css/index.css" rel="external nofollow" > <script src="js/animate.js"></script> <script src="js/index.js"></script> </head> <body> <div class="box"> <ul> <li><img src="img/focus.jpg" alt=""></li> <li><img src="img/focus1.jpg" alt=""></li> <li><img src="img/focus2.jpg" alt=""></li> <li><img src="img/focus3.jpg" alt=""></li> </ul> <ol> </ol> <div class="arr-l"> < </div> <div class="arr-r"> > </div> </div> </body> </html>
css文件:
* {
margin: 0;
padding: 0;
outline: none;
}
.box {
overflow: hidden;
position: relative;
height: 455px;
width: 721px;
background-color: pink;
margin: 50px auto;
}
.box ul {
position: absolute;
top: 0;
left: 0;
width: 600%;
}
.box ul li {
list-style: none;
float: left;
}
.box ol {
position: absolute;
bottom: 10px;
left: 100px;
background-color: rgba(255, 255, 255, .3);
border-radius: 8px;
list-style: none;
height: 10px;
padding: 2px 5px;
}
.box ol li {
cursor: pointer;
float: left;
height: 8px;
width: 8px;
border: 1px solid #fff;
border-radius: 50%;
margin: 0 5px;
}
.arr-l,
.arr-r {
display: none;
color: #fff;
cursor: pointer;
position: absolute;
height: 40px;
width: 30px;
background-color: rgba(0, 0, 0, .3);
line-height: 40px;
text-align: center;
}
.arr-l {
top: 50%;
left: 0;
transform: translateY(-50%);
}
.arr-r {
right: 0;
top: 50%;
transform: translateY(-50%);
}
.current {
background-color: #fff;
}
動(dòng)畫實(shí)現(xiàn)js文件animate.js:
function animate(obj, target, callback) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
if (target - obj.offsetLeft > 0) {
var step = Math.ceil((target - obj.offsetLeft) / 10);
} else {
var step = Math.floor((target - obj.offsetLeft) / 10);
}
if (obj.offsetLeft == target) {
clearInterval(obj.timer);
callback && callback();
}
obj.style.left = obj.offsetLeft + step + 'px';
}, 15)
}
index.js:
window.addEventListener('load', function () {
var arrl = this.document.querySelector('.arr-l');
var arrr = this.document.querySelector('.arr-r');
var box = this.document.querySelector('.box');
var boxWidth = box.offsetWidth;
box.addEventListener('mouseenter', function () {
arrl.style.display = 'block';
arrr.style.display = 'block';
clearInterval(timer);
timer = null;
})
box.addEventListener('mouseleave', function () {
arrl.style.display = 'none';
arrr.style.display = 'none';
timer = setInterval(function () {
arrr.click();
}, 2000);
});
var ul = box.querySelector('ul');
var ol = box.querySelector('ol');
for (var i = 0; i < ul.children.length; i++) {
var li = document.createElement('li');
li.setAttribute('data-index', i);
ol.appendChild(li);
ol.children[0].className = 'current';
li.addEventListener('click', function () {
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
this.className = 'current';
var dateindex = this.getAttribute('data-index');
num = dateindex;
circle = dateindex;
animate(ul, -dateindex * boxWidth);
});
}
var first = ul.children[0].cloneNode(true);
ul.appendChild(first);
var num = 0;
var circle = 0;
var flag = true;
arrr.addEventListener('click', function () {
if (flag) {
flag = false;
if (num == ul.children.length - 1) {
ul.style.left = 0;
num = 0;
}
num++;
animate(ul, -num * boxWidth, function () {
flag = true;
});
circle++;
if (circle == ol.children.length) {
circle = 0;
}
circleChange();
}
});
arrl.addEventListener('click', function () {
if (flag) {
flag = false;
if (num == 0) {
num = ul.children.length - 1;
ul.style.left = -num * boxWidth + 'px';
}
num--;
animate(ul, -num * boxWidth, function () {
flag = true;
});
circle--;
if (circle < 0) {
circle = ol.children.length - 1;
}
circleChange();
}
});
function circleChange() {
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
ol.children[circle].className = 'current';
}
var timer = setInterval(function () {
arrr.click();
}, 2000);
})
注意事項(xiàng)
引用兩個(gè)js文件時(shí),animate.js要在index.js之前引用,因?yàn)閕ndex.js中要用到animate.js里的函數(shù)。
精彩專題分享:jQuery圖片輪播 JavaScript圖片輪播 Bootstrap圖片輪播
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)3D粒子酷炫動(dòng)態(tài)旋轉(zhuǎn)特效
- js+css3實(shí)現(xiàn)簡單時(shí)鐘特效
- js實(shí)現(xiàn)可愛的氣泡特效
- JS實(shí)現(xiàn)頁面鼠標(biāo)點(diǎn)擊出現(xiàn)圖片特效
- js實(shí)現(xiàn)輪播圖特效
- js實(shí)現(xiàn)無縫輪播圖特效
- js實(shí)現(xiàn)煙花特效
- JS實(shí)現(xiàn)商品櫥窗特效
- JS實(shí)現(xiàn)吸頂特效
- JavaScript實(shí)現(xiàn)聯(lián)動(dòng)菜單特效
- JS實(shí)現(xiàn)電商商品展示放大鏡特效
- js實(shí)現(xiàn)星星海特效的示例
相關(guān)文章
javascript實(shí)現(xiàn)日期格式轉(zhuǎn)換
這篇文章主要介紹了javascript實(shí)現(xiàn)日期格式轉(zhuǎn)換,非常的簡單實(shí)用,項(xiàng)目中經(jīng)??梢杂玫剑@里推薦給大家2014-12-12
快速解決FusionCharts聯(lián)動(dòng)的中文亂碼問題
Nuxt.js 數(shù)據(jù)雙向綁定的實(shí)現(xiàn)
JS圖片根據(jù)鼠標(biāo)滾動(dòng)延時(shí)加載的實(shí)例代碼

