前端模块化之seajs

决心从java后台转作前端有些日子了,不断关注前端知识。从学习了nodejs的 require按需加载模块的思路以后感受js的世界变得好美好啊,前几天无心看到了seajs,国内大牛的做品,专为前端js模块化而设计,项目地址。遂学习了一把,而且把该项目examples的第一个例子作成了一个jQuery的插件,分享以下:javascript

1.项目目录css

HelloSeaJS                                项目目录html

              --app                         存放html文件目录前端

             --game.html java

              --sea-modules            seajs的模块目录,也是各类第三方插件目录node

        --seajquery

        --jquerycss3

             --static                        本地js,css资源文件目录web

        --applicationapi

2.具体代码

HelloSeaJS/app/game.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SeaJS</title>
</head>
<body>
<div id="container"></div>
<script type="text/javascript" src="../sea-modules/seajs/seajs/2.1.1/sea.js"></script> <!-- 引入seajs-->
<script type="text/javascript">
// Set configuration
seajs.config({
	base: "../sea-modules/",                          //配置seajs模块路径
	alias: {
	  "jquery": "jquery/jquery/1.10.1/jquery.js",     //引入jquery
	  "loverotate": "jquery/loverotate/loverotate.js" //引入自定义插件
	}
});

seajs.use("../static/application/game");                  //引入js脚本
</script>
</body>
</html>

 HelloSeaJS/static/application/game.js

define(function(require,exports,module){     //seajs的模块写法,具体参看seajs的api
     var $ = require('jquery');              //加载jquery模块,这里对应的名字是html页面里配置好了的名字
     var loverotate = require('loverotate'); //这里也是html里配置好了的

     var container = $('#container');

     container.loverotate({                  //插件使用
     	content: 'I LOVE YOU!'               //配置显示的字符
     });
});

  HelloSeaJS/sea-modules/jquery/loverotate/loverotate.js

(function($){
	$.fn.loverotate = function(options) {
		var opts = $.extend({}, $.fn.loverotate.defaults, options);
		var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

		var data = [];
		var $this = $(this);
		$this.css({
			width: 'inherit',
			height: 'auto'
		});
		var ul = $('<ul></ul>').css({
			margin: '0px',
			padding: '0px'
		});
		for(var i = 0, len = o.content.length; i < len; i++) {
			var degree = degree360();
			  	data.push(degree);
			var li = $("<li></li>").css({
				width: o.liWidth,
				backgroundColor : o.liBackgroundColor,
				textAlign: o.liTextAlign,
				color: o.liColor,
				lineHeight: o.liLineHeight,
				fontSize: o.liFontSize,
				cursor: o.liCursor,
				position: o.liPosition,
				opacity: o.liOpacity,
				transform: 'rotate('+degree+'deg)',
				top: randomNum(15)+'px',
				margin: o.liMargin,
				listStyle: 'none',
				float: 'left',
				'-webkit-transition-duration': o.liDuration, //设置动画时间
				'-moz-transition-duration': o.liDuration,
				'-o-transition-duration': o.liDuration,
				'transition-duration': o.liDuration,
			}).html(o.content.substring(i, i+1) == ' ' ? ' ': o.content.substring(i, i+1))
			  .hover(function() {
			  	$(this).fadeTo(250, 1)
			  		   .css({'transform': 'rotate(0deg)',
			  				'opacity': '1'});
			  }, function() {
			  	var lithis = $(this);
			  	setTimeout(function() {
			  		lithis.css({'transform': 'rotate('+degree360()+'deg)',
			  				'opacity': '0.5'});
			  	}, randomNum(10000));
			  });

			  ul.append(li);
		}

		return $this.append(ul);
	};

	var debug = function($obj) {
		if(window.console && window.console.log) {
			window.console.log('count: ' + $obj.size());
		}
	};

	var numberType = ['-', '+'];
     
	var degree360 = function() { //获得一个-360到+360的数
	    return numberType[Math.floor(Math.random() * 2)] + Math.floor(Math.random() * 360); 
	}

    var randomNum = function(x) { //返回一个x之内的随机数
        return Math.floor(Math.random() * x);       //floor向下取整
        // return Math.parseInt(Math.random() * x); //parseInt取整,丢弃小数部分
        // return Math.ceil(Math.random() * x);     //ceil向上取整
        // return Math.round(Math.random() * x);    //round四舍五入
    };  

	$.fn.loverotate.defaults = { //li的默认值
		content: 'I LOVE YOU!',
		liWidth: '45px',
		liBackgroundColor: '#C0E6DC',
		liTextAlign: 'center',
		liColor: '#999999',
		liLineHeight: '40px',
		liFontSize: '40px',
		liCursor: 'pointer',
		liPosition: 'relative',
		liOpacity: 0.5,
		liMargin: '0px 3px',
		liDuration: '0.8s'
	};
})(jQuery);

 自定义的jQuery插件,今天也是照着样子学着写的,不足之处欢迎指正。因为有使用css3的动画效果,因此有些低版本的浏览器是达不到效果的,你们能够本身去发挥了。

这里有一篇详细介绍jQuery插件的写法,感受思路比较清晰。

3.效果

初始时

完成时

相关文章
相关标签/搜索