最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生js實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果

 更新時(shí)間:2017年01月20日 11:58:29   作者:夏天不做夢(mèng)  
本文主要介紹了原生js實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧

知識(shí)要點(diǎn)

1.實(shí)現(xiàn)無(wú)限循環(huán)的原理:

以偏移的距離來(lái)判斷是否跳回第一張和最后一張

也可以利用循環(huán)判斷圖片的當(dāng)前索引值

var newLeft=parseInt(list.style.left)+offset;//當(dāng)前的偏移量+下一次的偏移量=新的偏移量
list.style.left=newLeft+"px";//當(dāng)前的偏移值=新的偏移值
//以偏移的距離來(lái)判斷是否跳回第一張和最后一張
if(newLeft>-600){
 list.style.left=-3000+"px";
}
if (newLeft<-3000){
 list.style.left=-600+"px";
}

2.當(dāng)前圖片輪播的圓點(diǎn)變色顯示:

因?yàn)槊看吸c(diǎn)擊index+1 所以當(dāng)前的index-1就是button的索引

//添加on前先清空on
for(var i=0;i<buttons.length;i++){
 if(buttons[i].className=="on"){
 buttons[i].className="";
 break;
 }
}
buttons[index-1].className="on";

3.實(shí)現(xiàn)動(dòng)畫滾動(dòng)效果:

原理就是把每次的偏移量分為多次完成比如一次600px那就分為10次每次偏移60px

就要用到setTimeout(go,10);//10毫秒再次調(diào)用go函數(shù),一直到不滿足條件就停

var newLeft=parseInt(list.style.left)+offset;//當(dāng)前的偏移量+下一次的偏移量=新的偏移量
var time=300;//位移總時(shí)間
var interval=10;//位移間隔時(shí)間
//動(dòng)畫效果自定義公式: 每次位移的距離=單次偏移距離/位移次數(shù)
var speed=offset/(time/interval);
//遞歸函數(shù) 直到不滿足條件(跳到輔助圖)
//遞歸就是把600偏移量分為多次完成偏移
function go(){
 //speed<0 并且 當(dāng)前偏移量>下一次偏移量 就是向左偏移 || 反之向右偏移 
 if ((speed<0 &&parseInt(list.style.left)>newLeft) || (speed>0 &&parseInt(list.style.left)<newLeft)) {
 list.style.left=parseInt(list.style.left)+speed+"px";//每次位移的值
 setTimeout(go,interval);//10毫秒再次調(diào)用go函數(shù)
 }else{
 animated=false;
 list.style.left=newLeft+"px";//當(dāng)前的偏移值=新的偏移值
 if(newLeft>-600){
 list.style.left=-3000+"px";
 }
 if (newLeft<-3000){
 list.style.left=-600+"px";
 }
 }
}

4.點(diǎn)擊圓點(diǎn)按鈕執(zhí)行動(dòng)畫:

原理獲取當(dāng)前的按鈕位置再獲取要點(diǎn)擊的按鈕的位置

用(點(diǎn)擊的——當(dāng)前的)*-600=需要跳轉(zhuǎn)的正負(fù)距離(向左或向右)

for(var i=0;i<buttons.length;i++){
 buttons[i].onclick=function(){
 if(this.className=="on"){
 return;
 }
 //要點(diǎn)擊的index屬性的值 轉(zhuǎn)換為整數(shù)
 var myIndex=parseInt(this.getAttribute("index"));
 //偏移量=-600*(要點(diǎn)擊的位置-當(dāng)前所在的位置),當(dāng)前的位置就是index循環(huán)所得
 var os=-600*(myIndex-index);
 //切換完成后,把點(diǎn)擊的index位置變成當(dāng)前的index位置 
 index=myIndex;
 showButton();
 if(!animated){
 animate(os);
 }
 }
}

5.自動(dòng)播放:

給外層容器加個(gè)onmouseover事件再調(diào)用setInterval方法

//給方法定義全局變量是因?yàn)橥V沟臅r(shí)候要使用
function play(){
 timer=setInterval(function(){
 next.onclick();
 },3000);
}
clearInterval(timer)

完整代碼

