原生jsシームレスな輪播プラグインの使用について
このブログは主にどのようにオリジナルjsを使って互換性のあるIE 9+のシームレスな輪播図を実現するかについて話しています。主な知識点は以下の通りです。オブジェクト向け js最適化の節流関数 js運動
効果
html構造
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。
html構造
<div class="sliders-wraper" id="rotation-1">
<ul class="sliders clear">
<li class="slider" style="background:purple">5</li>
<li class="slider" style="background:pink">1</li>
<li class="slider" style="background:beige">2</li>
<li class="slider" style="background:gold">3</li>
<li class="slider" style="background:skyblue">4</li>
<li class="slider" style="background:purple">5</li>
<li class="slider" style="background:pink">1</li>
</ul>
<div class="pagenation">
<div class="page page-active"><a></a></div>
<div class="page"><a></a></div>
<div class="page"><a></a></div>
<div class="page"><a></a></div>
<div class="page"><a></a></div>
</div>
<span class='prev rotation-btn'><</span>
<span class='next rotation-btn'>></span>
</div>
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 //
this.timer = null //
this.animation = null //
// obj
this.mode = 'easy-in-out'// , 'linear'
this.step = 5 //
this.delay = 2500 //
this.duration = 40 //
this.auto = true //
this.btn = false //
this.focusBtn = true //
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 //
}
this.init()
}
var D = Rotation.prototype
/*
* func init
*
* @para 0
*/
D.init = function(){
this.width = this.wraper.clientWidth
if(this.mode == 'linear')
this.duration = 1
for(var i=0; i<this.length; i++)
this.slideList[i].style.width = this.width + 'px'
this.sliders.style.width = this.width * this.length + 'px'
this.sliders.style.marginLeft = -this.width + "px";
this.handler()
this.auto && this.autoPlay()
}
D.getStyle = function(obj, attr){
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj, false)[attr];
}
/*
* @method bind
*
* bind events
*/
D.handler = function(){
var _th = this,i=0
if(this.btn){
this.prev.onclick = function(){
_th.turnLeft();
}
this.next.onclick = function(){
_th.turnRight();
}
}
if(this.focusBtn){
for(; i<this.pageList.length; i++){
this.pageList[i].key = i
this.pageList[i].function(){
_th.index = this.key + 1
_th.toggleClass()
}
}
}
this.wraper.onmouseover = function(){
clearInterval(_th.animation);
}
this.wraper.onmouseout = function(){
_th.auto && _th.autoPlay()
}
}
/*
* func turnRight
*
* @para 0
*/
D.turnRight = function(){
this.index++;
if(this.index == this.length-1){
this.index=1;
this.sliders.style.marginLeft = 0;
}
this.toggleClass();
}
/*
* func turnLeft
*
* @para 0
*/
D.turnLeft = function(){
this.index--;
if(this.index == 0){
this.index = this.length-2;
this.sliders.style.marginLeft = -this.width * (this.length-1) + "px";
}
this.toggleClass();
}
/*
* func toggleClass
* ,
* @para 0
*/
D.toggleClass = function(){
if(this.focusBtn){
this.pageList[this.activePage].className = "page";
this.pageList[this.index-1].className = "page page-active";
this.activePage = this.index-1;
}
this.slide(-this.index * this.width);
}
/*
* func slide
* ,
* @param ins
*/
D.slide = function(ins){
var _th = this
//
if(_th.timer) clearInterval(_th.timer);
_th.timer = setInterval(move,_th.duration);
function move(){
var currentPosition = parseFloat(_th.sliders.style.marginLeft);
//
var n = ins-currentPosition<0?-1:1;
if(Math.abs(ins-currentPosition) < _th.step){
_th.sliders.style.marginLeft = ins + "px";
clearInterval(_th.timer);
}else{
// ,
if(_th.mode == 'easy-in-out')
_th.step = Math.abs(ins-currentPosition)/5
_th.sliders.style.marginLeft = currentPosition + n*_th.step + "px";
}
}
}
/*
* func autoPlay
*
* @para 0
*/
D.autoPlay = function(){
var _th = this
clearInterval(_th.animation)
_th.animation = setInterval(function(){
_th.turnRight();
},_th.delay)
}
/*
* func debounce
*
* @para fn< > delay< >
* @value func
*/
D.debounce = function(fn,delay){
var timer = null
return function(){
if(timer){
clearTimeout(timer)
}
timer = setTimeout(fn,delay)
}
}
/*
* func refresh
* ,
* @para 0
*/
D.refresh = function(){
var _th = this
this.debounce(function(){
_th.init()
_th.toggleClass()
},100)()
}
/*
* func rotation
*
* @para obj
* @value
*/
win.rotation = function(obj){
return new Rotation(obj)
}
})(document, window)
呼び出し方式
var r2 = rotation({
el: 'rotation-1',
mode: 'easy-in-out', //
auto: true,//
btn: true, //
focusBtn: false//
})
window.onresize = function(){
r2 && r2.refresh()
}
还有,需要问一下jQuery写真は順番に放送します JavaScript画像は順番に放送されます。 Bootstrapの写真は順番に放送します。以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。