一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

利用jquery插件制作Tabs切换教程

时间:2010-07-07 编辑:简简单单 来源:一聚教程网

自动轮换,ajax等主要功能,然后是dom的排列形式,这里采用传统的


  
  
hello world!

  

首先写个jquery插件的闭包,园子里这两天有个朋友写了网页特效的闭包概念,挺好的,有兴趣的朋友去看看
www.111com.net 代码如下:
(function ($) {
//code here
})(jquery);

二,插件命名,这里命名为atabs,这样绑定的时候可以用$(...).atabs(),本人英文名allen,所以用a字头命名了~
 代码如下:
$.fn.atabs = function (options) {
//api
//main function
}

三,把想好的功能写成api,供外部修改
www.111com.net 代码如下:
$.fn.atabs.defaults = {
firston: 0,
classname: 'selected',
eventname: 'all', //click,mouserover,all
loadname: '加载中...', //ajax等待字符串
fadein: 'normal',
autofade: false,
autofadetime: 3
};
var opts = $.extend({}, $.fn.atabs.defaults, options); //这里可以将外部输入的代替掉默认的值,$.extend作用详见 http://api.jquery.com/jquery.extend/,看不懂英文的直接看其中的例子就行

四,编写主体功能,说明在代码中看注释
www.111com.net 代码如下:
return this.each(function () { //这里为每个绑定dom插件
var target = $(this);
var div = target.children().not("ul,span"); //所有的tabs显示体div
var tabs = target.find('ul:eq(0) li'); //所有的tabs头部索引
function tabs() {
if ($(this).hasclass(opts.classname)) {
return false;
}
tabsshow(div, $(this));
return false;
}
function tabsshow(div, li, index) {
div.stop(true, true).hide();
//自动轮换用
if (typeof (index) == "number") {
if (li.find("a").attr("rel")) ajax(div, li);
$(div[index]).stop(true,true).fadein(opts.fadein);
tabs.stop(true, true).removeclass(opts.classname);
$(tabs[index]).stop(true, true).addclass(opts.classname);

 

//非自动轮换
else {
var tabbody = div.filter(li.find("a").attr("href"));
if (li.find("a").attr("rel")) ajax(div, li);
tabbody.stop(true,true).fadein(opts.fadein);
tabs.stop(true, true).removeclass(opts.classname);
li.stop(true, true).addclass(opts.classname);
}
}
function ajax(div, li) {  //这里是关键ajax,通过操作rel的方式实现只请求服务器一次
var href = li.find("a").attr("href");
var rel = li.find("a").attr("rel"); //ajax请求url
var i = div.filter(href); //当前div
if (rel) { //如果ajax请求url不为空,只ajax一次
i.html(opts.loadname);
$.ajax({
url: rel,
cache: false,
success: function (html) {
i.html(html);
},
error: function () {
i.html('加载错误,请重试!');
}
});
li.find("a").removeattr("rel"); //只ajax一次
}
}
if (opts.autofade) {
var index = opts.firston + 1;
setinterval(function () {
if (index >= div.length) {
index = 0;
}
tabsshow(div, $(this), index++);
}, opts.autofadetime * 1000);
}
tabs.bind(opts.eventname == 'all' ? 'click mouseo教程ver' : opts.eventname, tabs) //绑定事件
.filter(':first').trigger(opts.eventname == 'all' ? 'click' : opts.eventname); //自动触发事件
});

 

最后,将以上整合,tabs插件就诞生了,下面是全部源码
www.111com.net 代码如下:
/*
* 作者:黑曜石
*/
(function ($) {
$.fn.atabs = function (options) {
$.fn.atabs.defaults = {
firston: 0,
classname: 'selected',
eventname: 'all', //click,mouserover,all
loadname: '加载中...', //ajax等待字符串
fadein: 'normal',
autofade: false,
autofadetime: 3
};
var opts = $.extend({}, $.fn.atabs.defaults, options);
return this.each(function () {
var target = $(this);
var div = target.children().not("ul,span"); //所有的tabs显示体div
var tabs = target.find('ul:eq(0) li'); //所有的tabs头部索引
function tabs() {
if ($(this).hasclass(opts.classname)) {
return false;
}
tabsshow(div, $(this));
return false;
}
function tabsshow(div, li, index) {
div.stop(true, true).hide();
//自动轮换用
if (typeof (index) == "number") {
if (li.find("a").attr("rel")) ajax(div, li);
$(div[index]).stop(true,true).fadein(opts.fadein);
tabs.stop(true, true).removeclass(opts.classname);
$(tabs[index]).stop(true, true).addclass(opts.classname);
}
//非自动轮换
else {
var tabbody = div.filter(li.find("a").attr("href"));
if (li.find("a").attr("rel")) ajax(div, li);
tabbody.stop(true,true).fadein(opts.fadein);
tabs.stop(true, true).removeclass(opts.classname);
li.stop(true, true).addclass(opts.classname);
}
}
function ajax(div, li) {
var href = li.find("a").attr("href");
var rel = li.find("a").attr("rel"); //ajax请求url
var i = div.filter(href); //当前div
if (rel) { //如果ajax请求url不为空,只ajax一次
i.html(opts.loadname);
$.ajax({
url: rel,
cache: false,
success: function (html) {
i.html(html);
},
error: function () {
i.html('加载错误,请重试!');
}
});
li.find("a").removeattr("rel"); //只ajax一次
}
}
if (opts.autofade) {
var index = opts.firston + 1;
setinterval(function () {
if (index >= div.length) {
index = 0;
}
tabsshow(div, $(this), index++);
}, opts.autofadetime * 1000);
}
tabs.bind(opts.eventname == 'all' ? 'click mouseover' : opts.eventname, tabs) //绑定事件
.filter(':first').trigger(opts.eventname == 'all' ? 'click' : opts.eventname); //自动触发事件
});
};
})(jquery);

 

热门栏目