JavaScript模块化开发

index.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#div1{ width:200px; height:200px; background:red; position:absolute; display:none;}
#div2{ width:30px; height:30px; background:yellow; position:absolute; right:0; bottom:0;}
#div3{ width:100px; height:100px; background:blue; position:absolute; right:0; top:0;}
</style>
<script src="../sea/sea.js"></script>
<script>

//A开发

seajs.use('./main.js');

</script>
</head>

<body>
<input type="button" value="弹窗" id="input1">
<div id="div1">
	<div id="div2"></div>
</div>
<div id="div3"></div>
</body>
</html>

main.js

// JavaScript Document

//A开发

define(function(require,exports,module){
	
	var oInput = document.getElementById('input1');
	var oDiv1 = document.getElementById('div1');
	var oDiv2 = document.getElementById('div2');
	var oDiv3 = document.getElementById('div3');
	
	require('./drag.js').drag(oDiv3);
	
	oInput.onclick = function(){
		
		oDiv1.style.display = 'block';
		
		require('./scale.js').scale(oDiv1,oDiv2);
		
	};
	
});
// JavaScript Document

//B开发

define(function(require,exports,module){
	
	function drag(obj){
		var disX = 0;
		var disY = 0;
		
		obj.onmousedown = function(ev){
			
			var ev = ev || window.event;
			disX = ev.clientX - obj.offsetLeft;
			disY = ev.clientY - obj.offsetTop;
			
			document.onmousemove = function(ev){
				var ev = ev || window.event;
				
				var L = ev.clientX - disX;
				var T = ev.clientY - disY;
				
				//L = require('./range.js').range(L , document.documentElement.clientWidth - obj.offsetWidth , 0);
				//T = require('./range.js').range(T , document.documentElement.clientHeight - obj.offsetHeight , 0);
				
				obj.style.left = L + 'px';
				obj.style.top = T + 'px';
			};
			
			document.onmouseup = function(){
				document.onmousemove = null;
				document.onmouseup = null;
			};
			
			return false;
			
		};
		
	}
	
	exports.drag = drag;
	
});
// JavaScript Document

//C开发

define(function(require,exports,module){
	
	function scale(obj1,obj2){
		
		var downX = 0;
		var downY = 0;
		var downW = 0;
		var downH = 0;
		
		obj2.onmousedown = function(ev){
			var ev = ev || window.event;
			downX = ev.clientX;
			downY = ev.clientY;
			downW = obj1.offsetWidth;
			downH = obj1.offsetHeight;
			
			document.onmousemove = function(ev){
				var ev = ev || window.event;
				
				var W =  ev.clientX - downX + downW;
				var H = ev.clientY - downY + downH;
				
				W = require('./range.js').range(W , 500 , 100);
				H = require('./range.js').range(H , 500 , 100);
				
				obj1.style.width = W + 'px';
				obj1.style.height = H + 'px';
			};
			
			document.onmouseup = function(){
				document.onmousemove = null;
				document.onmouseup = null;
			};
			
			return false;
			
		};
		
	}
	
	exports.scale = scale;
	
});
// JavaScript Document

define(function(require,exports,module){
	
	function range(val , max , min){
		
		if( val > max ){
			return max;
		}
		else if( val < min ){
			return min;
		}
		else{
			return val;
		}
		
	}
	
	exports.range = range;
	
});

sea.js