注:圖片鏈接本地替換一下

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
#container{width: 600px; height: 400px; overflow: hidden; position: relative; }
#list{width: 4200px; height: 400px; position: absolute; z-index: 1;}
#list img{float: left;}
#buttons { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px;}
#buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 5px;}
#buttons .on { background: orangered;}
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px; position: absolute; z-index: 2; top: 180px; background-color: RGBA(0,0,0,.3); color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#container:hover .arrow { display: block;}
#prev { left: 20px;}
#next { right: 20px;}
</style> 
</head> 
<body>
 <div id="container">
 <div id="list" style="left: -600px;">
 <img src="images/5.jpg" alt="5"/>  
  <img src="images/1.jpg" alt="1"/>
  <img src="images/2.jpg" alt="2"/>
  <img src="images/3.jpg" alt="3"/>
  <img src="images/4.jpg" alt="4"/>
  <img src="images/5.jpg" alt="5"/>
  <img src="images/1.jpg" alt="1"/>
 </div>
 <div id="buttons">
  <span index="1" class="on"></span>
  <span index="2"></span>
  <span index="3"></span>
  <span index="4"></span>
  <span index="5"></span>
 </div>
 <a href="javascript:;" id="prev" class="arrow">&lt;</a>
 <a href="javascript:;" id="next" class="arrow">&gt;</a>
 </div>
 <script type="text/javascript">
 //在頁(yè)面加載完后立即執(zhí)行多個(gè)函數(shù)方案。
 function addloadEvent(func){
 var oldonload=window.onload;
 if(typeof window.onload !="function"){
  window.onload=func;
 }
 else{
  window.onload=function(){
  if(oldonload){
   oldonload(); 
  }
  func();
  }
 }
 }
 //在頁(yè)面加載完后立即執(zhí)行多個(gè)函數(shù)方案結(jié)束。
 addloadEvent(lbt);
 //輪播圖動(dòng)畫切換原理
 function lbt(){
 var container=document.getElementById("container");
 var prev=document.getElementById("prev");
 var next=document.getElementById("next");
 var list=document.getElementById("list");
 var buttons=document.getElementById("buttons").getElementsByTagName("span");
 var imgs=list.getElementsByTagName("img");
 var index=1;
 var animated=false;
 var timer;
 //當(dāng)前圖片輪播的圓點(diǎn)變色顯示,原理:index數(shù)值是跟隨list滑動(dòng)次數(shù)遞增的,第一次index=1,所以第一個(gè)button的索引值就是0。
 //for循環(huán)是添加on樣式之前要清空之前的on。
 function showButton(){
 for(var i=0;i<buttons.length;i++){
 if(buttons[i].className=="on"){
 buttons[i].className="";
 break;
 }
 }
 buttons[index-1].className="on";
 }
 //圓點(diǎn)變色顯示 結(jié)束。
 function animate(offset){
 animated=true;
 var newLeft=parseInt(list.style.left)+offset;//當(dāng)前的偏移量+下一次的偏移量=新的偏移量
 var time=300;//位移總時(shí)間
 var interval=10;//位移間隔時(shí)間
 //動(dòng)畫效果自定義公式: 每次位移的距離=單次偏移距離/位移次數(shù)
 var speed=offset/(time/interval);
 //遞歸函數(shù) 直到不滿足條件(跳到輔助圖)
 //遞歸就是把600偏移量分為多次完成偏移
 function go(){
 //speed<0 并且 當(dāng)前偏移量>下一次偏移量 就是向左偏移 || 反之向右偏移 
 if ((speed<0 &&parseInt(list.style.left)>newLeft) || (speed>0 &&parseInt(list.style.left)<newLeft)) {
 list.style.left=parseInt(list.style.left)+speed+"px";//每次位移的值
 setTimeout(go,interval);//10毫秒再次調(diào)用go函數(shù)
 }else{
 animated=false;
 list.style.left=newLeft+"px";//當(dāng)前的偏移值=新的偏移值
 if(newLeft>-600){
 list.style.left=-3000+"px";
 }
 if (newLeft<-3000){
 list.style.left=-600+"px";
 }
 }
 }
 go();
 }
 //自動(dòng)播放3秒執(zhí)行一次next.onclick
 function play(){
 timer=setInterval(function(){
 next.onclick();
 },3000);
 }
 function stop(){
 clearInterval(timer);
 }
 //執(zhí)行所有函數(shù)
 next.onclick=function(){
 if(index==5){
 index=1;
 }else{
 index+=1;
 }
 showButton();
 if(!animated){
 animate(-600);
 }
 }
 //執(zhí)行所有函數(shù)
 prev.onclick=function(){
 if(index==1){
 index=5;
 }else{
 index-=1;
 }
 showButton();
 if(!animated){
 animate(600);
 }
 }
 //點(diǎn)擊圓點(diǎn)按鈕 偏移
 for(var i=0;i<buttons.length;i++){
 buttons[i].onclick=function(){
 if(this.className=="on"){
 return;
 }
 //要點(diǎn)擊的index屬性的值 轉(zhuǎn)換為整數(shù)
 var myIndex=parseInt(this.getAttribute("index"));
 //偏移量=-600*(要點(diǎn)擊的位置-當(dāng)前所在的位置),當(dāng)前的位置就是index循環(huán)所得
 var os=-600*(myIndex-index);
 //切換完成后,把點(diǎn)擊的index位置變成當(dāng)前的index位置 
 index=myIndex;
 showButton();
 if(!animated){
 animate(os);
 }
 }
 }
 container.onmouseover=stop;
 container.onmouseout=play;
 play();
 }
 </script>
