純js實現(xiàn)輪播圖效果
本文實例為大家分享了js實現(xiàn)輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下
結(jié)合我們前面學(xué)過的:鼠標(biāo)監(jiān)聽事件(移入移出、點擊),創(chuàng)建節(jié)點,排他思想、定時器等,就可以實現(xiàn)一個能手動和自動播放的輪播圖
效果圖

代碼
1. css
/*清除元素默認的內(nèi)外邊距 */
* {
margin: 0;
padding: 0
}
body{
width: 1000px;
margin: 0 auto;
}
/*去掉列表前面的小點*/
li {
list-style: none;
}
/*圖片沒有邊框 去掉圖片底側(cè)的空白縫隙*/
img {
border: 0; /*ie6*/
vertical-align: middle;
}
/*取消鏈接的下劃線*/
a {
color: #666;
text-decoration: none;
}
a:hover {
color: #e33333;
}
.fl {
float: left;
}
.fr {
float: right;
}
.focus {
position: relative;
width: 721px;
height: 455px;
background-color: purple;
overflow: hidden;
margin-top: 20px;
}
.focus ul {
position: absolute;
top: 0;
left: 0;
width: 600%;
}
.focus ul li {
float: left;
}
.arrow-l,
.arrow-r {
display: none;
position: absolute;
top: 50%;
margin-top: -20px;
width: 24px;
height: 40px;
background: rgba(0, 0, 0, .3);
text-align: center;
line-height: 40px;
color: #fff;
font-family: 'icomoon';
font-size: 18px;
z-index: 2;
}
.arrow-r {
right: 0;
}
.circle {
position: absolute;
bottom: 10px;
left: 50px;
}
.circle li {
float: left;
width: 8px;
height: 8px;
/*background-color: #fff;*/
border: 2px solid rgba(255, 255, 255, 0.5);
margin: 0 3px;
border-radius: 50%;
/*鼠標(biāo)經(jīng)過顯示小手*/
cursor: pointer;
}
.current {
background-color: #fff;
}
2. html
<div class="focus fl">
<!-- 左側(cè)按鈕 -->
<a href="javascript:;" class="arrow-l arrow"> < </a>
<!-- 右側(cè)按鈕 -->
<a href="javascript:;" class="arrow-r arrow"> > </a>
<!-- 核心的滾動區(qū)域 -->
<ul>
<li>
<a href="#" ><img src="images/focus.jpg" alt=""></a>
</li>
<li>
<a href="#" ><img src="images/focus1.jpg" alt=""></a>
</li>
<li>
<a href="#" ><img src="images/focus2.jpg" alt=""></a>
</li>
<li>
<a href="#" ><img src="images/focus3.jpg" alt=""></a>
</li>
</ul>
<!-- 小圓圈 -->
<ol class="circle">
</ol>
</div>
3. JavaScript
window.addEventListener('load', function() {
// 1. 獲取元素
var arrow_l = document.querySelector('.arrow-l');
var arrow_r = document.querySelector('.arrow-r');
var focus = document.querySelector('.focus');
var focusWidth = focus.offsetWidth;
// 2. 鼠標(biāo)經(jīng)過focus 就顯示隱藏左右按鈕
focus.addEventListener('mouseenter', function() {
arrow_l.style.display = 'block';
arrow_r.style.display = 'block';
clearInterval(timer);
timer = null; // 清除定時器變量
});
focus.addEventListener('mouseleave', function() {
arrow_l.style.display = 'none';
arrow_r.style.display = 'none';
timer = setInterval(function() {
//手動調(diào)用點擊事件
arrow_r.click();
}, 2000);
});
// 3. 動態(tài)生成小圓圈 有幾張圖片,我就生成幾個小圓圈
var ul = focus.querySelector('ul');
var ol = focus.querySelector('.circle');
// console.log(ul.children.length);
for (var i = 0; i < ul.children.length; i++) {
// 創(chuàng)建一個小li
var li = document.createElement('li');
// 記錄當(dāng)前小圓圈的索引號 通過自定義屬性來做
li.setAttribute('index', i);
// 把小li插入到ol 里面
ol.appendChild(li);
// 4. 小圓圈的排他思想 我們可以直接在生成小圓圈的同時直接綁定點擊事件
li.addEventListener('click', function() {
// 干掉所有人 把所有的小li 清除 current 類名
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
// 留下我自己 當(dāng)前的小li 設(shè)置current 類名
this.className = 'current';
// 5. 點擊小圓圈,移動圖片 當(dāng)然移動的是 ul
// ul 的移動距離 小圓圈的索引號 乘以 圖片的寬度 注意是負值
// 當(dāng)我們點擊了某個小li 就拿到當(dāng)前小li 的索引號
var index = this.getAttribute('index');
// 當(dāng)我們點擊了某個小li 就要把這個li 的索引號給 num
num = index;
// 當(dāng)我們點擊了某個小li 就要把這個li 的索引號給 circle
circle = index;
// num = circle = index;
console.log(focusWidth);
console.log(index);
animate(ul, -index * focusWidth);
})
}
// 把ol里面的第一個小li設(shè)置類名為 current
ol.children[0].className = 'current';
// 6. 克隆第一張圖片(li)放到ul 最后面
var first = ul.children[0].cloneNode(true);
ul.appendChild(first);
// 7. 點擊右側(cè)按鈕, 圖片滾動一張
var num = 0;
// circle 控制小圓圈的播放
var circle = 0;
// flag 節(jié)流閥
var flag = true;
arrow_r.addEventListener('click', function() {
if (flag) {
flag = false; // 關(guān)閉節(jié)流閥
// 如果走到了最后復(fù)制的一張圖片,此時 我們的ul 要快速復(fù)原 left 改為 0
if (num == ul.children.length - 1) {
ul.style.left = 0;
num = 0;
}
num++;
animate(ul, -num * focusWidth, function() {
flag = true; // 打開節(jié)流閥
});
// 8. 點擊右側(cè)按鈕,小圓圈跟隨一起變化 可以再聲明一個變量控制小圓圈的播放
circle++;
// 如果circle == 4 說明走到最后我們克隆的這張圖片了 我們就復(fù)原
if (circle == ol.children.length) {
circle = 0;
}
// 調(diào)用函數(shù)
circleChange();
}
});
// 9. 左側(cè)按鈕做法
arrow_l.addEventListener('click', function() {
if (flag) {
flag = false;
if (num == 0) {
num = ul.children.length - 1;
ul.style.left = -num * focusWidth + 'px';
}
num--;
animate(ul, -num * focusWidth, function() {
flag = true;
});
// 點擊左側(cè)按鈕,小圓圈跟隨一起變化 可以再聲明一個變量控制小圓圈的播放
circle--;
// 如果circle < 0 說明第一張圖片,則小圓圈要改為第4個小圓圈(3)
// if (circle < 0) {
// circle = ol.children.length - 1;
// }
circle = circle < 0 ? ol.children.length - 1 : circle;
// 調(diào)用函數(shù)
circleChange();
}
});
function circleChange() {
// 先清除其余小圓圈的current類名
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
// 留下當(dāng)前的小圓圈的current類名
ol.children[circle].className = 'current';
}
// 10. 自動播放輪播圖
var timer = setInterval(function() {
//手動調(diào)用點擊事件
arrow_r.click();
}, 2000);
})
重點?。。?/span>
用到的實現(xiàn)圖片移動的動畫文件,animate.js
function animate(obj, target, callback) {
// console.log(callback); callback = function() {} 調(diào)用的時候 callback()
// 先清除以前的定時器,只保留當(dāng)前的一個定時器執(zhí)行
clearInterval(obj.timer);
obj.timer = setInterval(function() {
// 步長值寫到定時器的里面
// 把我們步長值改為整數(shù) 不要出現(xiàn)小數(shù)的問題
// var step = Math.ceil((target - obj.offsetLeft) / 10);
var step = (target - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (obj.offsetLeft == target) {
// 停止動畫 本質(zhì)是停止定時器
clearInterval(obj.timer);
// 回調(diào)函數(shù)寫到定時器結(jié)束里面
// if (callback) {
// // 調(diào)用函數(shù)
// callback();
// }
callback && callback();
}
// 把每次加1 這個步長值改為一個慢慢變小的值 步長公式:(目標(biāo)值 - 現(xiàn)在的位置) / 10
obj.style.left = obj.offsetLeft + step + 'px';
}, 15);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript forEach 方法跳出循環(huán)的操作方法
這篇文章主要介紹了JavaScript forEach 方法跳出循環(huán)的操作方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01
JS中的算法與數(shù)據(jù)結(jié)構(gòu)之字典(Dictionary)實例詳解
這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之字典(Dictionary),結(jié)合實例形式詳細分析了javascript數(shù)據(jù)結(jié)構(gòu)中字典的概念、原理、定義與常見使用方法,需要的朋友可以參考下2019-08-08
javascript圓盤抽獎程序?qū)崿F(xiàn)原理和完整代碼例子
這篇文章主要介紹了javascript圓盤抽獎程序?qū)崿F(xiàn)原理和完整代碼例子,需要的朋友可以參考下2014-06-06
Echarts中X軸/Y軸坐標(biāo)標(biāo)簽顯示不下的問題解決
本文主要介紹了Echarts中X軸/Y軸坐標(biāo)標(biāo)簽顯示不下的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09

