查了一下jq的官方插件编写文档(http://docs.jquery.com/Plugins/Authoring)以及文档中推荐的Mike Alsup写的一篇A Plugin Development Pattern。英语不是很好,但还是努力看下来(既学习到知识又能练习英语,何乐不为),照猫画虎的写了一个处女作——tabBox。
顾名思义,这个插件就是方便的产生具有tab选项卡功能“盒子”的。看图一下就明白
这样功能在网页上是非常肠炎宁个的,不论前台后台。
在这,我首先提供了3个参数用于自定义插件,
$.fn.tabBox.defaults = {
width : 260,
height : 200,
basePath : “tabBox/”
};
width和height定义“盒子”的宽度和高度,basePath用于定义使用插件的页面对插件文件夹的相对路径。这个选项的出现时不得已而为之,因为选项卡的样式中用到了图片,而必须要有一个基准路径才能正确找到图片的路径。这也是参照了一个叫jqtransform(http://www.dfc-e.com/metiers/multimedia/opensource/jqtransform/)的插件的做法,他也有个参数用于指定图片文件夹所在的位置。当然还有一种做法,就是想WebUI(http://www.jqueryui.com/)一样,样式写到css文件里,这样图片的引用就是先对与css文件的路径了,而这两个都是插件的组成部分,相对路劲是不变的。所以不用提供这个路径了。只是由于这个插件用到的样式比较少,所以没有采用这个方法。
插件的原理很简单,核心的函数就是一个render(),用于渲染出tab的样式:
$.fn.tabBox.render = function() {
$(“.tabBox”).css({
width : $.fn.tabBox.defaults.width+”px”,
height : $.fn.tabBox.defaults.height+”px”,
position : “relative”,
border : “1px #ccc solid”,
background : “url(“+$.fn.tabBox.defaults.basePath+”tabHead.gif) top left repeat-x”
});
$(“.tabBox h2”).each(function(i){
$(this).css({
width : “80px”,
height : “30px”,
position : “absolute”,
“border-top” : “none”,
cursor : “pointer”,
left : 10+(i*80),
background : “url(“+$.fn.tabBox.defaults.basePath+”tabNormal.gif) top right no-repeat”,
“text-align” : “center”,
“font-size” : “12px”,
“font-weight” : “normal”,
color : “#06c”,
“line-height” : “22px”
});
});
$(“.tabBox div”).each(function(){
$(this).css({
width : $.fn.tabBox.defaults.width+”px”,
height : ($.fn.tabBox.defaults.height-30)+”px”,










