CSS选项卡之一

来源:互联网 发布:windows 用户界面设计 编辑:程序博客网 时间:2024/05/19 22:03
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5. <title>井底的蛙</title>
  6. <script type="text/javascript">
  7. /*
  8. 选项卡封装
  9. by 井底的蛙
  10. 2008-2-4
  11. */
  12. opCard = function()
  13. {
  14. this.bind = new Array();
  15. this.index = 0;     //默认显示哪个选项卡,从0开始
  16. this.style = new Array();       //["","",""]
  17. this.overStyle = false;     //选项是否有over, out变换样式事件,样式为this.style[2]
  18. this.overChange = false;        //内容是否用over, out直接激活
  19. this.menu = false;              //菜单类型
  20. this.nesting = [false,false,"",""];     //是否嵌套,后面2个参数是指定menu,info的子集深度所用id
  21. this.auto = [false, 1000];      //自动滚动[true,2000]
  22. this.timerID = null;            //自动播放的
  23. this.menutimerID = null;        //菜单延时的
  24. this.creat = function(func)
  25. {
  26. var _arrMenu = document.getElementById(this.bind[0]).getElementsByTagName(this.bind[1]);
  27. var _arrInfo = document.getElementById(this.bind[2]).getElementsByTagName(this.bind[3]);
  28. var my = this, i;
  29. var argLen = arguments.length;
  30. var arrM = new Array();
  31. if(this.nesting[0] || this.nesting[1])  // 有选项卡嵌套
  32. {   // 过滤出需要的数据
  33. var arrMenu = this.nesting[0]?getChilds(_arrMenu,this.bind[0],2):_arrMenu;
  34. var arrInfo = this.nesting[1]?getChilds(_arrInfo,this.bind[2],3):_arrInfo;
  35. }
  36. else
  37. {
  38. var arrMenu = _arrMenu;
  39. var arrInfo = _arrInfo;
  40. }
  41. var l = arrMenu.length;
  42. if(l!=arrInfo.length){alert("菜单和内容必须拥有相同的数量/n如果需要,你可以放一个空的在那占位。")}
  43. // 修正
  44. if(this.menu){this.auto=false;this.overChange=true;} //如果是菜单,则没有自动运行,有over, out直接激活
  45. // 循环添加各个事件等
  46. for(i=0;i<l;i++)
  47. {
  48. arrMenu[i].cName = arrMenu[i].className;
  49. arrMenu[i].className = (i!=this.index || this.menu)?getClass(arrMenu[i],this.style[0]):getClass(arrMenu[i],this.style[1]);      //加载样式,菜单的话统一样式
  50. if(arrMenu[i].getAttribute("skip")) // 需要跳过的容器
  51. {
  52. if(this.overStyle || this.overChange)   // 有over, out 改变样式 或者 激活
  53. {
  54. arrMenu[i].onmouseover = function(){changeTitle(this, 2);autoStop(this, 0);}
  55. arrMenu[i].onmouseout = function(){changeTitle(this, 0);autoStop(this, 1);}
  56. }
  57. arrMenu[i].onclick = function(){if(argLen==1){func()}}
  58. arrInfo[i].style.display = "none";
  59. continue;
  60. }
  61. if(i!=this.index || this.menu){arrInfo[i].style.display="none"};    //隐藏初始化,菜单的话全部隐藏
  62. arrMenu[i].index = i;   //记录自己激活值[序号]
  63. arrInfo[i].index = i;
  64. if(this.overChange) //有鼠标over, out事件
  65. {
  66. arrMenu[i].onmouseover = function(){changeOption(this);my.menu?changeMenu(1):autoStop(this, 0);}
  67. arrMenu[i].onmouseout = function(){changeOption(this);my.menu?changeMenu(0):autoStop(this, 1);}
  68. }
  69. else    //onclick触发
  70. {
  71. arrMenu[i].onclick = function(){changeOption(this);autoStop(this, 0);if(argLen==1){func()}}
  72. if(this.overStyle)  // 有over, out 改变样式
  73. {
  74. arrMenu[i].onmouseover = function(){changeTitle(this, 2);autoStop(this, 0);}
  75. arrMenu[i].onmouseout = function(){changeTitle(this, 0);autoStop(this, 1);}
  76. }
  77. else    // 没有over, out 改变样式
  78. {
  79. if(this.auto[0])    // 有自动运行
  80. {
  81. arrMenu[i].onmouseover = function(){autoStop(this, 0);}
  82. arrMenu[i].onmouseout = function(){autoStop(this, 1);}
  83. }
  84. }
  85. }
  86. if(this.auto[0] || this.menu)   //arrinfo 控制自动播放
  87. {
  88. arrInfo[i].onmouseover = function(){my.menu?changeMenu(1):autoStop(this, 0);}
  89. arrInfo[i].onmouseout = function(){my.menu?changeMenu(0):autoStop(this, 1);}
  90. }
  91. }   //for结束
  92. if(this.auto[0])
  93. {
  94. this.timerID = setTimeout(autoMove,this.auto[1])
  95. }
  96. // 自动播放
  97. function autoMove()
  98. {
  99. var n;
  100. n = my.index + 1;
  101. if(n==l){n=0};
  102. while(arrMenu[n].getAttribute("skip"))      // 需要跳过的容器
  103. {
  104. n += 1;
  105. if(n==l){n=0};
  106. }
  107. changeOption(arrMenu[n]);
  108. my.timerID = setTimeout(autoMove,my.auto[1]);
  109. }
  110. // onmouseover时,自动播放停止。num:0为over,1为out。 obj暂时无用。 -_-!!
  111. function autoStop(obj, num)
  112. {
  113. if(!my.auto[0]){return;}
  114. //if(obj.index==my.index)
  115. num == 0 ? clearTimeout(my.timerID) : my.timerID = setTimeout(autoMove,my.auto[1]);
  116. }
  117. // 改变选项卡
  118. function changeOption(obj)
  119. {
  120. arrMenu[my.index].className = getClass(arrMenu[my.index],my.style[0]);  //修改旧内容
  121. arrInfo[my.index].style.display = "none";   //隐藏旧内容
  122. obj.className = getClass(obj,my.style[1]);      //修改为新样式
  123. arrInfo[obj.index].style.display = "";  //显示新内容
  124. my.index = obj.index;   //更新当前选择的index
  125. }
  126. /*
  127. 只有onclick时,overStyle的onmouseover,onmouseout事件。用来预激活
  128. obj:目标对象。   num:1为over,0为out
  129. */
  130. function changeTitle(obj, num)
  131. {
  132. if(!my.overStyle){return;};
  133. if(obj.index!=my.index){obj.className = getClass(obj,my.style[num])}
  134. }
  135. /*
  136. 菜单类型时用
  137. obj:目标对象。   num:1为over,0为out
  138. */
  139. function changeMenu(num)
  140. {
  141. if(!my.menu){return;}
  142. num==0?my.menutimerID = setTimeout(menuClose,1000):clearTimeout(my.menutimerID)
  143. }
  144. //关闭菜单
  145. function menuClose()
  146. {
  147. arrInfo[my.index].style.display = "none";
  148. arrMenu[my.index].className = getClass(arrMenu[my.index],my.style[0]);
  149. }
  150. // 得到className(防止将原有样式覆盖)
  151. function getClass(o, s)
  152. {
  153. if(o.cName==""){return s}
  154. else{return o.cName + " " + s}
  155. }
  156. //嵌套情况下得到真正的子集
  157. function getChilds(arrObj, id, num)
  158. {
  159. var depth = 0;
  160. var firstObj = my.nesting[num]==""?arrObj[0]:document.getElementById(my.nesting[num]);      //得到第一个子集
  161. do  //计算深度
  162. {
  163. if(firstObj.parentNode.getAttribute("id")==id){break}else{depth+=1}
  164. firstObjfirstObj = firstObj.parentNode;
  165. }
  166. while(firstObj.tagName.toLowerCase()!="body")   // body强制退出。
  167. var t;
  168. var arr = new Array();
  169. for(i=0;i<arrObj.length;i++)    //过滤出需要的数据
  170. {
  171. t = arrObj[i], d = 0;
  172. do
  173. {
  174. if(t.parentNode.getAttribute("id")==id && d == depth)
  175. {
  176. arr.push(arrObj[i]);break;      //得到数据
  177. }
  178. else
  179. {
  180. if(d==depth){break};d+=1;
  181. }
  182. tt = t.parentNode;
  183. }
  184. while(t.tagName.toLowerCase()!="body")  // body强制退出
  185. }
  186. return arr;
  187. }
  188. }
  189. }
  190. window.onload = function()
  191. {
  192. var aa = new opCard();
  193. aa.bind = ["a1","div","b1","div"];
  194. aa.style = ["a1_0","a1_1","a1_0"];
  195. aa.index = 0;
  196. aa.nesting = [false,true,"",""]
  197. aa.creat();
  198. aa =null;
  199. // 默认的onclick中,第一个例子
  200. var bba = new opCard();
  201. bba.bind = ["a2","li","b2","div"];
  202. bba.style = ["style1","style2","style3"];
  203. bba.overStyle = true;
  204. bba.creat();
  205. bba = null;
  206. // 默认的onclick中,第二个例子
  207. var bbb = new opCard();
  208. bbb.bind = ["a3","li","b3","div"];
  209. bbb.style = ["style1","style2","style3"];
  210. bbb.overStyle = true;
  211. bbb.creat();
  212. bbb = null;
  213. // onmousover触发中的例子
  214. var cc = new opCard();
  215. cc.bind = ["a4","li","b4","div"];
  216. cc.style = ["style1","style2","style3"];
  217. cc.overStyle = true;
  218. cc.overChange = true;
  219. cc.creat();
  220. cc = null;
  221. //自动播放auto第一个例子
  222. var dd = new opCard();
  223. dd.bind = ["a5","li","b5","div"];
  224. dd.style = ["style1","style2","style3"];
  225. dd.auto = [true, 3000];
  226. dd.creat();
  227. dd = null;
  228. //自动播放auto第二个例子
  229. var ee = new opCard();
  230. ee.bind = ["a6","li","b6","div"];
  231. ee.style = ["style1","style2","style3"];
  232. ee.auto = [true, 2000];
  233. ee.overChange = true;
  234. ee.creat();
  235. ee = null;
  236. //自动播放auto第三个例子
  237. var ff = new opCard();
  238. ff.bind = ["a7","li","b7","div"];
  239. ff.style = ["style1","style2","style3"];
  240. ff.auto = [true, 1000];
  241. ff.overChange = true;
  242. ff.overStyle = true;
  243. ff.creat();
  244. ff = null;
  245. //菜单导航例子
  246. var gg = new opCard();
  247. gg.bind = ["a8","li","b8","div"];
  248. gg.style = ["style1","style2","style3"];
  249. gg.overStyle = true;
  250. gg.menu = true;
  251. gg.creat();
  252. gg = null;
  253. //其他应用
  254. var hh = new opCard();
  255. hh.bind = ["a9","li","a9","li"];
  256. hh.style = ["style4","style4","style4"];
  257. //hh.overStyle = true;
  258. hh.auto = [true, 1000];
  259. hh.creat();
  260. hh = null;
  261. }
  262. </script>
  263. <style type="text/css">
  264. body{font-size:12px; font-family:Verdana,"宋体";}
  265. p,ul{margin:0px; padding:0px;}
  266. td,div{font-size:12px}
  267. .a1_0 {border:1px dotted #3399FF; width:120px; background-color:#f5f5f5; margin:3px; padding:2px 0px; cursor:pointer;}
  268. .a1_1 {border:1px solid #FF9900; width:120px; margin:3px; padding:2px 0px; cursor:pointer;}
  269. .test{text-decoration:underline;}
  270. #b1 div.s{border:1px solid #999999; width:90%; height:500px; margin:3px; padding:10px; overflow-y:auto; line-height:18px;}
  271. #b1 div.s strong{color:#0066FF;}
  272. /* */
  273. .style1{float:left; width:80px; background-color:#f5f5f5; border:1px solid #3399FF; text-align:center; margin-right:2px; list-style-type:none; cursor:pointer;}
  274. .style2{float:left; width:80px; background-color:#f5f5f5; border:1px solid #FF9900; text-align:center; margin-right:2px; list-style-type:none; cursor:pointer;}
  275. .style3{float:left; width:80px; background-color:#f5f5f5; border:1px solid #666666; text-align:center; margin-right:2px; list-style-type:none; cursor:pointer;}
  276. #a2,#a3,#a4,#a5,#a6,#a7,#a8 {height:22px;}
  277. #b2 div,#b3 div,#b4 div,#b5 div,#b6 div,#b7 div{border:1px solid #FF9900; height:100px; width:400px; padding:5px; overflow-y:auto;}
  278. #b8 div{border:1px solid #FF9900; height:20px; width:350px; padding:2px 5px;}
  279. #b8 div a{margin-right:20px;}
  280. .style4{float:left; background-color:#999999; text-align:left; list-style-type:none; padding:2px 5px; color:#FFFFFF;}
  281. </style>
  282. </head>
  283. <body>
  284. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  285. <tr>
  286. <td id="a1" width="140" valign="top" align="center">
  287. <div class="test">简单介绍</div>
  288. <div class="test">默认的onclick</div>
  289. <div class="test">onmousover触发</div>
  290. <div class="test">自动播放auto</div>
  291. <div class="test">菜单导航</div>
  292. <div class="test">关于嵌套</div>
  293. <div class="test">其他应用</div>
  294. <div class="test">一些说明</div>
  295. <div class="test"> </div>
  296. <div class="test"> </div>
  297. <div class="test"> </div>
  298. <div class="test"> </div>
  299. </td>
  300. <td id="b1" valign="top">
  301. <div class="s">
  302. 封装了一个选项卡,不过已经不像选项卡了-_-!!!<br><br>
  303. 现稍微说明下吧,如果不明白的话,旁边有几个例子可能说明起来更清楚些<br><br>
  304. <p>
  305. <strong>obj.bind = ["a1","td","b1","div"];</strong><br>
  306. 绑定id="a1"下的td标签为菜单,绑定id="b1"下的div标签为内容,简单么?<br>
  307. td标签的数量和div标签的数量必须相同<br>
  308. (若不需要显示内容,只显示菜单话,可以这个在td标签上加<td skip="true"><br>
  309. 如果id="a1"下的td标签有嵌套表格,这样的话,就不是所有的td都是菜单,这时候需要用下nesting<br><br>
  310. <strong>obj.nesting = [false,true,"",""];</strong><br>
  311. 当标签tag有嵌套时,需要用到这个<br>
  312. 比如选项卡内容是放在div容器里,而本身这个内容里也有div标签,这时就需要用到<br>
  313. 菜单嵌套为false,内容嵌套为true,且会自动判断出内容标签,多数时候这样就可以了<br>
  314. 判断方法为,认定getElementsByTagName后第一个标签为内容第一项,其他的就用这个第一项的深度来判断<br>
  315. 但有些情况下,这样还是不行<br>
  316. 我用后面2个参数做id来指定菜单或者内容的第一项nesting = [false,true,"","q2"];<br>
  317. 这样就肯定不会错了(不明白的话看下例子就简单多了)<br><br>
  318. <strong>obj.index = 0;</strong><br>
  319. 默认显示第几个选项卡,序号从0开始<br><br>
  320. <strong>obj.style = ["c1","c2","c3"]</strong><br>
  321. 菜单加载的样式的className:<br>
  322. 菜单未选中的className是c1<br>
  323. 菜单选中的className是c2<br>
  324. 菜单onmouseover的className是c3<br><br>
  325. <strong>obj.overStyle = false;</strong><br>
  326. 选项卡是否有onmouseover, onmouseout变换样式事件[非激活选项卡内容],对应的样式为style[2]<br><br>
  327. <strong>obj.overChange = false;</strong><br>
  328. 选项卡内容是否用onmouseover, onmouseout直接激活<br><br>
  329. <strong>obj.menu = false;</strong><br>
  330. 选项卡是菜单类型<br><br>
  331. <strong>obj.auto = [false, 1000];</strong><br>
  332. 选项卡是否自动播放,播放速度(毫秒)<br><br>
  333. <strong>obj.creat();</strong><br>
  334. 开始生成选项卡,需要onclick触发事件的话,可以obj.creat(函数名)<br>
  335. 所有的都会触发
  336. </p>
  337. </div>
  338. <div class="s">
  339. <!--默认的onclick中,第一个例子-->
  340. <div id="a2">
  341. <ul>
  342. <li>1</li>
  343. <li>2</li>
  344. <li>3</li>
  345. <li>4</li>
  346. </ul>
  347. </div>
  348. <div id="b2">
  349. <div>这个例子是用onclick触发的<br>并设置overStyle = true;<br>(over,out改变样式,但不激活)</div>
  350. <div>
  351. var bba = new opCard();<br>
  352. bba.bind = ["a2","li","b2","div"];<br>
  353. bba.style = ["style1","style2","style1"];<br>
  354. bba.overStyle = true;<br>
  355. bba.creat();<br>
  356. bba = null;
  357. </div>
  358. <div>33333</div>
  359. <div>4444444</div>
  360. </div><br>
  361. <!--默认的onclick中,第二个例子-->
  362. <div id="a3">
  363. <ul>
  364. <li>1</li>
  365. <li>2</li>
  366. <li>3</li>
  367. <li skip="true">4</li>
  368. </ul>
  369. </div>
  370. <div id="b3">
  371. <div>
  372. 这个例子是比上面增加skip="true"<br><li skip="true">4</li><br>所以选项卡4是没有内容的<br>
  373. 你可以为选项卡的文字直接加上链接,我这里就不加了
  374. </div>
  375. <div>222222</div>
  376. <div>33333</div>
  377. <div>4444444</div>
  378. </div>
  379. </div>
  380. <div class="s">
  381. <!--onmousover触发中的例子-->
  382. <div id="a4">
  383. <ul>
  384. <li>1</li>
  385. <li>2</li>
  386. <li skip="true">3</li>
  387. <li>4</li>
  388. </ul>
  389. </div>
  390. <div id="b4">
  391. <div>这个例子是用onmouseover触发的<br>(over,out直接激活)<br>并且<li skip="true">3</li>[选项卡3不会被激活]<br>overStyle = true;</div>
  392. <div>
  393. var cc = new opCard();<br>
  394. cc.bind = ["a4","li","b4","div"];<br>
  395. cc.style = ["style1","style2","style1"];<br>
  396. cc.overStyle = true;<br>
  397. cc.overChange = true;<br>
  398. cc.creat();<br>
  399. cc = null;
  400. </div>
  401. <div>33333</div>
  402. <div>4444444</div>
  403. </div>
  404. </div>
  405. <div class="s">
  406. <!--自动播放auto第一个例子-->
  407. <div id="a5">
  408. <ul>
  409. <li>1</li>
  410. <li>2</li>
  411. <li>3</li>
  412. <li>4</li>
  413. </ul>
  414. </div>
  415. <div id="b5">
  416. <div>1111111<br>我每3秒切换一个选项卡</div>
  417. <div>
  418. var ee = new opCard();<br>
  419. ee.bind = ["a6","li","b6","div"];<br>
  420. ee.style = ["style1","style2","style1"];<br>
  421. ee.auto = [true, 2000];<br>
  422. ee.overChange = true;<br>
  423. ee.creat();<br>
  424. ee = null;
  425. </div>
  426. <div>33333<br>我每3秒切换一个选项卡</div>
  427. <div>4444444<br>我每3秒切换一个选项卡</div>
  428. </div>
  429. <div>注:<strong>鼠标在选项卡任意位置都会使停止播放</strong><br><br>下面这个结合overChange一起</div>
  430. <!--自动播放auto第二个例子-->
  431. <div id="a6">
  432. <ul>
  433. <li>1</li>
  434. <li>2</li>
  435. <li>3</li>
  436. <li>4</li>
  437. </ul>
  438. </div>
  439. <div id="b6">
  440. <div>1111111<br>我每2秒切换一个选项卡</div>
  441. <div>
  442. var dd = new opCard();<br>
  443. dd.bind = ["a5","li","b5","div"];<br>
  444. dd.style = ["style1","style2","style1"];<br>
  445. dd.auto = [true, 3000];<br>
  446. dd.creat();<br>
  447. dd = null;<br>
  448. </div>
  449. <div>33333<br>我每2秒切换一个选项卡</div>
  450. <div>4444444<br>我每2秒切换一个选项卡</div>
  451. </div>
  452. <div><br>下面这个再来个<li skip="true">3</li>,且overStyle=true;</div>
  453. <!--自动播放auto第三个例子-->
  454. <div id="a7">
  455. <ul>
  456. <li>1</li>
  457. <li>2</li>
  458. <li skip="true">3</li>
  459. <li>4</li>
  460. </ul>
  461. </div>
  462. <div id="b7">
  463. <div>1111111<br>我每1秒切换一个选项卡</div>
  464. <div>
  465. var ff = new opCard();<br>
  466. ff.bind = ["a7","li","b7","div"];<br>
  467. ff.style = ["style1","style2","style1"];<br>
  468. ff.auto = [true, 1000];<br>
  469. ff.overChange = true;<br>
  470. ff.overStyle = true;<br>
  471. ff.creat();<br>
  472. ff = null;
  473. </div>
  474. <div>33333<br>我每1秒切换一个选项卡</div>
  475. <div>4444444<br>我每1秒切换一个选项卡</div>
  476. </div>
  477. </div>
  478. <!--菜单导航的例子-->
  479. <div class="s">
  480. <div id="a8">
  481. <ul>
  482. <li skip="true">首页</li>
  483. <li>新闻</li>
  484. <li>论坛</li>
  485. <li skip="true">联系我们</li>
  486. </ul>
  487. </div>
  488. <div id="b8">
  489. <div></div>
  490. <div><a href="#">国内新闻</a><a href="#">国际新闻</a><a href="#">娱乐新闻</a><a href="#">体育新闻</a></div>
  491. <div><a href="#">蓝色理想</a><a href="#">blue idea</a></div>
  492. <div></div>
  493. </div>
  494. <div><strong>注:菜单延时一秒关闭</strong><br>这里只是演示,实际可以设置下position什么的。-o-<br>多级菜单不知道能不能支持,没试过</div>
  495. </div>
  496. <div class="s">
  497. 我见过的许多选项卡都不能用来嵌套<br>但还是有些时候会用到的<br>
  498. 所以如您所见,这个演示本身就是一个嵌套,效果还不错
  499. </div>
  500. <!--其他应用-->
  501. <div class="s">
  502. 比如图片切换什么,不过我太懒了,不写了。。。<br>
  503. 这个能干什么,我也不知道。。
  504. <div id="a9">
  505. <li>新年好啊</li>
  506. <li>快过年了</li>
  507. <li>天好冷啊</li>
  508. <li>大家去抢红包吧</li>
  509. <li>红包~!红包~!你在哪里啊?</li>
  510. </div>
  511. </div>
  512. <div class="s">
  513. 选项卡标题(或者菜单)必须和选项卡内容的数量相同,否则会出错<br>即使选项卡菜单skip="true",内容也要放一个空的标签占位<br>
  514. 其他好像暂时没发现什么<br><br>
  515. 本来打算加入ajax的,但有些还没想好。<br>
  516. 效率的话,应该还行,我这个演示放了几个选项卡,似乎还不错<br><br>
  517. 写这个的目的就是以后弄选项卡只要处理样式就可以了,很多选项卡或者切换之类都是大同小异<br>
  518. 本来是打算弄些特效在里面,考虑会增加不少代码,所以不加了,简洁点。<br><br>
  519. 哦,还有选项卡是附加样式,不会取消原本设置的样式<br>
  520. 如左边的下划线是原有的样式
  521. </div>
  522. <div class="s">啥都没有1</div>
  523. <div class="s">啥都没有2</div>
  524. <div class="s">啥都没有3</div>
  525. <div class="s">啥都没有4</div>
  526. </td>
  527. </tr>
  528. </table>
  529. </body>
  530. </html>