</body> 
</html> 

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)時(shí)間軸特效

    微信小程序?qū)崿F(xiàn)時(shí)間軸特效

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)時(shí)間軸特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • JavaScript實(shí)現(xiàn)分頁(yè)效果

    JavaScript實(shí)現(xiàn)分頁(yè)效果

    本文主要介紹了JavaScript實(shí)現(xiàn)分頁(yè)效果的示例代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • 微信小程序利用co處理異步流程的方法教程

    微信小程序利用co處理異步流程的方法教程

    最近在學(xué)習(xí)微信小程序,下面就學(xué)習(xí)的內(nèi)容進(jìn)行總結(jié),這篇文章主要給大家介紹了關(guān)于微信小程序利用co處理異步流程的方法,文中給出了詳細(xì)的介紹和示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • ros::spin() 和 ros::spinOnce()函數(shù)的區(qū)別及詳解

    ros::spin() 和 ros::spinOnce()函數(shù)的區(qū)別及詳解

    這篇文章主要介紹了ros::spin() 和 ros::spinOnce()函數(shù)的區(qū)別及詳解的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感謝興趣的朋友一起看看吧
    2016-10-10
  • javascript 精確獲取頁(yè)面元素的位置

    javascript 精確獲取頁(yè)面元素的位置

    現(xiàn)在網(wǎng)上最流行方法是John Resig在《Pro JavaScript techniques》提出的offset大法,累加元素offsetParent的offsetLeft和offsetTop一直到DOM的頂層。
    2010-01-01
  • javascript等號(hào)運(yùn)算符使用詳解

    javascript等號(hào)運(yùn)算符使用詳解

    在JavaScript中,等號(hào)由雙等號(hào)(==)表示,當(dāng)且僅當(dāng)兩個(gè)運(yùn)算數(shù)相等時(shí),它返回true。今天我們就來(lái)詳細(xì)探討下等號(hào)運(yùn)算符的問(wèn)題,并附上等號(hào)運(yùn)算符和全等號(hào)運(yùn)算符的區(qū)別分析。
    2015-04-04
  • javascript刪除Table中的一行的腳本代碼

    javascript刪除Table中的一行的腳本代碼

    用js實(shí)現(xiàn)的刪除table中一行數(shù)據(jù)的代碼
    2008-06-06
  • JavaScript中內(nèi)存泄漏的介紹與教程(推薦)

    JavaScript中內(nèi)存泄漏的介紹與教程(推薦)

    內(nèi)存泄露是指一塊被分配的內(nèi)存既不能使用,又不能回收,直到瀏覽器進(jìn)程結(jié)束。下面這篇文章主要給的大家介紹了關(guān)于JavaScript中內(nèi)存泄漏的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • 詳解ES6 Promise的生命周期和創(chuàng)建

    詳解ES6 Promise的生命周期和創(chuàng)建

    這篇文章主要介紹了詳解ES6 Promise Promise的生命周期和創(chuàng)建,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享

    JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)防止網(wǎng)頁(yè)被嵌入Frame框架的代碼分享,本文給出了2種防嵌入方法,需要的朋友可以參考下
    2014-12-12

最新評(píng)論

灌云县| 大安市| 沈丘县| 鹰潭市| 广灵县| 饶阳县| 沁水县| 周口市| 阜康市| 泾川县| 赞皇县| 江陵县| 贵州省| 区。| 顺义区| 逊克县| 通州市| 湘潭市| 佛冈县| 衡东县| 东海县| 阿尔山市| 长沙县| 海林市| 外汇| 崇左市| 宣汉县| 合川市| 邹平县| 房山区| 桑植县| 伊川县| 巴楚县| 上林县| 蕲春县| 萝北县| 新疆| 彰武县| 乐业县| 务川| 大冶市|