記一個簡單js插件封裝,怕忘了😂git
封裝仍是先須要瞭解一下js原型prototypegithub
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// COMMONJS
module.exports = factory();
} else {
// BROWSER
root.YScroll = factory();
}
}(this, function () {···
}));
複製代碼
var defaults = {
wrapper: '#YScroll',
autoPlay: false,
interTime: 2500,
prevCell: ".prev",
nextCell: ".next",
arrow: ".arrow",
sizeDatas: [],
centerX: 0,
centerY: 0,
a: 0, //半徑:寬
b: 0, //半徑:高
style: 1, //展現的樣式 1:默認 2:兩側對稱
};
var extend = function () {···
};
// 插件的承載對象
var YScroll = function () {
this.wrapper = '';
this.CardNumber = 0; //生成點的個數, 根據外部的li個數來肯定
this.arc = 360; //弧度: 默認360
this.cardWidth = this.cardHeight = 0, //卡片寬高
this.sizeDatas = [];
};
複製代碼
YScroll.prototype = {
conBox: null,
nextBtn: null,
prevBtn: null,
arrowD: null,
timer: null,
init: function (options) {···
},
animate: function () {···
},
nextCad: function () {···
},
prevCad: function () {···
},
doPlay: function () {/*自動播放*/···
},
applyEllipse: function () {···
},
attachEvent: function () {···
}
}
複製代碼
函數很簡明,這裏主要總結一下:算法
var wapperJQ = $(this.wrapper); self.conBox = $("ul li", wapperJQ);
self.conBox.eq(i).stop().animate(data, 500);
nextCad: function () {
var first = YScroll.sizeDatas.shift();
YScroll.sizeDatas.push(first);
YScroll.animate();
},
prevCad: function () {
var last = this.sizeDatas.pop();
this.sizeDatas.unshift(last);
this.animate();
},
複製代碼
var hudu = (Math.PI / 180) * (i * pnC),
x1 = this.settings.centerX - this.settings.a * Math.sin(hudu),
y1 = this.settings.centerY + (this.settings.b * Math.cos(hudu)),
複製代碼
zIndex = factor ? this.CardNumber - i : i,
elopacity = 1 - 0.02 * xCode, //透明度
複製代碼
switch (this.settings.style) {
case 1: //尺寸係數 --1 默認
{
scaleX = 1 - xCode * (factor ? cose1 *= 0.9 : cose1 /= 0.92);
}
break;
case 2://尺寸係數 --2 -左右對稱 i太大 (圓弧描點 )會致使尺寸太小
{
scaleX = 1 - xCode * 0.12;
.2>scaleX && (scaleX = lastScale);
lastScale = scaleX;//防止尺寸過小
}
}
複製代碼
附上github地址3D-Yscroll數組
總的來講代碼不存在難度,將就着用了bash