js用構(gòu)造函數(shù)封裝輪播圖

用過(guò)swiper的人都知道,swiper給我們提供了許多不同形式的輪播圖暗赶,它們都是用面向?qū)ο蟮男问椒庋b的彤避。本人嘗試了用構(gòu)造函數(shù)封裝出同樣的效果,封裝的不好大家見(jiàn)諒麦乞。

首先html代碼如下:

<div class="box swiper">

? ? <ul class="big">

? ? ? ? <li style="background:#00a0af">

? ? ? ? <li style="background:#82a6af">

? ? ? ? <li style="background:#21af41">

? ? ? ? <li style="background:#af46ad">

? ? ? ? <li style="background:#619eaf">

? ? <ul class="small">

? ? <div class="left"> <

? ? <div class="right"> >

</div>

css代碼如下:


.box{

width:300px;

? ? height:200px;

}

.swiper{

overflow:hidden;

? ? position:relative;

? ? margin:0 auto;

}

.big{

? ? position:absolute;

}

.big li{

? ? float:left;

}

.small{

height:20px;

? ? position:absolute;

}

.small li{

? ? margin-left:10px;

? ? background:grey;

? ? float:left;

}

.small li.active{

background:#0F0F0F;

}

div{

width:40px;

? ? height:40px;

? ? background:#0a6cd6;

? ? position:absolute;

}

最重要的js代碼如下:

首先創(chuàng)建一個(gè)構(gòu)造函數(shù)swiper

function Swiper(box,json) {

this.box=document.querySelector(box);

? ? this.bul=document.querySelector(box+' .big');

? ? this.bli=document.querySelectorAll(box+' .big li');

? ? this.sul=document.querySelector(box+' .small');

? ? this.sleft=document.querySelector(box+' .left');

? ? this.sright=document.querySelector(box+' .right');? ? //獲取元素

? ? this.timer=null;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//定時(shí)器

? ? this.iNow=0;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//計(jì)數(shù)器

? ? this.json=json || {};? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//判斷有沒(méi)有傳參

? ? this.auto=this.json.autoplay ||'true';? ? ? ? ? ? ? ? ? ? ? ? ?//是否自動(dòng)輪播

? ? this.time=this.json.time ||2000;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //切換時(shí)間

? ? this.event=this.json.event ||'click';? ? ? ? ? ? ? ? ? ? ? ? ? ? //切換事件

? ? this.init();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//初始化

}

