iSlider手机端图片滑动切换插件使用详解手机端图片滑动切换插件使用详解
主要为大家详细介绍了iSlider手机端图片滑动切换插件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
自适应轮播图,支持手机触屏滑动,三种切换效果。
效果图:
js:
var iSlider = function(opts) {
if (!opts.dom) {
throw new Error("dom element can not be empty!");
}
if (!opts.data || !opts.data.length) {
throw new Error("data must be an array and must have more than one element!");
}
this._opts = opts;
this._setting();
this._renderHTML();
this._bindHandler();
};
iSlider.prototype._setting = function() {
var opts = this._opts;
this.wrap = opts.dom;
this.data = opts.data;
this.type = opts.type || 'pic';
this.isVertical = opts.isVertical || false;
this.onslide = opts.onslide;
this.onslidestart = opts.onslidestart;
this.onslideend = opts.onslideend;
this.onslidechange = opts.onslidechange;
this.duration = opts.duration || 4000;
this.log = opts.isDebug ?
function(str) {
console.log(str)
}: function() {};
this.axis = this.isVertical ? 'Y': 'X';
this.width = this.wrap.clientWidth;
this.height = this.wrap.clientHeight;
this.ratio = this.height / this.width;
this.scale = opts.isVertical ? this.height: this.width;
this.sliderIndex = this.sliderIndex || 0;
if (this.data.length < 2) {
this.isLooping = false;
this.isAutoPlay = false;
} else {
this.isLooping = opts.isLooping || false;
this.isAutoplay = opts.isAutoplay || false;
}
if (this.isAutoplay) {
this.play();
}
this._setUpDamping();
this._animateFunc = (opts.animateType in this._animateFuncs) ? this._animateFuncs[opts.animateType] : this._animateFuncs['default'];
this._setPlayWhenFocus();
};
iSlider.prototype._setPlayWhenFocus = function() {
var self = this;
window.addEventListener('focus',
function() {
self.isAutoplay && self.play();
},
false);
window.addEventListener('blur',
function() {
self.pause();
},
false);
}
iSlider.prototype._animateFuncs = {
'default': function(dom, axis, scale, i, offset) {
dom.style.webkitTransform = 'translateZ(0) translate' + axis + '(' + (offset + scale * (i - 1)) + 'px)';
},
'rotate': function(dom, axis, scale, i, offset) {
var rotateDirect = (axis == "X") ? "Y": "X";
var absoluteOffset = Math.abs(offset);
var bdColor = window.getComputedStyle(this.wrap.parentNode, null).backgroundColor;
if (this.isVertical) {
offset = -offset;
}
this.wrap.style.webkitPerspective = scale * 4;
if (i == 1) {
dom.style.zIndex = scale - absoluteOffset;
} else {
dom.style.zIndex = (offset > 0) ? (1 - i) * absoluteOffset: (i - 1) * absoluteOffset;
}
dom.style.backgroundColor = bdColor || '#333';
dom.style.position = 'absolute';
dom.style.webkitBackfaceVisibility = 'hidden';
dom.style.webkitTransformStyle = 'preserve-3d';
dom.style.webkitTransform = 'rotate' + rotateDirect + '(' + 90 * (offset / scale + i - 1) + 'deg) translateZ(' + (0.888 * scale / 2) + 'px) scale(0.888)';
},
'flip': function(dom, axis, scale, i, offset) {
var rotateDirect = (axis == "X") ? "Y": "X";
var bdColor = window.getComputedStyle(this.wrap.parentNode, null).backgroundColor;
if (this.isVertical) {
offset = -offset;
}
this.wrap.style.webkitPerspective = scale * 4;
if (offset > 0) {
dom.style.visibility = (i > 1) ? 'hidden': 'visible';
} else {
dom.style.visibility = (i < 1) ? 'hidden': 'visible';
}
dom.style.backgroundColor = bdColor || '#333';
dom.style.position = 'absolute';
dom.style.webkitBackfaceVisibility = 'hidden';
dom.style.webkitTransform = 'translateZ(' + (scale / 2) + 'px) rotate' + rotateDirect + '(' + 180 * (offset / scale + i - 1) + 'deg) scale(0.875)';
},
'depth': function(dom, axis, scale, i, offset) {
var rotateDirect = (axis == "X") ? "Y": "X";
var zoomScale = (4 - Math.abs(i - 1)) * 0.15;
this.wrap.style.webkitPerspective = scale * 4;
if (i == 1) {
dom.style.zIndex = 100;
} else {
dom.style.zIndex = (offset > 0) ? (1 - i) : (i - 1);
}
dom.style.webkitTransform = 'scale(' + zoomScale + ', ' + zoomScale + ') translateZ(0) translate' + axis + '(' + (offset + 1.3 * scale * (i - 1)) + 'px)';
},
'tear': function(dom, axis, scale, i, offset) {
var rotateDirect = (axis == "X") ? "Y": "X";
var zoomScale = 1 - (Math.abs(i - 1) * 0.2);
this.wrap.style.webkitPerspective = scale * 4;
if (i == 1) {