封装我的jQuery工具库
(function(){
function jQuery(selector){
return new jQuery.prototype.init(selector);
}
jQuery.prototype.init = function (selector){
this.length = 0;
if(selector == null){
return this;
}
if(typeof selector == 'string' && selector.indexOf('.') != -1){
var dom = document.getElementsByClassName(selector.slice(1));
}else if(typeof selector == 'string' && selector.indexOf('#') != -1){
var dom = document.getElementById(selector.slice(1))
}
if(selector instanceof Element || dom.length == undefined){
this[0] = dom || selector;
this.length++;
}else{
for(var i = 0; i < dom.length; i ++){
this[i] = dom[i];
this.length++;
}
}
}
jQuery.prototype.pushStack = function (dom){
if(dom.constructor != jQuery){
dom = jQuery(dom)
}else{
dom.prevObject = this;
}
return dom;
}
jQuery.prototype.css = function(config){
for(var i = 0; i < this.length; i ++){
for(var attr in config){
this[i].style[attr] = config[attr];
}
}
return this;
}
jQuery.prototype.get = function(num){
return num != null ? (num >= 0 ? this[num] : this[num + this.length]) : [].slice.call(this, 0);
}
jQuery.prototype.eq = function(num){
var dom = num != null ? (num >= 0 ? this[num] : this[num + this.length]) : null;
return this.pushStack(dom)
}
jQuery.prototype.add = function(selector){
var curObj = jQuery(selector);
var baseObj = this;
var newObj = jQuery();
for( var i = 0; i < curObj.length; i ++){
newObj[newObj.length++] = curObj[i];
}
for( var i = 0; i < baseObj.length; i ++){
newObj[newObj.length++] = baseObj[i];
}
this.pushStack(newObj);
return newObj;
}
jQuery.prototype.end = function(selector){
return this.prevObject;
}
jQuery.prototype.myOn = function(type, handle){
for(var i = 0; i < this.length; i ++){
if(!this[i].cacheEvent){
this[i].cacheEvent = {};
}
if(!this[i].cacheEvent[type]){
this[i].cacheEvent[type] = [handle];
}else{
this[i].cacheEvent[type].push(handle);
}
}
}
jQuery.prototype.myTrigger = function (type){
var params = arguments.length > 1 ? [].slice.call(arguments , 1) : [];
var self = this;
for (var i = 0; i < this.length; i ++){
if(this[i].cacheEvent[type]){
this[i].cacheEvent[type].forEach(function(ele, index){
ele.apply(self, params)
});
}
}
}
jQuery.prototype.myQueue = function(){
var queueObj = this;
var queueName = arguments[0] || 'fx';
var addFunc = arguments[1] || null;
var len = arguments.length;
if(len == 1){
return queueObj[0][queueName]
}
queueObj[0][queueName] == undefined ? queueObj[0][queueName] = [addFunc] : queueObj[0][queueName].push(addFunc);
return this;
}
jQuery.prototype.myDequeue = function(type){
var self = this;
var queueName = arguments[0] || 'fx';
var queueArr = this.myQueue(queueName);
var currFunc = queueArr.shift();
if(currFunc == undefined){
return;
}
var next = function(){
self.myDequeue(queueName);
}
currFunc(next);
return this;
}
jQuery.prototype.myDelay = function(duration){
var queueArr = this[0]['fx'];
queueArr.push(function(next){
setTimeout(function(){
next();
},duration)
});
return this;
}
jQuery.prototype.myAnimate = function(json, callack){
var len = this.length;
var self = this;
var baseFunc = function (next){
var times = 0;
for(var i = 0 ; i < len; i ++){
startMove(self[i], json, function(){
times ++;
if(times == len){
callack && callack();
next();
}
})
}
}
this.myQueue('fx',baseFunc);
if(this.myQueue('fx').length == 1){
this.myDequeue('fx');
}
function getStyle (obj, attr){
if(obj.currentStyle){
return obj.currentStyle[attr]
}else{
return window.getComputedStyle(obj,false)[attr]
}
}
function startMove(obj, json, callback) {
clearInterval(obj.timer);
var iSpeed = null, iCur = null;
obj.timer = setInterval(function () {
var bStop = true;
for (var attr in json) {
if (attr == 'opacity') {
iCur = parseFloat(getStyle(obj, attr)) * 100
} else {
iCur = parseFloat(getStyle(obj, attr));
}
iSpeed = (json[attr] - iCur) / 7;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(attr == 'opacity'){
obj.style.opacity = (iCur + iSpeed) / 100;
}else{
obj.style[attr] = iCur + iSpeed + 'px'
}
if( iCur != json[attr]){
bStop = false;
}
}
if(bStop) {
clearInterval(obj.timer);
typeof callback == 'function' && callback()
}
}, 30)
}
return this;
}
jQuery.myCallbacks = function(){
var options = arguments[0] || '';
var list = [];
var fireIndex = 0;
var fired = false;
var args = [];
var fire = function () {
for(; fireIndex < list.length ; fireIndex ++){
list[fireIndex].apply(window, args);
}
if (options.indexOf('once') != -1) {
list = [];
fireIndex = 0;
}
}
return {
add : function (func) {
list.push(func);
if(options.indexOf('memory') != -1 && fired){
fire();
}
return this;
},
fire : function () {
fireIndex = 0;
args = arguments;
fired = true;
fire();
}
}
}
jQuery.myDeferred = function () {
var arr = [
[
jQuery.myCallbacks('once memory'), 'done', 'resolve'
],[
jQuery.myCallbacks('once memory'), 'fail', 'reject'
],[
jQuery.myCallbacks('memory'), 'progress', 'notify'
]
];
var pendding = true;
var deferred = {};
for(var i = 0; i < arr.length; i ++){
deferred[ arr[i][1 ]] = (function (index){
return function (func) {
arr[index][0].add(func)
}
}(i));
deferred[ arr[i][2] ] = (function (index) {
return function () {
var args = arguments;
if(pendding) {
arr[index][0].fire.apply(window, args);
arr[index][2] == 'resolve' || arr[index][2] == 'reject' ? pendding = false : '';
}
}
} (i));
};
return deferred;
}
jQuery.prototype.init.prototype = jQuery.prototype;
window.$ = window.jQuery = jQuery;
}());