Swiper.prototype.init=function () {

let boxwidth=this.box.offsetWidth;

? ? let boxheight=this.box.offsetHeight;

? ? this.bul.style.width=this.bli.length*boxwidth+'px';

? ? this.sul.style.width=this.bli.length*30+'px';

? ? this.sul.style.top=boxheight-this.sul.offsetHeight+'px';

? ? this.sul.style.left=(boxwidth-this.sul.offsetWidth)/2+'px';

? ? this.sleft.style.top=(boxheight-this.sleft.offsetHeight)/2+'px';

? ? this.sleft.style.left=10+'px';

? ? this.sright.style.top=(boxheight-this.sright.offsetHeight)/2+'px';

? ? this.sright.style.right=10+'px';? ? ? ? ? ? ? ? //以上為根據(jù)box的大小調(diào)節(jié)圓點(diǎn)和左右鍵的大小和位置

? ? for(let i=0;i<this.bli.length;i++){

this.bli[i].style.width=boxwidth+'px';

? ? ? ? this.bli[i].style.height=boxheight+'px';

? ? ? ? let oLi=document.createElement('li');

? ? ? ? this.sul.appendChild(oLi);

? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //根據(jù)圖片的個(gè)數(shù)創(chuàng)建對(duì)應(yīng)的幾個(gè)圓點(diǎn)

this.sli=document.querySelectorAll('.box .small li');? ? ?//獲取圓點(diǎn)

? ? for(let i=0;i<this.sli.length;i++){

this.sli[i].style.width=20+'px';

? ? ? ? this.sli[i].style.height=20+'px';

? ? }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //設(shè)置寬高

this.sli[0].className='active';

? ? var _this=this;? ? ? ? ? ? ? ? ? ? ? ? //在事件內(nèi)使用this的話蕴茴,this指向的是最外層? 的div,所以要先讓? ? ? ? ? ????????????????????????????????????????????指向swiper的this賦值給_this,這樣使用就不會(huì)出問(wèn)題了

? ? if(this.auto=='true'){

this.autoplay();? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//如果有傳參為true的話姐直,則一開(kāi)始就自動(dòng)輪播

? ? }

this.box.onmouseenter=function(){

_this.enter();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //鼠標(biāo)移入時(shí)倦淀,執(zhí)行函數(shù)

? ? };

? ? this.box.onmouseleave=function(){

_this.leave();?????????????????????????????????//鼠標(biāo)移出時(shí),執(zhí)行函數(shù)

? ? };

? ? this.sleft.onclick=function () {? ? ? //點(diǎn)擊左方向鍵時(shí)

_this.iNow--;

? ? ? ? if(_this.iNow<0){

_this.iNow=_this.bli.length-1;

? ? ? ? }

_this.move();

? ? }

this.sright.onclick=function () {? ? ? ? ?//點(diǎn)擊右方向鍵時(shí)

_this.iNow++;

? ? ? ? if(_this.iNow==_this.bli.length){

_this.iNow=0;

? ? ? ? }

_this.move();

? ? }

for(let i=0;i<this.sli.length;i++){? ? ? ? ? ? ? ?//給每一個(gè)圓點(diǎn)加點(diǎn)擊事件

this.sli[i]['on'+this.event]=function () {

_this.iNow=i;

? ? ? ? ? ? _this.move();

? ? ? ? }

}

}

Swiper.prototype.autoplay=function () {

var _this=this;

? ? this.timer=setInterval(function () {

_this.iNow++;

? ? ? ? if(_this.iNow==_this.bli.length){

_this.iNow=0;

? ? ? ? }

_this.move();

? ? },this.time);

}

Swiper.prototype.move=function () {

for(var i=0;i

this.sli[i].className='';

? ? }

this.sli[this.iNow].className='active';

? ? this.bul.style.left=-this.iNow*this.bli[0].offsetWidth+'px';

}

Swiper.prototype.enter=function () {

clearInterval(this.timer);

}

Swiper.prototype.leave=function () {

this.autoplay();

}

最后我們調(diào)用構(gòu)造函數(shù)時(shí)可以

?new Swiper('.box',{

? ? ?autoplay:'false', //true

? ? ?time:1000,//1000

? ? ?event:'mouseover',//click,

?});

也可以不傳參直接new Swiper('.box');

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末声畏,一起剝皮案震驚了整個(gè)濱河市撞叽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌插龄,老刑警劉巖能扒,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異辫狼,居然都是意外死亡初斑,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)膨处,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)见秤,“玉大人,你說(shuō)我怎么就攤上這事真椿【榇穑” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵突硝,是天一觀的道長(zhǎng)测摔。 經(jīng)常有香客問(wèn)我,道長(zhǎng)解恰,這世上最難降的妖魔是什么锋八? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮护盈,結(jié)果婚禮上挟纱,老公的妹妹穿的比我還像新娘。我一直安慰自己腐宋,他們只是感情好紊服,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布檀轨。 她就那樣靜靜地躺著,像睡著了一般欺嗤。 火紅的嫁衣襯著肌膚如雪参萄。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,258評(píng)論 1 300
  • 那天煎饼,我揣著相機(jī)與錄音拧揽,去河邊找鬼。 笑死腺占,一個(gè)胖子當(dāng)著我的面吹牛淤袜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播衰伯,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼铡羡,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了意鲸?” 一聲冷哼從身側(cè)響起烦周,我...
    開(kāi)封第一講書(shū)人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎怎顾,沒(méi)想到半個(gè)月后读慎,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡槐雾,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年夭委,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片募强。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡株灸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出擎值,到底是詐尸還是另有隱情慌烧,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布鸠儿,位于F島的核電站屹蚊,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏进每。R本人自食惡果不足惜汹粤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望品追。 院中可真熱鬧玄括,春花似錦冯丙、人聲如沸肉瓦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)泞莉。三九已至哪雕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鲫趁,已是汗流浹背斯嚎。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留挨厚,地道東北人堡僻。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像疫剃,于是被迫代替她去往敵國(guó)和親钉疫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容