第四讲:标签页效果

来源:互联网 发布:背英文单词软件 编辑:程序博客网 时间:2024/05/01 03:31

tab.html

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>JQuery实例-标签页效果</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <link type="text/css" rel="stylesheet" href="css/tab.css" />
  7. <script type="text/javascript" src="js/jquery.js"></script>
  8. <script type="text/javascript" src="js/tab.js"></script>
  9. </head>
  10. <body>
  11. <ul id="tabfirst">
  12. <li class="tabin">标签1</li>
  13. <li>标签2</li>
  14. <li>标签3</li>
  15. </ul>
  16. <div class="contentin contentfirst">我是内容1</div>
  17. <div class="contentfirst">我是内容2</div>
  18. <div class="contentfirst">我是内容3</div>
  19. <br />
  20. <br />
  21. <br />
  22. <ul id="tabsecond">
  23. <li class="tabin">装入完整页面</li>
  24. <li>装入部分页面</li>
  25. <li>从远程获取数据</li>
  26. </ul>
  27. <div id="contentsecond">
  28. <img alt="装载中" src="images/img-loading.gif" />
  29. <div id="realcontent"></div>
  30. </div>
  31. </body>
  32. </html>
tab.css

  1. ul,li {
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. }
  6. #tabfirst li {
  7. float: left;
  8. background-color: #868686;
  9. color: white;
  10. padding: 5px;
  11. margin-right: 2px;
  12. border: 1px solid white;
  13. }
  14. #tabfirst li.tabin {
  15. background-color: #6E6E6E;
  16. border: 1px solid #6E6E6E;
  17. }
  18. div.contentfirst {
  19. clear: left;
  20. background-color: #6E6E6E;
  21. color: white;
  22. width: 300px;
  23. height: 100px;
  24. padding: 10px;
  25. display: none;
  26. }
  27. div.contentin {
  28. display: block;
  29. }


  30. #tabsecond li {
  31. float: left;
  32. background-color: white;
  33. color: blue;
  34. padding: 5px;
  35. margin-right: 2px;
  36. cursor: pointer;
  37. }
  38. #tabsecond li.tabin {
  39. background-color: #F2F6FB;
  40. border: 1px solid black;
  41. border-bottom: 0;
  42. z-index: 100;
  43. position: relative;
  44. }
  45. #contentsecond {
  46. width: 500px;
  47. height: 200px;
  48. padding: 10px;
  49. background-color: #F2F6FB;
  50. clear: left;
  51. border: 1px solid black;
  52. position: relative;
  53. top: -1px;
  54. }
  55. img {
  56. display: none;
  57. }

tab.js

  1. var timoutid;
  2. $(document).ready(function(){
  3. //找到所有的标签
  4. /*
  5. $("li").mouseover(function(){
  6. //将原来显示的内容区域进行隐藏
  7. $("div.contentin").hide();
  8. //当前标签所对应的内容区域显示出来
  9. });
  10. */
  11. $("#tabfirst li").each(function(index){
  12. //每一个包装li的jquery对象都会执行function中的代码
  13. //index是当前执行这个function代码的li对应在所有li组成的数组中的索引值
  14. //有了index的值之后,就可以找到当前标签对应的内容区域
  15. $(this).mouseover(function(){
  16. var liNode = $(this);
  17. timoutid = setTimeout(function(){
  18. //将原来显示的内容区域进行隐藏
  19. $("div.contentin").removeClass("contentin");
  20. //对有tabin的class定义的li清除tabin的class
  21. $("#tabfirst li.tabin").removeClass("tabin");
  22. //当前标签所对应的内容区域显示出来
  23. //$("div").eq(index).addClass("contentin");
  24. $("div.contentfirst:eq(" + index + ")").addClass("contentin");
  25. liNode.addClass("tabin");
  26. },300);
  27. }).mouseout(function(){
  28. clearTimeout(timoutid);
  29. });
  30. });
  31. //在整个页面装入完成后,标签效果2的内容区域需要装入静态的html页面内容
  32. $("#realcontent").load("TabLoad.html");
  33. //找到标签2效果对应的三个标签,注册鼠标点击事件
  34. $("#tabsecond li").each(function(index){
  35. $(this).click(function(){
  36. $("#tabsecond li.tabin").removeClass("tabin");
  37. $(this).addClass("tabin");
  38. if (index == 0) {
  39. //装入静态完成页面
  40. $("#realcontent").load("TabLoad.html");
  41. } else if (index == 1) {
  42. //装入动态部分页面
  43. $("#realcontent").load("TabLoad.jsp h2");
  44. } else if (index == 2) {
  45. //装入远程数据(这里也是一个动态页面输出的数据)
  46. $("#realcontent").load("TabData.jsp")
  47. }
  48. });
  49. });
  50. //对于loading图片绑定ajax请求开始和交互结束的事件
  51. $("#contentsecond img").bind("ajaxStart",function(){
  52. //把div里面的内容清空
  53. $("#realcontent").html("");
  54. //整个页面中任意ajax交互开始前,function中的内容会被执行
  55. $(this).show();
  56. }).bind("ajaxStop",function(){
  57. //整个页面中任意ajax交互结束后,function中的内容会被执行
  58. $(this).slideUp("1000");
  59. });
  60. });

0 0
原创粉丝点击