⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 control.modal.js

📁 可以轻松实现ajax效果的js源码
💻 JS
📖 第 1 页 / 共 2 页
字号:
/** * @author Ryan Johnson <ryan@livepipe.net> * @copyright 2007 LivePipe LLC * @package Control.Modal * @license MIT * @url http://livepipe.net/projects/control_modal/ * @version 2.0.0 */if(typeof(Control) == "undefined")	Control = {};Control.Modal = Class.create();Object.extend(Control.Modal,{	loaded: false,	loading: false,	loadingTimeout: false,	overlay: false,	container: false,	current: false,	ie: false,	effects: {		containerFade: false,		containerAppear: false,		overlayFade: false,		overlayAppear: false	},	targetRegexp: /#(.+)$/,	imgRegexp: /\.(jpe?g|gif|png|tiff?)$/,	overlayStyles: {		position: 'absolute',		top: 0,		left: 0,		zIndex: 9998	},	disableHoverClose: false,	load: function(){		if(!Control.Modal.loaded){			Control.Modal.loaded = true;			Control.Modal.ie = !!(window.attachEvent && !window.opera);			Control.Modal.overlay = $(document.createElement('div'));			Control.Modal.overlay.id = 'modal_overlay';			Object.extend(Control.Modal.overlay.style,Control.Modal.overlayStyles);			Control.Modal.overlay.hide();			Control.Modal.container = $(document.createElement('div'));			Control.Modal.container.id = 'modal_container';			Control.Modal.container.hide();			Control.Modal.loading = $(document.createElement('div'));			Control.Modal.loading.id = 'modal_loading';			Control.Modal.loading.hide();			var body_tag = document.getElementsByTagName('body')[0];			body_tag.appendChild(Control.Modal.overlay);			body_tag.appendChild(Control.Modal.container);			body_tag.appendChild(Control.Modal.loading);			//hover handling			Control.Modal.container.observe('mouseout',function(event){				if(!Control.Modal.disableHoverClose && Control.Modal.current && Control.Modal.current.options.hover && !Position.within(Control.Modal.container,Event.pointerX(event),Event.pointerY(event)))					Control.Modal.close();			});		}	},	open: function(contents,options){		options = options || {};		if(!options.contents)			options.contents = contents;		var modal_instance = new Control.Modal(false,options);		modal_instance.open();		return modal_instance;	},	close: function(force){		if(Control.Modal.current)			Control.Modal.current.close(force);	},	attachEvents: function(){		Event.observe(window,'load',Control.Modal.load);		Event.observe(window,'unload',Event.unloadCache,false);	},	center: function(element){		if(!element._absolutized){			element.setStyle({				position: 'absolute'			}); 			element._absolutized = true;		}		var dimensions = element.getDimensions();		Position.prepare();		var offset_left = (Position.deltaX + Math.floor((Control.Modal.getWindowWidth() - dimensions.width) / 2));		var offset_top = (Position.deltaY + Math.floor((Control.Modal.getWindowHeight() - dimensions.height) / 2));		element.setStyle({			top: ((dimensions.height <= Control.Modal.getWindowHeight()) ? ((offset_top != null && offset_top > 0) ? offset_top : '0') + 'px' : 0),			left: ((dimensions.width <= Control.Modal.getWindowWidth()) ? ((offset_left != null && offset_left > 0) ? offset_left : '0') + 'px' : 0)		});	},	getWindowWidth: function(){		return (self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0) - (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 ? 0 : Control.Modal.getScrollBarWidth());	},	getWindowHeight: function(){		return (self.innerHeight ||  document.documentElement.clientHeight || document.body.clientHeight || 0);	},	getDocumentWidth: function(){		return Math.min(document.body.scrollWidth,Control.Modal.getWindowWidth());	},	getDocumentHeight: function(){		return Math.max(document.body.scrollHeight,Control.Modal.getWindowHeight());	},	getScrollBarWidth: function(){		var scroller_div = document.createElement('div');		Object.extend(scroller_div.style,{			position: 'absolute',			top:'-1000px',			left: '-1000px',			width: '100px',			height: '50px',			overflow: 'hidden'		});		var inner_div = document.createElement('div');		inner_div.style.width = '100%';		inner_div.style.height = '200px';		scroller_div.appendChild(inner_div);		document.body.appendChild(scroller_div);		var withNoScroll = inner_div.offsetWidth;		scroller_div.style.overflow = 'auto';		var withScroll = inner_div.offsetWidth;		document.body.removeChild(document.body.lastChild);		return (withNoScroll - withScroll);	},	onKeyDown: function(event){		if(event.keyCode == Event.KEY_ESC)			Control.Modal.close();	}});Object.extend(Control.Modal.prototype,{	mode: '',	html: false,	href: '',	element: false,	src: false,	imageLoaded: false,	ajaxRequest: false,	initialize: function(element,options){		this.element = $(element);		this.options = {			beforeOpen: Prototype.emptyFunction,			afterOpen: Prototype.emptyFunction,			beforeClose: Prototype.emptyFunction,			afterClose: Prototype.emptyFunction,			onSuccess: Prototype.emptyFunction,			onFailure: Prototype.emptyFunction,			onException: Prototype.emptyFunction,			beforeImageLoad: Prototype.emptyFunction,			afterImageLoad: Prototype.emptyFunction,			contents: false,			loading: false, //display loading indicator			fade: false,			fadeDuration: 0.75,			image: false,			imageTemplate: new Template('<img src="#{src}" id="#{id}"/>'),			imageAutoDisplay: true,			imageCloseOnClick: true,			hover: false,			iframe: false,			iframeTemplate: new Template('<iframe src="#{href}" width="100%" height="100%" frameborder="0" id="#{id}"></iframe>'),			evalScripts: true, //for Ajax, define here instead of in requestOptions			requestOptions: {}, //for Ajax.Request			overlayDisplay: true,			overlayClassName: '',			overlayCloseOnClick: true,			containerClassName: '',			opacity: 0.3,			zIndex: 9998,			width: null,			height: null,			offsetLeft: 0, //for use with 'relative'			offsetTop: 0, //for use with 'relative'			position: 'absolute' //'absolute' or 'relative'		};		Object.extend(this.options,options || {});		var target_match = false;		var image_match = false;		if(this.element){			target_match = Control.Modal.targetRegexp.exec(this.element.href);			image_match = Control.Modal.imgRegexp.exec(this.element.href);		}		if(this.options.contents){			this.mode = 'contents';		}else if(this.options.image || image_match){			this.mode = 'image';			this.src = this.element.href;		}else if(target_match){			this.mode = 'named';			var x = $(target_match[1]);			this.html = x.innerHTML;			x.remove();			this.href = target_match[1];		}else{			this.mode = (this.options.iframe) ? 'iframe' : 'ajax';			this.href = this.element.href;		}		if(this.element){			if(this.options.hover){				this.element.observe('mouseover',this.open.bind(this));				this.element.observe('mouseout',function(event){					if(!Position.within(Control.Modal.container,Event.pointerX(event),Event.pointerY(event)))						this.close();				}.bindAsEventListener(this));			}else{				this.element.onclick = function(){					this.open();					return false;				}.bindAsEventListener(this);			}		}		var targets = Control.Modal.targetRegexp.exec(window.location);		this.position = function(){			Control.Modal.overlay.setStyle({				height: Control.Modal.getDocumentHeight() + 'px',				width: Control.Modal.getDocumentWidth() + 'px'			});			if(this.options.position == 'absolute')				Control.Modal.center(Control.Modal.container);			else{				var yx = Position.cumulativeOffset(this.element);				Control.Modal.container.setStyle({					position: 'absolute',					top: yx[1] + (typeof(this.options.offsetTop) == 'function' ? this.options.offsetTop() : this.options.offsetTop) + 'px',					left: yx[0] + (typeof(this.options.offsetLeft) == 'function' ? this.options.offsetLeft() : this.options.offsetLeft) + 'px'				});			}		}.bind(this);		if(this.mode == 'image'){			this.afterImageLoad = function(){				if(this.options.imageAutoDisplay && !window.opera)					$('modal_image').show();				this.position();				this.notify('afterImageLoad');				this.hideLoadingIndicator();			}.bind(this);		}		if(this.mode == 'named' && targets && targets[1] && targets[1] == this.href)			this.open();	},	showLoadingIndicator: function(){		if(this.options.loading){			Control.Modal.loadingTimeout = window.setTimeout(function(){				var modal_image = $('modal_image');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -