源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

用jQuery打造TabPanel效果代码

  • 时间:2020-01-05 22:25 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:用jQuery打造TabPanel效果代码
如大量信息查看,当网页多窗口框架等都会用到,现在网上基于jquery Tab控件,其实也蛮多了,我以前用过的[url=http://www.sunsean.com/idTabs/]idtabs[/url],就比较简单实用,也是比较灵活,但是对于复杂情况就要编码多些,太简单了些。还有就是jquery UI的里面的tab控件(没用过,我对jquery ui不太感冒),另外就是近期有点小火的[url=http://www.easyui.org.cn/]easyui[/url] 中的tab控件,最早在javaeye上面看到的,界面还算漂亮,因为之前没开源,所以一直没跟进(好像最近开源了,前几天下载了看看,编码风格有点像prototype,看不出jquery的影子,不知道为什么叫jquery easyui呵呵,因为没太深入去学习,也不好做其他评价)。说了那么多,我们还是回到主题,因为种种原因不得不想着自己开发一个吧。于是就有了这篇,先来看看效果吧。 下图是单网页多窗口框架的效果图 [url=http://images.cnblogs.com/cnblogs_com/xuanye/WindowsLiveWriter/jQueryTabPanel_133AD/image_10.png][/url][img]http://files.jb51.net/upload/201005/20100522022555575.png[/img]   下图是文末提供调用示例的效果截图。 [url=http://images.cnblogs.com/cnblogs_com/xuanye/WindowsLiveWriter/jQueryTabPanel_133AD/image_8.png][/url][img]http://files.jb51.net/upload/201005/20100522022613939.png[/img]   大家可以看到了还是使用ExtJs的效果。其实CSS基本上是直接copy它的。我觉得它那个就非常好看,当然实际使用的时候大家有能力完全可以自己样子 [b]第一 我们还是从HTML开始吧[/b] [i]注:我先控件的思路始终是先确定HTML结构,其次是样式,最终才是js实现的事件方法等。[/i] 其实看图我们就可以基本确定,tab控件主要有两个部分的html 一个是头,用于放tab选项卡的;另外一个是体,是内容的容器。那么就是两个Div容器,讲tab控件分成了header和body两部分。 其中header部分因为包含多选项卡 所以很容易想到ul +li的配合。来看一下header中的实际html结构 [url=http://images.cnblogs.com/cnblogs_com/xuanye/WindowsLiveWriter/jQueryTabPanel_133AD/image_12.png][/url][img]http://files.jb51.net/upload/201005/20100522022624407.png[/img]   通过通过其中li即是一个选项卡,第一个a是关闭按钮,第二个a才是实际内容 通过嵌套标签来实现 左右中的背景图片设置(这个做法比较多见的)。当然能够有个好的效果,还是要靠CSS支持。必须对CSS有一定的了解。 Body的结构则更简单就是div嵌套div就就结束了。 [b]第二 CSS样式表[/b] 因为CSS是copy EXTJS的我也就不多介绍了。大家可以看代码下载里面的实际代码,如果有问题可以再沟通交流 [b]第三:开始编写JS了[/b] 老规矩先来一段完整的JS代码,大概有500行左右的代码,其实我换行比较勤快,实际的代码量其实还是比较少。
[url=#]parray.push("<em class='x-tab-left'><span class='x-tab-strip-inner'><span class='x-tab-strip-text ",item.classes||"","'>",item.text,"</span></span></em>"); parray.push("</a></li>"); } function builditembodyhtml(item,parray) { parray.push("<div class='x-panel x-panel-noborder",item.isactive?"":" x-hide-display","' id='tab_item_",item.id,"' style='width:",innerwidth,"px'>"); parray.push("<div class='x-panel-bwrap'>"); parray.push("<div class='x-panel-body x-panel-body-noheader x-panel-body-noborder' id='tab_item_content_",item.id,"' style='position:relative; width:",innerwidth,"px; height:",bodyheight,"px; overflow: auto;'>"); if(item.url){ parray.push("<iframe name='tab_item_frame_",item.id,"' width='100%' height='100%' id='tab_item_frame_",item.id,"' src='about:blank' frameBorder='0' />"); } else if(item.cuscall){ parray.push("<div class='loadingicon'/>"); } else{ parray.push(item.content); } parray.push("</div></div></div>"); } function initevents() { //reset scoller resetscoller(); scollerclick(); ulwrap.find("li:not(.x-tab-edge)").each(function(e){ inititemevents(this); }); } function inititemevents(liitem) { liswaphover.call(liitem); liclick.call(liitem); closeitemclick.call(liitem); } function scollerclick() { if(dfop.autoscroll) { scrollerleft.click(function(e){scolling("left")}); scrollerright.click(function(e){scolling("right")}); } } function resetscoller() { if(dfop.autoscroll) { var edge = ulwrap.find("li.x-tab-edge"); var eleft =edge.position().left; var sleft = stripwrap.attr("scrollLeft"); if( sleft+eleft>innerwidth ) { header.addClass("x-tab-scrolling"); scrollerleft.css("visibility","visible"); scrollerright.css("visibility","visible"); if(sleft>0) { scrollerleft.removeClass("x-tab-scroller-left-disabled"); } else{ scrollerleft.addClass("x-tab-scroller-left-disabled"); } if(eleft>innerwidth) { scrollerright.removeClass("x-tab-scroller-right-disabled"); } else{ scrollerright.addClass("x-tab-scroller-right-disabled"); } dfop.showscrollnow =true; } else { header.removeClass("x-tab-scrolling"); stripwrap.animate({"scrollLeft":0},"fast"); scrollerleft.css("visibility","hidden"); scrollerright.css("visibility","hidden"); dfop.showscrollnow =false; } } } // function scolling(type,max) { //debugger; if(!dfop.autoscroll || !dfop.showscrollnow) { return; } //debugger; //var swidth = stripwrap.attr("scrollWidth"); var sleft = stripwrap.attr("scrollLeft"); var edge = ulwrap.find("li.x-tab-edge"); var eleft = edge.position().left ; if(type=="left"){ if(scrollerleft.hasClass("x-tab-scroller-left-disabled")) { return; } if(sleft-dfop.scrollwidth-20>0) { sleft -=dfop.scrollwidth; } else{ sleft =0; scrollerleft.addClass("x-tab-scroller-left-disabled"); } if(scrollerright.hasClass("x-tab-scroller-right-disabled")) { scrollerright.removeClass("x-tab-scroller-right-disabled"); } stripwrap.animate({"scrollLeft":sleft},"fast"); } else{ if(scrollerright.hasClass("x-tab-scroller-right-disabled") && !max) { return; } //left + ; if(max || (eleft>innerwidth && eleft-dfop.scrollwidth-20<=innerwidth)) { //debugger; sleft = sleft+eleft-(innerwidth-38) ; scrollerright.addClass("x-tab-scroller-right-disabled"); // sleft = eleft-innerwidth; } else { sleft +=dfop.scrollwidth; } if(sleft>0) { if(scrollerleft.hasClass("x-tab-scroller-left-disabled")) { scrollerleft.removeClass("x-tab-scroller-left-disabled"); } } stripwrap.animate({"scrollLeft":sleft},"fast"); } } function scollingToli(liitem) { var sleft = stripwrap.attr("scrollLeft"); var lleft = liitem.position().left; var lwidth = liitem.outerWidth(); var edge = ulwrap.find("li.x-tab-edge"); var eleft = edge.position().left ; if(lleft<=0) { sleft +=(lleft-2) ; if(sleft<0) { sleft=0; scrollerleft.addClass("x-tab-scroller-left-disabled"); } if(scrollerright.hasClass("x-tab-scroller-right-disabled")) { scrollerright.removeClass("x-tab-scroller-right-disabled"); } stripwrap.animate({"scrollLeft":sleft},"fast"); } else{ if(lleft+lwidth>innerwidth-40) { sleft = sleft+lleft+lwidth+-innerwidth+40; // 40 =scrollerleft and scrollerrightwidth; if(scrollerleft.hasClass("x-tab-scroller-left-disabled")) { scrollerleft.removeClass("x-tab-scroller-left-disabled"); } //滚到最后一个了,那么就要禁用right; if(eleft-(lleft+lwidth+-innerwidth+40)<=innerwidth) { scrollerright.addClass("x-tab-scroller-right-disabled"); } stripwrap.animate({"scrollLeft":sleft},"fast"); } } liitem.click(); } function liswaphover() { $(this).hover(function(e){ if(!$(this).hasClass("x-tab-strip-disabled")) { $(this).addClass("x-tab-strip-over"); } },function(e){ if(!$(this).hasClass("x-tab-strip-disabled")) { $(this).removeClass("x-tab-strip-over"); } }); } function closeitemclick() { if($(this).hasClass("x-tab-strip-closable")) { $(this).find("a.x-tab-strip-close").click(function(){ deleteitembyliid($(this).parent().attr("id")); }); } } function liclick() { $(this).click(function(e){ var itemid = this.id.substr(7); var curr = getactiveitem(); if( curr !=null && itemid == curr.id) { return; } var clickitem = getitembyid(itemid); if(clickitem && clickitem.disabled) { return ; } if(curr) { $("#tab_li_"+curr.id).removeClass("x-tab-strip-active"); $("#tab_item_"+curr.id).addClass("x-hide-display"); curr.isactive =false; } if(clickitem) { $(this).addClass("x-tab-strip-active"); $("#tab_item_"+clickitem.id).removeClass("x-hide-display"); if(clickitem.url) { var cururl = $("#tab_item_frame_"+clickitem.id).attr("src"); if(cururl =="about:blank") { $("#tab_item_frame_"+clickitem.id).attr("src",clickitem.url); } } else if(clickitem.cuscall && !clickitem.cuscalled) { var panel = $("#tab_item_content_"+clickitem.id); var ret = clickitem.cuscall(this,clickitem,panel); clickitem.cuscalled =true; if(ret) //如果存在返回值,且不为空 { clickitem.content = ret; panel.html(ret); } } clickitem.isactive =true; if(clickitem.onactive) { clickitem.onactive.call(this,clickitem); } } }); } //获取当前活跃项 function getactiveitem() { for(var i=0,j=dfop.items.length;i<j ;i++) { if(dfop.items[i].isactive) { return dfop.items[i]; break; } } return null; } //根据ID获取Item数据 function getitembyid(id) { for(var i=0,j=dfop.items.length;i<j ;i++) { if(dfop.items[i].id == id) { return dfop.items[i]; break; } } return null; } function getIndexbyId(id) { for(var i=0,j=dfop.items.length;i<j ;i++) { if(dfop.items[i].id == id) { return i; break; } } return -1; } //添加项 function addtabitem(item) { var chkitem =getitembyid(item.id); if(!chkitem){ var isactive =item.isactive; item.isactive =false; var lastitem = dfop.items[dfop.items.length-1]; dfop.items.push(item); var lastli = $("#tab_li_"+lastitem.id); var lastdiv = $("#tab_item_"+lastitem.id); var litemp =[]; var bodytemp = []; builditemlihtml(item,litemp); builditembodyhtml(item,bodytemp); var liitem = $(litemp.join("")); var bodyitem= $(bodytemp.join("")); lastli.after(liitem); lastdiv.after(bodyitem); //事件 var li = $("#tab_li_"+item.id); inititemevents(li); if(isactive) { li.click(); } resetscoller(); scolling("right",true); } else{ alert("指定的tab项已存在!"); } } function openitemOrAdd(item,allowAdd) { var checkitem = getitembyid(item.id); if(!checkitem && allowAdd ) { addtabitem(item); } else{ var li = $("#tab_li_"+item.id); scollingToli(li); } } //移除一个tab 项 function deleteitembyliid(liid) { var id= liid.substr(7); $("#"+liid).remove(); $("#tab_item_"+id).remove(); var index = getIndexbyId(id); if(index>=0) { var nextcur; if(index < dfop.items.length -1) { nextcur = dfop.items[index+1]; } else if(index>0){ nextcur = dfop.items[index-1]; } if(nextcur) { $("#tab_li_"+nextcur.id).click(); } dfop.items.splice(index,1); resetscoller(); scolling("right",true); } } function resize(width,height) { if(width ==dfop.width && height ==dfop.height) { return; } if(width){ dfop.width=width}; if(height){ dfop.height =height;} innerwidth = width-2; bodyheight=dfop.height-headerheight; me.css("width",dfop.width); header.css("width",innerwidth); body.css({width:innerwidth,height:bodyheight}); for(var i=0,j=dfop.items.length;i<j;i++) { var item =dfop.items[i]; $("#tab_item_"+item.id).css({width:innerwidth}); $("#tab_item_content_"+item.id).css({width:innerwidth,height:bodyheight}); } resetscoller(); } //设置选项卡项是否disabled function setdisabletabitem(itemId,disabled) { var chitem= getitembyid(itemId); if(!chitem || chitem.disabled ==disabled) { return; } if(disabled) { chitem.disabled =true; $("#tab_item_"+item.id).addClass("x-tab-strip-disabled"); } else{ chitem.disabled =false; $("#tab_item_"+item.id).removeClass("x-tab-strip-disabled"); } } me[0].tab = { addtabitem:addtabitem, opentabitem:openitemOrAdd, resize:resize, setdisabletabitem:setdisabletabitem }; }; $.fn.addtabitem =function(item) { if(this[0].tab) { return this[0].tab.addtabitem(item); } return false; } $.fn.opentabitem =function(item,orAdd) { if(this[0].tab) { return this[0].tab.opentabitem(item,orAdd); } return false; } $.fn.resizetabpanel =function(w,h) { if(this[0].tab) { return this[0].tab.resize(w,h); } return false; } $.fn.setdisabletabitem =function(itemId,disabled) { if(this[0].tab) { return this[0].tab.setdisabletabitem(itemId,disabled); } return false; } })(jQuery);
接着我们来一步一步来分析我的实现,开始还是编写jQuery控件的“模板”,关于为什么要这么写,请参考这篇的说明 接着就是编写默认参数
[url=http://code.google.com/p/xjplugin/downloads/list]http://code.google.com/p/xjplugin/downloads/list[/url] [url=http://xiazai.jb51.net/201005/yuanma/xjPlugin_addtabpanel.rar]http://xiazai.jb51.net/201005/yuanma/xjPlugin_addtabpanel.rar[/url]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部