js實(shí)現(xiàn)自動播放勻速輪播圖
本文實(shí)例為大家分享了js實(shí)現(xiàn)自動播放勻速輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
函數(shù)封裝: ( 勻速運(yùn)動函數(shù))
function animate(obj,target,step,speed){
clearInterval(obj.timer);
var absStep = Math.abs(step);
step = target > obj.offsetLeft ? absStep : -absStep;
obj.timer = setInterval(function(){
var distance = Math.abs(target - obj.offsetLeft);
obj.style.left = obj.offsetLeft + step + 'px';
if(distance < absStep){
clearInterval(obj.timer);
obj.style.left = target + 'px';
}
},speed);
}
對象可以動態(tài)生成屬性,用對象的timer,避免了全局變量的使用,
實(shí)現(xiàn)步驟:
1.動態(tài)生成ol導(dǎo)航條,并將導(dǎo)航條放入all中使其成為孩子節(jié)點(diǎn)
2.根據(jù)ul中圖片數(shù)量動態(tài)生成li標(biāo)簽,使li成為ol的子節(jié)點(diǎn),
3.給第0個li標(biāo)簽加上顏色(添加屬性current)
4.用設(shè)置的屬性的值去操作圖片使圖片移動,達(dá)到鼠標(biāo)放上去移動到該圖片效果,排它原理實(shí)現(xiàn)樣式效果
5.深度克隆ul中的第一張圖片并將圖片放在ul的末尾
6.加入自動播放函數(shù)使其自動播放,設(shè)置兩個變量key,squre,key的值用來計算圖片的序號,squre用來計算當(dāng)前l(fā)i的序號
7.自動播放函數(shù)中排他原理設(shè)置當(dāng)前l(fā)i標(biāo)簽樣式
8.在設(shè)置onmouseover和onmouseout事件鼠標(biāo)放在盒子上暫停,鼠標(biāo)離開盒子,繼續(xù)運(yùn)動
9.在鼠標(biāo)放在li標(biāo)簽時讓key等于當(dāng)前圖片的index屬性值 ,并把key的值賦給squre。
實(shí)現(xiàn)細(xì)節(jié):
1.動態(tài)給ul克隆出第0張圖片補(bǔ)到末尾,以實(shí)現(xiàn)自動輪播是無縫的效果,
2.克隆分深克隆和淺克隆,深克隆克隆帶標(biāo)簽內(nèi)的所有內(nèi)容,
3.淺克隆只克隆外部標(biāo)簽,深克隆參數(shù)為true
效果:

代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>勻速輪播動畫</title>
<style type="text/css">
*{ padding:0; margin:0; list-style:none; border:0;}
.all{
width:500px;
height:200px;
padding:7px;
border:1px solid #ccc;
margin:100px auto;
position:relative;
}
.screen{
width:500px;
height:200px;
overflow:hidden;
position:relative;
}
.screen li{
width:500px;
height:200px;
overflow:hidden;
float:left;
}
.screen ul{
position:absolute;
left:0;
top:0px;
width:3000px;
}
.all ol{
position:absolute;
right:10px;
bottom:10px;
line-height:20px;
text-align:center;
}
.all ol li{
float:left;
width:20px;
height:20px;
background:#fff;
border:1px solid #ccc;
margin-left:10px;
cursor:pointer;
}
.all ol li.current{
background:yellow;
}
</style>
<script src="js/勻速運(yùn)動.js"></script>
<script>
function $(id){
return document.getElementById(id);
}
window.onload = function(){
var ul = $('ul');
var all = $('all');
var imgs = ul.getElementsByTagName('img');
var ol = document.createElement('ol');
all.appendChild(ol);
for(var i=0;i<imgs.length;i++){
var li = document.createElement('li');
li.innerHTML = i+1;
li.setAttribute('index',i);
ol.appendChild(li);
}
ol.childNodes[0].className = 'current';
var olLis = ol.children;
for(var i=0;i<olLis.length;i++){
olLis[i].onmouseover = function(){
for(var j=0;j<olLis.length;j++){
olLis[j].className = '';
}
this.className = 'current';
var index = -500*this.getAttribute('index');
animate(ul,index,20,10);
key=this.getAttribute('index');
squre = key;
}
}
ul.appendChild(ul.children[0].cloneNode(true));
var timer=null;
var key=0;
var squre = 0;
timer=setInterval(autoPlay, 1000);
function autoPlay(){
key++; //記錄圖片
squre++;//記錄導(dǎo)航條
if(key>olLis.length){
key=1;
ul.style.left = 0 + 'px';
}
if(squre>=olLis.length){
squre = 0;
}
animate(ul,-500*key,20,10);
for(var i=0;i<olLis.length;i++){
olLis[i].className = '';
}
olLis[squre].className = 'current';
}
all.onmouseover = function(){
clearInterval(timer);
}
all.onmouseout = function(){
timer=setInterval(autoPlay, 1000);
}
}
</script>
</head>
<body>
<div class="all" id='all'>
<div class="screen">
<ul id="ul">
<li><img src="images/taobao/1.jpg" width="500" height="200" /></li>
<li><img src="images/taobao/2.jpg" width="500" height="200" /></li>
<li><img src="images/taobao/3.jpg" width="500" height="200" /></li>
<li><img src="images/taobao/4.jpg" width="500" height="200" /></li>
<li><img src="images/taobao/5.jpg" width="500" height="200" /></li>
</ul>
</div>
</div>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 如何使用JavaScript實(shí)現(xiàn)無縫滾動自動播放輪播圖效果
- js實(shí)現(xiàn)支持手機(jī)滑動切換的輪播圖片效果實(shí)例
- vue.js+elementUI實(shí)現(xiàn)點(diǎn)擊左右箭頭切換頭像功能(類似輪播圖效果)
- JS仿京東移動端手指撥動切換輪播圖效果
- javascript實(shí)現(xiàn)點(diǎn)擊按鈕切換輪播圖功能
- 原生JS實(shí)現(xiàn)旋轉(zhuǎn)輪播圖+文字內(nèi)容切換效果【附源碼】
- js實(shí)現(xiàn)輪播圖效果 純js實(shí)現(xiàn)圖片自動切換
- vue自定義js圖片碎片輪播圖切換效果的實(shí)現(xiàn)代碼
- js輪播圖透明度切換(帶上下頁和底部圓點(diǎn)切換)
- js實(shí)現(xiàn)點(diǎn)擊切換和自動播放的輪播圖
相關(guān)文章
JS實(shí)現(xiàn)給不同元素設(shè)置不同的定時器
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)給不同元素設(shè)置不同的定時器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
JScript中的"this"關(guān)鍵字使用方式補(bǔ)充材料
JScript中的"this"關(guān)鍵字使用方式補(bǔ)充材料...2007-03-03
微信小程序 點(diǎn)擊控件后選中其它反選實(shí)例詳解
這篇文章主要介紹了微信小程序 點(diǎn)擊控件后選中其它反選實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
js實(shí)現(xiàn)checkbox全選、不選與反選的方法
這篇文章主要介紹了js實(shí)現(xiàn)checkbox全選、不選與反選的方法,以實(shí)例形式詳細(xì)分析了實(shí)現(xiàn)的思路及對應(yīng)的html與js代碼的實(shí)現(xiàn)過程,非常具有實(shí)用價值,需要的朋友可以參考下2015-02-02
JavaScript實(shí)現(xiàn)可拖動模態(tài)框
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)可拖動模態(tài)框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
BootStrap modal模態(tài)彈窗使用小結(jié)
這篇文章主要為大家詳細(xì)介紹了BootStrap modal模態(tài)彈窗使用小結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

