日期:2014-05-16  浏览次数:20573 次

仿照Jquery风格写的一段拖拽,Ajax等代码

?

window.$=function(selector){
	return new $.fn.init(selector);
}
$.fn = $.prototype = {
	init : function(selector){
	
		selector = selector || document;
		if (selector.nodeType) {
			this[0] = selector;
			this.length = 1;
			return this;
		}
		if(typeof selector == "string"){
			return $(document.getElementById(selector));
		}
	},
	html : function(value) {
		return value == undefined ? (this[0] ? this[0].innerHTML : null) : (this[0] ? this[0].innerHTML = value : this);
	},
	css : function(s){
		for ( var name in s ) {
			this[0].style[ name ] = s[ name ];
		}
	},
	hide : function(speed,func){
		speed = speed || 10;
		this.css({filter:'alpha(opacity=100)',opacity:1});
		var self_ = this;
		this.alpha({start:10,end:0,speed:speed},function(){
			self_.css({display:"none"});
			if(func) func();
		});
	},
	show : function(speed,func){
		speed = speed || 10;
		this.css({display:""});
		this.alpha({start:0,end:10,speed:speed},func);
	},
	alpha : function(op,func){
		var self_ = this;
		var n = op.start;
		var obj = this[0];
		var end = op.end;
		var step = op.start > op.end ? -1:1;
		var alpha = function(){
			n += step;
			self_.css({'filter':'alpha(opacity=' + n*10 + ')','opacity':n/10});
			if( n != end ){
				window.setTimeout(alpha,op.speed);
			}else{
				if(func) func();
			}
		};
		alpha();
	},
	val : function(value){
		return value == undefined ? (this[0] ? this[0].value : null) : (this[0] ? this[0].value = value : this);
	},
	drag : function(){
		var div = this[0];
		var mouseD = false;
		var mx = 0,my = 0;
		var alphaFlg = false;
		div.onmousedown = function(e){
			var e = e ? e : event;
			if(e.button == (document.all ? 1 : 0)){
				mouseD = true;
				mx = e.x ? e.x : e.pageX;
				my = e.y ? e.y : e.pageY;
				$(div).css({'left':this.offsetLeft + "px",'top':this.offsetTop + "px"});
				if( $.isIE ){
				    this.setCapture();
				}else{
				    window.captureEvents(Event.MOUSEMOVE);
				}
				$(div).css({'filter':'alpha(opacity=80)','opacity':0.8});
			}
		};
		div.onmousemove = function(e){
			var e = e ? e : event;
			if( mouseD == true ){
				var emX = e.x ? e.x : e.pageX;
				var emY = e.y ? e.y : e.pageY;
				var mrx = emX - mx;
				var mry = emY - my;
				$(div).css({left:parseInt(div.style.left) + mrx + "px",
								  top:parseInt(div.style.top) + mry + "px"});
				mx = emX;
				my = emY;
				
			}
		};
		div.onmouseup = function(){
			mouseD = false;
			alphaFlg = false;
			if( $.isIE ){
				div.releaseCapture();
			}else{
				window.releaseEvents(div.MOUSEMOVE);
			} 
			$(div).css({'filter':'alpha(opacity=100)','opacity':1});
		};
	}
	
	
}
$.fn.init.prototype = $.fn;
	
$.extend = $.fn.extend = function() {

	var target = arguments[0] || {}, 
	i = 1, length = arguments.length, deep = false, options;

	if (target.constructor == Boolean) {
		deep = target;
		target = arguments[1] || {};
		i = 2;
	}

	if (typeof target != "object" && typeof target != "function")
		target = {};

	if (length == i) {
		target = this;
		--i;
	}

	for (;i < length; i++)
		if ((options = arguments[i]) != null)

			for (var name in options) {
				var src = target[name], copy = options[name];
				if (target === copy)
					continue;

				if (deep && copy && typeof copy == "object" && !copy.nodeType)
					target[name] = jQuery.extend(deep, src
							|| (copy.length != null ? [] : {}), copy);
				else if (copy !== undefined)
					target[name] = copy;

			}

	return target;
};

$.extend({
	isIE : !-[1,],
	isArray : function(v){
	    return toString.apply(v) === '[object Array]';
	},
	isFunction : function(fn) {
		return fn instanceof Function;
	}

});

// AJAX
$.extend({
	get : function(url,data,callback){
		return $.ajax({
			url : url,
			data : data,
			success : callback,
			type : "GET"
		});
	},
	post : function(url,data,callback){
		return $.ajax({
			url : url,
			data :data,
			success : callback,
			type : "POST"
		});
	},
	a