js制作輪播圖效果
輪播圖在前端開(kāi)發(fā)中我認(rèn)為是一個(gè)比較重要的點(diǎn),因?yàn)榘撕芏嘣鷍s知識(shí)點(diǎn),以下是我學(xué)習(xí)制作輪播圖的過(guò)程
難點(diǎn):
1、如何讓底下圓圈和圖片所對(duì)應(yīng)自動(dòng)動(dòng)態(tài)生成
2、如何讓底下圓圈和圖片所對(duì)應(yīng)的起來(lái)
3、上一頁(yè)和下一頁(yè)所在盒子所移動(dòng)的距離
4、圖片切換時(shí)的漸出動(dòng)畫(huà)效果
5、節(jié)流閥的概念
效果:

代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
a {
text-decoration: none;
color: white;
line-height: 50px;
text-align: center;
}
li {
list-style: none;
}
.tb-promo {
position: relative;
width: 700px;
height: 300px;
margin: auto;
overflow: hidden;
}
.tb-promo .imgg {
position: absolute;
top: 0;
left: 0;
width: 3000px;
}
.tb-promo .imgg li {
float: left;
}
.tb-promo .imgg li img {
width: 700px;
height: 300px;
}
.tb-promo .prev {
display: none;
position: absolute;
top: 125px;
left: 0;
width: 25px;
height: 50px;
background-color: rgba(0, 0, 0, 0.2);
border-top-right-radius: 25px;
border-bottom-right-radius: 25px;
z-index: 999;
}
.tb-promo .prev:hover {
background-color: rgba(0, 0, 0, 0.5);
}
.tb-promo .next {
display: none;
position: absolute;
top: 125px;
right: 0;
width: 25px;
height: 50px;
background-color: rgba(0, 0, 0, 0.2);
border-top-left-radius: 25px;
border-bottom-left-radius: 25px;
z-index: 999;
}
.tb-promo .next:hover {
background-color: rgba(0, 0, 0, 0.5);
}
.tb-promo .promo-nav {
position: absolute;
top: 270px;
left: 50%;
margin-left: -40px;
height: 25px;
}
.tb-promo .promo-nav li {
float: left;
width: 16px;
height: 16px;
background-color: white;
border-radius: 8px;
margin: 4px;
}
.tb-promo .promo-nav .one {
background-color: tomato;
}
</style>
</head>
<body>
<div class="tb-promo">
<a href="javascript:;" class="prev"><</a>
<a href="javascript:;" class="next">></a>
<ul class="imgg">
<li><img src="./61.jpeg" alt=""></li>
<li><img src="./62.jpeg" alt=""></li>
<li><img src="./63.jpeg" alt=""></li>
</ul>
<ol class="promo-nav">
</ol>
</div>
<script>
var prev = document.querySelector('.prev');
var next = document.querySelector('.next');
var tbpromo = document.querySelector('.tb-promo');
var ul = document.querySelector('ul');
var ol = document.querySelector('ol');
//動(dòng)畫(huà)函數(shù)
function animate(obj, target) {
clearInterval(obj.timer);//調(diào)用防止點(diǎn)擊多次調(diào)用
obj.timer = setInterval(function () {
var step = (target - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step); //正直和負(fù)值取整
if (obj.offsetLeft == target) {
clearInterval(obj.timer);
} else {
obj.style.left = obj.offsetLeft + step + 'px';
}
}, 10)
}
//生成動(dòng)態(tài)導(dǎo)航圈圈
var tbpromWidth = tbpromo.offsetWidth;
for (var i = 0; i < ul.children.length; i++) {
var li = document.createElement('li');
ol.appendChild(li);
//記錄索引號(hào) 通過(guò)自定義屬性
li.setAttribute('index', i);
//排他思想 寫(xiě)圓圈變色
li.addEventListener('click', function () {
//清除所有圈圈顏色
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
this.className = 'one';
var index = this.getAttribute('index');
//點(diǎn)擊li 把li 的索引號(hào)分別給控制變量
num = index;
circle=index;
animate(ul, -index * tbpromWidth);
})
ol.children[0].className = 'one';
}
//克隆第一張圖片li放在最后面 無(wú)縫切換
var frist = ul.children[0].cloneNode(true);
ul.appendChild(frist);
//隱藏顯示 下一頁(yè)上一頁(yè)
tbpromo.addEventListener('mouseenter', function () {
prev.style.display = 'block';
next.style.display = 'block';
clearInterval(timer);
timer=0;//清除定時(shí)器變量
})
tbpromo.addEventListener('mouseleave', function () {
prev.style.display = 'none';
next.style.display = 'none';
timer=setInterval(function () {
next.click();//手動(dòng)調(diào)動(dòng)點(diǎn)擊事件
},1500)
})
//next prev動(dòng)畫(huà)
var num = 0;
//控制圓圈
var circle = 0;
//節(jié)流閥變量
var flag=true;
//下一頁(yè)
next.addEventListener('click', function () {
//最后一張進(jìn)行判斷 ul復(fù)原 left為0
if (num == (ul.children.length - 1)) {
ul.style.left = 0;
num = 0;
}
num++;
animate(ul, -num * tbpromWidth);
circle++;
if (circle == 3) {
circle = 0;
}
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
ol.children[circle].className = 'one';
})
//上一頁(yè)
prev.addEventListener('click', function () {
if (num == 0) {
num = ul.children.length-1;
ul.style.left = -num*tbpromWidth+'px';
}
else {
num--;
animate(ul, -num * tbpromWidth);
circle--;
if (circle <0) {
circle = 2;
}
for (var i = 0; i < ol.children.length; i++) {
ol.children[i].className = '';
}
ol.children[circle].className = 'one';
}
})
//自動(dòng)播放
var timer=setInterval(function () {
next.click();//手動(dòng)調(diào)動(dòng)點(diǎn)擊事件
},2000)
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript日期對(duì)象(Date)基本用法示例
這篇文章主要介紹了JavaScript日期對(duì)象(Date)基本用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了JavaScript日期對(duì)象(Date)獲取日期、時(shí)間戳、年月日、星期及日期比對(duì)等操作技巧,需要的朋友可以參考下2017-01-01
js判斷數(shù)組key是否存在(不用循環(huán))的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇js判斷數(shù)組key是否存在(不用循環(huán))的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
整理的比較全的event對(duì)像在ie與firefox瀏覽器中的區(qū)別
event對(duì)像在IE與FF中的區(qū)別,本文整理了很多,個(gè)人感覺(jué)還是比較全面的,需要的朋友可以收藏下2013-11-11
JS限制Textarea文本域字符個(gè)數(shù)的具體實(shí)現(xiàn)
這篇文章介紹了JS限制Textarea文本域字符個(gè)數(shù)的具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-08-08
小程序使用wxs解決wxml保留2位小數(shù)問(wèn)題
這篇文章主要介紹了小程序使用wxs解決wxml保留2位小數(shù)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
BootStrap下拉菜單和滾動(dòng)監(jiān)聽(tīng)插件實(shí)現(xiàn)代碼
這篇文章主要介紹了BootStrap下拉菜單和滾動(dòng)監(jiān)聽(tīng)插件實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
BootStrap Validator使用注意事項(xiàng)(必看篇)
針對(duì)bootstrap2和bootstrap3有不同的版本,在使用bootstrap validator時(shí)需要了解其注意事項(xiàng),下面小編把我遇到的注意事項(xiàng)分享給大家,供大家參考2016-09-09

