JQuery标签页效果实例详解

第一个标签页中鼠标滑过显示不同的标签页,第二个标签页中点击不同标签加载其他页面中的内容,加载等待的图片缓慢隐藏,效果图如下:

JQuery标签页效果实例详解

/WebRoot/4.Tab.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JQuery实例4:标签页效果</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" href="https://www.jb51.net/css/tab.css" /> <script type="text/javascript" src="https://www.jb51.net/js/jquery.js"></script> <script type="text/javascript" src="https://www.jb51.net/js/tab.js"></script> </head> <body> <ul> <li>标签1</li> <li>标签2</li> <li>标签3</li> </ul> <div>我是内容1</div> <div>我是内容2</div> <div>我是内容3</div> <br /> <br /> <br /> <ul> <li>装入完整页面</li> <li>装入部分页面</li> <li>从远程获取数据</li> </ul> <div> <img alt="装载中" src="https://www.jb51.net/images/img-loading.gif" /> <div></div> </div> </body> </html>

/WebRoot/js/tab.js:

var timoutid; $(document).ready(function(){ //找到所有的标签 /* $("li").mouseover(function(){ //将原来显示的内容区域进行隐藏 $("div.contentin").hide(); //当前标签所对应的内容区域显示出来 }); */ $("#tabfirst li").each(function(index){ //每一个包装li的jquery对象都会执行function中的代码 //index是当前执行这个function代码的li对应在所有li组成的数组中的索引值 //有了index的值之后,就可以找到当前标签对应的内容区域 $(this).mouseover(function(){ var liNode = $(this); timoutid = setTimeout(function(){ //将原来显示的内容区域进行隐藏 $("div.contentin").removeClass("contentin"); //对有tabin的class定义的li清除tabin的class $("#tabfirst li.tabin").removeClass("tabin"); //当前标签所对应的内容区域显示出来 //$("div").eq(index).addClass("contentin"); $("div.contentfirst:eq(" + index + ")").addClass("contentin"); liNode.addClass("tabin"); },300); }).mouseout(function(){ clearTimeout(timoutid); }); }); //在整个页面装入完成后,标签效果2的内容区域需要装入静态的html页面内容 $("#realcontent").load("TabLoad.html"); //找到标签2效果对应的三个标签,注册鼠标点击事件 $("#tabsecond li").each(function(index){ $(this).click(function(){ $("#tabsecond li.tabin").removeClass("tabin"); $(this).addClass("tabin"); if (index == 0) { //装入静态完成页面 $("#realcontent").load("TabLoad.html"); } else if (index == 1) { //装入动态部分页面 $("#realcontent").load("TabLoad.jsph2"); } else if (index == 2) { //装入远程数据(这里也是一个动态页面输出的数据) //$("#realcontent").load("TabData.jsp"); $("#realcontent").load("TabLoad.jsp"); } }); }); //对于loading图片绑定ajax请求开始和交互结束的事件 $("#contentsecond img").bind("ajaxStart",function(){ //把div里面的内容清空 $("#realcontent").html(""); //整个页面中任意ajax交互开始前,function中的内容会被执行 $(this).show(); }).bind("ajaxStop",function(){ //整个页面中任意ajax交互结束后,function中的内容会被执行 $(this).slideUp(5000); }); });

/WebRoot/css/tab.css:

ul,li { margin: 0; padding: 0; list-style: none; } #tabfirst li { float: left; background-color: #868686; color: white; padding: 5px; margin-right: 2px; border: 1px solid white; } #tabfirst li.tabin { background-color: #6E6E6E; border: 1px solid #6E6E6E; } div.contentfirst { clear: left; background-color: #6E6E6E; color: white; width: 300px; height: 100px; padding: 10px; display: none; } div.contentin { display: block; } #tabsecond li { float: left; background-color: white; color: blue; padding: 5px; margin-right: 2px; cursor: pointer; } #tabsecond li.tabin { background-color: #F2F6FB; border: 1px solid black; border-bottom: 0; z-index: 100; position: relative; } #contentsecond { width: 500px; height: 200px; padding: 10px; background-color: #F2F6FB; clear: left; border: 1px solid black; position: relative; top: -1px; } img { display: none; }

/WebRoot/TabLoad.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>这是一个静态页面</title> </head> <body> 载入静态页面的内容。<br> 载入静态页面的内容。<br> 载入静态页面的内容。<br> 载入静态页面的内容。<br> 载入静态页面的内容。<br> 载入静态页面的内容。<br> </body> </html>

/WebRoot/TabLoad.jsp:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwsfp.html