/*
 SeaJS - A Module Loader for the Web
 v1.3.0 | seajs.org | MIT Licensed
*/
this.seajs={_seajs:this.seajs};seajs.version="1.3.0";seajs._util={};seajs._config={debug:"",preload:[]};
(function(a){var b=Object.prototype.toString,d=Array.prototype;a.isString=function(a){return"[object String]"===b.call(a)};a.isFunction=function(a){return"[object Function]"===b.call(a)};a.isRegExp=function(a){return"[object RegExp]"===b.call(a)};a.isObject=function(a){return a===Object(a)};a.isArray=Array.isArray||function(a){return"[object Array]"===b.call(a)};a.indexOf=d.indexOf?function(a,b){return a.indexOf(b)}:function(a,b){for(var c=0;c<a.length;c++)if(a[c]===b)return c;return-1};var c=a.forEach=
d.forEach?function(a,b){a.forEach(b)}:function(a,b){for(var c=0;c<a.length;c++)b(a[c],c,a)};a.map=d.map?function(a,b){return a.map(b)}:function(a,b){var d=[];c(a,function(a,c,e){d.push(b(a,c,e))});return d};a.filter=d.filter?function(a,b){return a.filter(b)}:function(a,b){var d=[];c(a,function(a,c,e){b(a,c,e)&&d.push(a)});return d};var e=a.keys=Object.keys||function(a){var b=[],c;for(c in a)a.hasOwnProperty(c)&&b.push(c);return b};a.unique=function(a){var b={};c(a,function(a){b[a]=1});return e(b)};
a.now=Date.now||function(){return(new Date).getTime()}})(seajs._util);(function(a){a.log=function(){if("undefined"!==typeof console){var a=Array.prototype.slice.call(arguments),d="log";console[a[a.length-1]]&&(d=a.pop());if("log"!==d||seajs.debug)if(console[d].apply)console[d].apply(console,a);else{var c=a.length;if(1===c)console[d](a[0]);else if(2===c)console[d](a[0],a[1]);else if(3===c)console[d](a[0],a[1],a[2]);else console[d](a.join(" "))}}}})(seajs._util);
(function(a,b,d){function c(a){a=a.match(p);return(a?a[0]:".")+"/"}function e(a){f.lastIndex=0;f.test(a)&&(a=a.replace(f,"$1/"));if(-1===a.indexOf("."))return a;for(var b=a.split("/"),c=[],d,e=0;e<b.length;e++)if(d=b[e],".."===d){if(0===c.length)throw Error("The path is invalid: "+a);c.pop()}else"."!==d&&c.push(d);return c.join("/")}function l(a){var a=e(a),b=a.charAt(a.length-1);if("/"===b)return a;"#"===b?a=a.slice(0,-1):-1===a.indexOf("?")&&!k.test(a)&&(a+=".js");0<a.indexOf(":80/")&&(a=a.replace(":80/",
"/"));return a}function g(a){if("#"===a.charAt(0))return a.substring(1);var c=b.alias;if(c&&r(a)){var d=a.split("/"),e=d[0];c.hasOwnProperty(e)&&(d[0]=c[e],a=d.join("/"))}return a}function i(a){return 0<a.indexOf("://")||0===a.indexOf("//")}function m(a){return"/"===a.charAt(0)&&"/"!==a.charAt(1)}function r(a){var b=a.charAt(0);return-1===a.indexOf("://")&&"."!==b&&"/"!==b}var p=/.*(?=\/.*$)/,f=/([^:\/])\/\/+/g,k=/\.(?:css|js)$/,o=/^(.*?\w)(?:\/|$)/,j={},d=d.location,q=d.protocol+"//"+d.host+function(a){"/"!==
a.charAt(0)&&(a="/"+a);return a}(d.pathname);0<q.indexOf("\\")&&(q=q.replace(/\\/g,"/"));a.dirname=c;a.realpath=e;a.normalize=l;a.parseAlias=g;a.parseMap=function(d){var f=b.map||[];if(!f.length)return d;for(var n=d,m=0;m<f.length;m++){var h=f[m];if(a.isArray(h)&&2===h.length){var g=h[0];if(a.isString(g)&&-1<n.indexOf(g)||a.isRegExp(g)&&g.test(n))n=n.replace(g,h[1])}else a.isFunction(h)&&(n=h(n))}i(n)||(n=e(c(q)+n));n!==d&&(j[n]=d);return n};a.unParseMap=function(a){return j[a]||a};a.id2Uri=function(a,
d){if(!a)return"";a=g(a);d||(d=q);var e;i(a)?e=a:0===a.indexOf("./")||0===a.indexOf("../")?(0===a.indexOf("./")&&(a=a.substring(2)),e=c(d)+a):e=m(a)?d.match(o)[1]+a:b.base+"/"+a;return l(e)};a.isAbsolute=i;a.isRoot=m;a.isTopLevel=r;a.pageUri=q})(seajs._util,seajs._config,this);
(function(a,b){function d(a,c){a.οnlοad=a.οnerrοr=a.onreadystatechange=function(){p.test(a.readyState)&&(a.οnlοad=a.οnerrοr=a.onreadystatechange=null,a.parentNode&&!b.debug&&i.removeChild(a),a=void 0,c())}}function c(b,c){j||q?(a.log("Start poll to fetch css"),setTimeout(function(){e(b,c)},1)):b.οnlοad=b.οnerrοr=function(){b.οnlοad=b.οnerrοr=null;b=void 0;c()}}function e(a,b){var c;if(j)a.sheet&&(c=!0);else if(a.sheet)try{a.sheet.cssRules&&(c=!0)}catch(d){"NS_ERROR_DOM_SECURITY_ERR"===d.name&&(c=
!0)}setTimeout(function(){c?b():e(a,b)},1)}function l(){}var g=document,i=g.head||g.getElementsByTagName("head")[0]||g.documentElement,m=i.getElementsByTagName("base")[0],r=/\.css(?:\?|$)/i,p=/loaded|complete|undefined/,f,k;a.fetch=function(b,e,j){var g=r.test(b),h=document.createElement(g?"link":"script");j&&(j=a.isFunction(j)?j(b):j)&&(h.charset=j);e=e||l;"SCRIPT"===h.nodeName?d(h,e):c(h,e);g?(h.rel="stylesheet",h.href=b):(h.async="async",h.src=b);f=h;m?i.insertBefore(h,m):i.appendChild(h);f=null};
a.getCurrentScript=function(){if(f)return f;if(k&&"interactive"===k.readyState)return k;for(var a=i.getElementsByTagName("script"),b=0;b<a.length;b++){var c=a[b];if("interactive"===c.readyState)return k=c}};a.getScriptAbsoluteSrc=function(a){return a.hasAttribute?a.src:a.getAttribute("src",4)};a.importStyle=function(a,b){if(!b||!g.getElementById(b)){var c=g.createElement("style");b&&(c.id=b);i.appendChild(c);c.styleSheet?c.styleSheet.cssText=a:c.appendChild(g.createTextNode(a))}};var o=navigator.userAgent,
j=536>Number(o.replace(/.*AppleWebKit\/(\d+)\..*/,"$1")),q=0<o.indexOf("Firefox")&&!("onload"in document.createElement("link"))})(seajs._util,seajs._config,this);(function(a){var b=/(?:^|[^.$])\brequire\s*\(\s*(["'])([^"'\s\)]+)\1\s*\)/g;a.parseDependencies=function(d){var c=[],e,d=d.replace(/^\s*\/\*[\s\S]*?\*\/\s*$/mg,"").replace(/^\s*\/\/.*$/mg,"");for(b.lastIndex=0;e=b.exec(d);)e[2]&&c.push(e[2]);return a.unique(c)}})(seajs._util);
(function(a,b,d){function c(a,b){this.uri=a;this.status=b||0}function e(a,d){return b.isString(a)?c._resolve(a,d):b.map(a,function(a){return e(a,d)})}function l(a,t){var e=b.parseMap(a);v[e]?(f[a]=f[e],t()):q[e]?u[e].push(t):(q[e]=!0,u[e]=[t],c._fetch(e,function(){v[e]=!0;var d=f[a];d.status===j.FETCHING&&(d.status=j.FETCHED);n&&(c._save(a,n),n=null);s&&d.status===j.FETCHED&&(f[a]=s,s.realUri=a);s=null;q[e]&&delete q[e];if(d=u[e])delete u[e],b.forEach(d,function(a){a()})},d.charset))}function g(a,
b){var c=a(b.require,b.exports,b);void 0!==c&&(b.exports=c)}function i(a){var c=a.realUri||a.uri,d=k[c];d&&(b.forEach(d,function(b){g(b,a)}),delete k[c])}function m(a){var c=a.uri;return b.filter(a.dependencies,function(a){h=[c];if(a=r(f[a]))h.push(c),b.log("Found circular dependencies:",h.join(" --\> "),void 0);return!a})}function r(a){if(!a||a.status!==j.SAVED)return!1;h.push(a.uri);a=a.dependencies;if(a.length){var c=a.concat(h);if(c.length>b.unique(c).length)return!0;for(c=0;c<a.length;c++)if(r(f[a[c]]))return!0}h.pop();
return!1}function p(a){var b=d.preload.slice();d.preload=[];b.length?w._use(b,a):a()}var f={},k={},o=[],j={FETCHING:1,FETCHED:2,SAVED:3,READY:4,COMPILING:5,COMPILED:6};c.prototype._use=function(a,c){b.isString(a)&&(a=[a]);var d=e(a,this.uri);this._load(d,function(){p(function(){var a=b.map(d,function(a){return a?f[a]._compile():null});c&&c.apply(null,a)})})};c.prototype._load=function(a,d){function e(a){(a||{}).status<j.READY&&(a.status=j.READY);0===--i&&d()}var x=b.filter(a,function(a){return a&&
(!f[a]||f[a].status<j.READY)}),g=x.length;if(0===g)d();else for(var i=g,h=0;h<g;h++)(function(a){function b(){d=f[a];if(d.status>=j.SAVED){var t=m(d);t.length?c.prototype._load(t,function(){e(d)}):e(d)}else e()}var d=f[a]||(f[a]=new c(a,j.FETCHING));d.status>=j.FETCHED?b():l(a,b)})(x[h])};c.prototype._compile=function(){function a(b){b=e(b,c.uri);b=f[b];if(!b)return null;if(b.status===j.COMPILING)return b.exports;b.parent=c;return b._compile()}var c=this;if(c.status===j.COMPILED)return c.exports;
if(c.status<j.SAVED&&!k[c.realUri||c.uri])return null;c.status=j.COMPILING;a.async=function(a,b){c._use(a,b)};a.resolve=function(a){return e(a,c.uri)};a.cache=f;c.require=a;c.exports={};var d=c.factory;b.isFunction(d)?(o.push(c),g(d,c),o.pop()):void 0!==d&&(c.exports=d);c.status=j.COMPILED;i(c);return c.exports};c._define=function(a,d,g){var h=arguments.length;1===h?(g=a,a=void 0):2===h&&(g=d,d=void 0,b.isArray(a)&&(d=a,a=void 0));!b.isArray(d)&&b.isFunction(g)&&(d=b.parseDependencies(g.toString()));
var h={id:a,dependencies:d,factory:g},i;if(document.attachEvent){var m=b.getCurrentScript();m&&(i=b.unParseMap(b.getScriptAbsoluteSrc(m)));i||b.log("Failed to derive URI from interactive script for:",g.toString(),"warn")}if(m=a?e(a):i){if(m===i){var k=f[i];k&&(k.realUri&&k.status===j.SAVED)&&(f[i]=null)}h=c._save(m,h);if(i){if((f[i]||{}).status===j.FETCHING)f[i]=h,h.realUri=i}else s||(s=h)}else n=h};c._getCompilingModule=function(){return o[o.length-1]};c._find=function(a){var c=[];b.forEach(b.keys(f),
function(d){if(b.isString(a)&&-1<d.indexOf(a)||b.isRegExp(a)&&a.test(d))d=f[d],d.exports&&c.push(d.exports)});return c};c._modify=function(b,c){var d=e(b),i=f[d];i&&i.status===j.COMPILED?g(c,i):(k[d]||(k[d]=[]),k[d].push(c));return a};c.STATUS=j;c._resolve=b.id2Uri;c._fetch=b.fetch;c._save=function(a,d){var i=f[a]||(f[a]=new c(a));i.status<j.SAVED&&(i.id=d.id||a,i.dependencies=e(b.filter(d.dependencies||[],function(a){return!!a}),a),i.factory=d.factory,i.status=j.SAVED);return i};var q={},v={},u=
{},n=null,s=null,h=[],w=new c(b.pageUri,j.COMPILED);a.use=function(b,c){p(function(){w._use(b,c)});return a};a.define=c._define;a.cache=c.cache=f;a.find=c._find;a.modify=c._modify;c.fetchedList=v;a.pluginSDK={Module:c,util:b,config:d}})(seajs,seajs._util,seajs._config);
(function(a,b,d){var c="seajs-ts="+b.now(),e=document.getElementById("seajsnode");e||(e=document.getElementsByTagName("script"),e=e[e.length-1]);var l=e&&b.getScriptAbsoluteSrc(e)||b.pageUri,l=b.dirname(function(a){if(a.indexOf("??")===-1)return a;var c=a.split("??"),a=c[0],c=b.filter(c[1].split(","),function(a){return a.indexOf("sea.js")!==-1});return a+c[0]}(l));b.loaderDir=l;var g=l.match(/^(.+\/)seajs\/[\.\d]+(?:-dev)?\/$/);g&&(l=g[1]);d.base=l;d.main=e&&e.getAttribute("data-main");d.charset=
"utf-8";a.config=function(e){for(var g in e)if(e.hasOwnProperty(g)){var l=d[g],p=e[g];if(l&&g==="alias")for(var f in p){if(p.hasOwnProperty(f)){var k=l[f],o=p[f];/^\d+\.\d+\.\d+$/.test(o)&&(o=f+"/"+o+"/"+f);k&&k!==o&&b.log("The alias config is conflicted:","key =",'"'+f+'"',"previous =",'"'+k+'"',"current =",'"'+o+'"',"warn");l[f]=o}}else if(l&&(g==="map"||g==="preload")){b.isString(p)&&(p=[p]);b.forEach(p,function(a){a&&l.push(a)})}else d[g]=p}if((e=d.base)&&!b.isAbsolute(e))d.base=b.id2Uri((b.isRoot(e)?
"":"./")+e+"/");if(d.debug===2){d.debug=1;a.config({map:[[/^.*$/,function(a){a.indexOf("seajs-ts=")===-1&&(a=a+((a.indexOf("?")===-1?"?":"&")+c));return a}]]})}if(d.debug)a.debug=!!d.debug;return this};d.debug&&(a.debug=!!d.debug)})(seajs,seajs._util,seajs._config);
(function(a,b,d){a.log=b.log;a.importStyle=b.importStyle;a.config({alias:{seajs:b.loaderDir}});b.forEach(function(){var a=[],e=d.location.search,e=e.replace(/(seajs-\w+)(&|$)/g,"$1=1$2"),e=e+(" "+document.cookie);e.replace(/seajs-(\w+)=[1-9]/g,function(b,d){a.push(d)});return b.unique(a)}(),function(b){a.use("seajs/plugin-"+b);"debug"===b&&(a._use=a.use,a._useArgs=[],a.use=function(){a._useArgs.push(arguments);return a})})})(seajs,seajs._util,this);
(function(a,b,d){var c=a._seajs;if(c&&!c.args)d.seajs=a._seajs;else{d.define=a.define;b.main&&a.use(b.main);if(b=(c||0).args)for(var c={"0":"config",1:"use",2:"define"},e=0;e<b.length;e+=2)a[c[b[e]]].apply(a,b[e+1]);d.define.cmd={};delete a.define;delete a._util;delete a._config;delete a._seajs}})(seajs,seajs._config,this);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值