仿照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"
		});
	},
	ajax : function(s){
		var xhr = window.ActiveXObject
				? new ActiveXObject("Microsoft.XMLHTTP")
				: new XMLHttpRequest();
				
		var type = s.type.toUpperCase();
		var time = s.time ? s.time : 10000;
		var url = s.url;
		var sendBody  = this.formatParam(s.data);
		
		var timer = setTimeout(function(){
				if ( typeof s.timeout == "function" ) s.timeout();
				if ( xhr ) {
					xhr.abort();
					xhr = null;
				}
			    return true;
			},time);
			
		if ( 'GET' == type ) {
			url = [url, ( url.indexOf('?') == -1 ? '?' : '&') ,sendBody].join('');
			sendBody = null;
		}

		if ( ! s.cache ) {
		    url = [url, ( url.indexOf('?') == -1 ? '?' : '&') , "ajaxtimestamp=" , (new Date()).getTime()].join('');
		}

		xhr.open(type,url,s.async);
		if ( "POST" == type ) xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");

		xhr.onreadystatechange = function(){
			if( xhr.readyState == 4 ){
				if ( xhr.status == 200 ){
					if ( timer ) clearTimeout(timer);
					var text = xhr.responseText;
					if (s.success) s.success(text);
				}
			}
		}
		xhr.send( sendBody );	
		
	},
	formatParam:function(data){
		if ( ! data || typeof data != "object" ) return data;
		var k,r = [];
		for ( k in data ) {
			r.push([k,'=',encodeURIComponent(data[k])].join(''));
		}
		return r.join('&');
	}
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值