這篇文章給大家分享的是有關(guān)原生js無(wú)縫輪播插件怎么用的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁(yè)設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開(kāi)發(fā)技術(shù)和網(wǎng)頁(yè)設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)建站專業(yè)提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站制作(企業(yè)站、響應(yīng)式網(wǎng)站設(shè)計(jì)、電商門戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!
效果
html結(jié)構(gòu)
css樣式
*{margin: 0;padding: 0;box-sizing: border-box;} .clear{zoom: 0;} .clear:after{content: '';display: block;overflow: hidden;clear: both;widows: 0;height: 0;} .sliders-wraper{width: 100%;height: 400px;line-height: 400px; overflow: hidden;position: relative;text-align: center;} .sliders{position: absolute;list-style: none;font-size: 50px;} .slider{float: left;} .rotation-btn{position: absolute;top: 50%;width: 50px;height: 50px; line-height: 50px;margin-top: -25px;font-size: 30px;color: #ccc;cursor: pointer;} .prev{left:0;} .next{right:0;} .pagenation{position: absolute;bottom: 10px;width: 100%;height: 25px;line-height: 25px;} .pagenation .page{width: 40px;height: 25px;display: inline-block;cursor: pointer;} .pagenation .page a{display: block;width: 30px;height: 5px;border: 1px solid #ccc; border-radius: 5px;background: transparent;margin: 10px auto;} .pagenation .page-active a{border-color: #0076ff;background-color: #0076ff;}
js
;(function(doc, win){ function Rotation(obj){ this.wraper = doc.getElementById(obj.el) //窗口 this.sliders = this.wraper.getElementsByClassName('sliders')[0] //圖片父盒子 this.slideList = this.sliders.getElementsByClassName('slider') //所有圖片 this.length = this.slideList.length this.index = 1 //當(dāng)前顯示的圖片的索引 this.timer = null //單張圖片運(yùn)動(dòng)定時(shí)器 this.animation = null //自動(dòng)輪播定時(shí)器 // 在obj中可以自定義的參數(shù) this.mode = 'easy-in-out'//動(dòng)畫曲線,可選 'linear' this.step = 5 //勻速運(yùn)動(dòng)滾動(dòng)步長(zhǎng) this.delay = 2500 //輪播間隔 this.duration = 40 //單張圖片動(dòng)畫時(shí)長(zhǎng) this.auto = true //是否開(kāi)啟自動(dòng)輪播 this.btn = false //是否有左右按鈕 this.focusBtn = true //是否支持焦點(diǎn)事件 for(var k in obj) this[k] = obj[k] if(this.btn){ this.prev = this.wraper.getElementsByClassName('prev')[0] this.next = this.wraper.getElementsByClassName('next')[0] } if(this.focusBtn){ this.pagenation = this.wraper.getElementsByClassName('pagenation')[0] this.pageList = this.pagenation.getElementsByClassName('page') this.activePage = 0 //當(dāng)前的焦點(diǎn)的索引 } this.init() } var D = Rotation.prototype /* * func init * 初始化函數(shù) * @para 0 */ D.init = function(){ this.width = this.wraper.clientWidth if(this.mode == 'linear') this.duration = 1 for(var i=0; idelay<節(jié)流時(shí)間> * @value func */ D.debounce = function(fn,delay){ var timer = null return function(){ if(timer){ clearTimeout(timer) } timer = setTimeout(fn,delay) } } /* * func refresh * 自動(dòng)刷新函數(shù),這里采用節(jié)流是防止刷新操作太過(guò)于頻繁導(dǎo)致性能下降 * @para 0 */ D.refresh = function(){ var _th = this this.debounce(function(){ _th.init() _th.toggleClass() },100)() } /* * func rotation * 實(shí)例化函數(shù) * @para obj 實(shí)例化的具體參數(shù) * @value 返回具體實(shí)例 */ win.rotation = function(obj){ return new Rotation(obj) } })(document, window)
調(diào)用方式
var r2 = rotation({ el: 'rotation-1', mode: 'easy-in-out', //運(yùn)動(dòng)曲線 auto: true,//開(kāi)啟自動(dòng)輪播 btn: true, //左右按鈕 focusBtn: false//焦點(diǎn) }) window.onresize = function(){ r2 && r2.refresh() }
感謝各位的閱讀!關(guān)于“原生js無(wú)縫輪播插件怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!