<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        js實現輪播圖的兩種方式(構造函數、面向對象)

        來源:懂視網 責編:小OO 時間:2020-11-27 22:28:23
        文檔

        js實現輪播圖的兩種方式(構造函數、面向對象)

        本文實例為大家分享了js實現輪播圖的具體代碼,供大家參考,具體內容如下:1、構造函數。<,0,0,index++ picImg[index].style.display='none';tabLi[index].className='';index++;index%=tabLi.length;picImg[index].style.display='block';tabLi[index].className='on';},2000) };<;/script>;<;/body>;<;/html>。2、面向對象;
        推薦度:
        導讀本文實例為大家分享了js實現輪播圖的具體代碼,供大家參考,具體內容如下:1、構造函數。<,0,0,index++ picImg[index].style.display='none';tabLi[index].className='';index++;index%=tabLi.length;picImg[index].style.display='block';tabLi[index].className='on';},2000) };<;/script>;<;/body>;<;/html>。2、面向對象;

        本文實例為大家分享了js實現輪播圖的具體代碼,供大家參考,具體內容如下

        1、構造函數

        <!DOCTYPE html>
        <html>
         <head>
         <meta charset="UTF-8">
         <title></title>
         <style type='text/css'>
         *{ margin:0; padding:0;}
         
         #wrap{
         width:500px;
         height:360px;
         margin:100px auto;
         position:relative;
         }
        
         #pic{
         width:500px;
         height:360px;
         position:relative;
         }
        
         #pic img{
         width: 100%;
         height: 100%;
         position:absolute;
         top:0;
         left:0;
         display:none;
         }
        
         #tab{
         width:105px;
         height:10px;
         position:absolute;
         bottom:10px;
         left:50%;
         margin-left:-50px;
         }
        
         #tab ul li{
         width:10px;
         height:10px;
         margin:0 5px;
         background:#bbb;
         border-radius:100%;
         cursor:pointer;
         list-style:none;
         float:left;
         }
         #tab ul li.on{ background:#f60;}
        
         #btn div{
         width:40px;
         height:40px;
         position:absolute;
         top:50%;
         margin-top:-20px;
         color:#fff;
         background:#999;
         background:rgba(0,0,0,.5);
         font-size:20px;
         font-weight:bold;
         font-family:'Microsoft yahei';
         line-height:40px;
         text-align:center;
         cursor:pointer;
         }
         #btn div#left{ left:0;}
         #btn div#right{ right:0;}
        
         </style>
         </head>
         <body>
         <div id="wrap">
         <div id="pic">
         <img src="img/1.jpg" alt="" />
         <img src="img/2.jpg" alt="" />
         <img src="img/3.jpg" alt="" />
         <img src="img/4.jpg" alt="" />
         </div>
         <div id="tab">
         <ul>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
         </ul>
         </div>
         <div id="btn">
         <div id='left'><</div>
         <div id='right'>></div>
         </div>
         </div>
         <script>
         var oWrap=document.getElementById('wrap')
         var picImg=document.getElementById('pic').getElementsByTagName('img');
         var tabLi=document.getElementById('tab').getElementsByTagName('li');
         var btnDiv=document.getElementById('btn').getElementsByTagName('div');
         var index=0;
         var timer=null;//設置一個timer變量,讓他的值為空
         //初始化
         picImg[0].style.display='block';
         tabLi[0].className='on';
         
         for(var i=0;i<tabLi.length;i++){
        
         tabLi[i].index=i; 
         tabLi[i].onclick=function(){
         
         //不然要for循環清空
         /* for(var i=0;i<tabLi.length;i++){
         picImg[i].style.display='none'; 
         tabLi[i].className='';
         }*/
         picImg[index].style.display='none'; //每個li都有index自定義屬性
         tabLi[index].className='';
         index=this.index;
         picImg[index].style.display='block';
         tabLi[index].className='on';
         
         } 
         };
         for(var i=0;i<btnDiv.length;i++){
        
         btnDiv[i].index=i;
         btnDiv[i].onselectstart=function(){ //禁止選擇
         return false;
         }
         btnDiv[i].onclick=function(){
         
         picImg[index].style.display='none'; //每個li都有index自定義屬性
         tabLi[index].className='';
         //index=this.index;
         if(this.index){
         index++; //進來就加1,index就相當1%4 2%4 3%4 4%4
         //if(index>tabLi.length){index=0}
         //index=index%arrUrl.length; 自己取模自己等于0 alert(3%3) == 0 
         index%=tabLi.length;//相當于當大于tabLi.length就等于0
         }else{
         index--;
         if(index<0)index=tabLi.length-1; 
         } 
         picImg[index].style.display='block';
         tabLi[index].className='on';
         
         } 
         };
         auto();
         oWrap.onmouseover=function(){
         clearInterval(timer)
         }
         oWrap.onmouseleave=function(){
         auto();
         }
         function auto(){
         timer=setInterval(function(){ //一般都是向左輪播,index++
         picImg[index].style.display='none';
         tabLi[index].className='';
         index++;
         index%=tabLi.length;
         picImg[index].style.display='block';
         tabLi[index].className='on';
         },2000)
         };
         </script>
         </body>
        </html>
        


        2、面向對象

        <!DOCTYPE html>
        <html>
         <head>
         <meta charset="UTF-8">
         <title></title>
         <style type='text/css'>
         *{ margin:0; padding:0;}
         
         #wrap{
         width:500px;
         height:360px;
         margin:100px auto;
         position:relative;
         }
        
         #pic{
         width:500px;
         height:360px;
         position:relative;
         }
        
         #pic img{
         width: 100%;
         height: 100%;
         position:absolute;
         top:0;
         left:0;
         display:none;
         }
        
         #tab{
         width:105px;
         height:10px;
         position:absolute;
         bottom:10px;
         left:50%;
         margin-left:-50px;
         }
        
         #tab ul li{
         width:10px;
         height:10px;
         margin:0 5px;
         background:#bbb;
         border-radius:100%;
         cursor:pointer;
         list-style:none;
         float:left;
         }
         #tab ul li.on{ background:#f60;}
        
         #btn div{
         width:40px;
         height:40px;
         position:absolute;
         top:50%;
         margin-top:-20px;
         color:#fff;
         background:#999;
         background:rgba(0,0,0,.5);
         font-size:20px;
         font-weight:bold;
         font-family:'Microsoft yahei';
         line-height:40px;
         text-align:center;
         cursor:pointer;
         }
         #btn div#left{ left:0;}
         #btn div#right{ right:0;}
        
         </style>
         </head>
         <body>
         <div id="wrap">
         <div id="pic">
         <img src="img/1.jpg" alt="" />
         <img src="img/2.jpg" alt="" />
         <img src="img/3.jpg" alt="" />
         <img src="img/4.jpg" alt="" />
         </div>
         <div id="tab">
         <ul>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
         </ul>
         </div>
         <div id="btn">
         <div id='left'><</div>
         <div id='right'>></div>
         </div>
         </div>
         <script>
         var oWrap=document.getElementById('wrap')
         var picImg=document.getElementById('pic').getElementsByTagName('img');
         var tabLi=document.getElementById('tab').getElementsByTagName('li');
         var btnDiv=document.getElementById('btn').getElementsByTagName('div');
         
         function Banner(oWrap,picImg,tabLi,btnDiv){
         this.wrap=oWrap
         this.list=picImg
         this.tab=tabLi
         this.btn=btnDiv
         this.index=0; //這些都必須是私有的,不然兩個banner會一樣
         this.timer=null;
         this.length=this.tab.length;
         
         // this.init();//下面創建好,要在這里執行
         
         }
         
         //初始化分類
         Banner.prototype.init=function(){ //先把下面的分類
         var This=this; //var 一個This變量把this存起來
         this.list[0].style.display='block';
         this.tab[0].className='on';
         
         for(var i=0;i<this.length;i++){
         this.tab[i].index=i; 
         this.tab[i].onclick=function(){
         //this.list[index].style.display='none'; 這里的this指向tab的this 
         This.list[This.index].style.display='none'; 
         This.tab[This.index].className='';
         //index=this.index;
         This.index=this.index;
         This.list[This.index].style.display='block';
         //This.tab[This.index].className='on'; 
         this.className='on';
         } 
         };
         
         for(var i=0;i<this.btn.length;i++){
         this.btn[i].index=i;
         this.btn[i].onselectstart=function(){ 
         return false;
         }
         this.btn[i].onclick=function(){
         This.list[This.index].style.display='none'; 
         This.tab[This.index].className='';
         if(this.index){
         This.index++;
         This.index%=This.length; 
         }else{
         This.index--;
         if(index<0)This.index=This.length-1; 
         } 
         This.list[This.index].style.display='block';
         This.tab[This.index].className='on'; 
         }
         }
         this.auto();
         this.clear(); 
         };
         Banner.prototype.auto=function(){
         var This=this; 
        
         This.timer=setInterval(function(){ //一般都是向左輪播,index++
         This.list[This.index].style.display='none';
         This.tab[This.index].className='';
         This.index++;
         This.index%=This.length;
         This.list[This.index].style.display='block';
         This.tab[This.index].className='on';
         },2000)
         };
         
         Banner.prototype.clear=function(){
         var This=this; 
         this.wrap.onmouseover=function(){
         clearInterval(This.timer)
         }
         this.wrap.onmouseleave=function(){
         This.auto();
         } 
         };
         
         
         var banner1=new Banner(oWrap,picImg,tabLi,btnDiv);
         banner1.init();
         
         /*
         * init()
         * function init(){
         for(var i=0;i<tabLi.length;i++){
         tabLi[i].index=i; 
         tabLi[i].onclick=function(){
         picImg[index].style.display='none'; 
         tabLi[index].className='';
         index=this.index;
         picImg[index].style.display='block';
         tabLi[index].className='on'; 
         } 
         };
         
         
         }
         for(var i=0;i<btnDiv.length;i++){
         btnDiv[i].index=i;
         btnDiv[i].onselectstart=function(){ 
         return false;
         }
         btnDiv[i].onclick=function(){
         picImg[index].style.display='none'; 
         tabLi[index].className='';
         if(this.index){
         index++;
         index%=tabLi.length;
         }else{
         index--;
         if(index<0)index=tabLi.length-1; 
         } 
         picImg[index].style.display='block';
         tabLi[index].className='on';
         } 
         };
         auto();
         oWrap.onmouseover=function(){
         clearInterval(timer)
         }
         oWrap.onmouseleave=function(){
         auto();
         }
         function auto(){
         timer=setInterval(function(){ //一般都是向左輪播,index++
         picImg[index].style.display='none';
         tabLi[index].className='';
         index++;
         index%=tabLi.length;
         picImg[index].style.display='block';
         tabLi[index].className='on';
         },2000)
         };
         
         */
         </script>
         </body>
        </html>
        


        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        js實現輪播圖的兩種方式(構造函數、面向對象)

        本文實例為大家分享了js實現輪播圖的具體代碼,供大家參考,具體內容如下:1、構造函數。<,0,0,index++ picImg[index].style.display='none';tabLi[index].className='';index++;index%=tabLi.length;picImg[index].style.display='block';tabLi[index].className='on';},2000) };<;/script>;<;/body>;<;/html>。2、面向對象;
        推薦度:
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲精品电影在线| 亚洲 暴爽 AV人人爽日日碰| 亚洲午夜未满十八勿入网站2| 亚洲国产精品久久久久| 亚洲人成网站在线在线观看| AAAAA级少妇高潮大片免费看| 18禁男女爽爽爽午夜网站免费| 啊灬啊灬别停啊灬用力啊免费看| 亚洲AV成人无码久久精品老人| 亚洲精品国产综合久久久久紧 | 亚洲欧洲日产国码av系列天堂| 成人免费网站视频www| 黄+色+性+人免费| 亚洲自偷自偷偷色无码中文| 亚洲精品欧美综合四区| 成人免费男女视频网站慢动作| 亚洲国产精品成人久久| 无人视频在线观看免费播放影院| 国产在线观看免费观看不卡| 亚洲第一福利网站| 四虎永久在线观看免费网站网址| 亚洲欧美乱色情图片| 国产一区二区三区在线免费 | 暖暖免费高清日本一区二区三区| 久久久亚洲AV波多野结衣| 很黄很污的网站免费| 国产美女亚洲精品久久久综合| 国产午夜无码精品免费看| 亚洲午夜在线电影| 国产在线国偷精品产拍免费| 亚洲美女aⅴ久久久91| 中文字幕免费在线看线人动作大片 | 大地资源在线观看免费高清 | 国产亚洲精品成人久久网站| 最近的中文字幕大全免费版| 成a人片亚洲日本久久| 久久国产免费一区| 亚洲国产人成精品| 亚洲综合综合在线| 日本免费一区二区三区最新| 国产精品99爱免费视频|