博客列表 >实战:选项卡

实战:选项卡

P粉751989631
P粉751989631原创
2022年11月16日 22:14:24542浏览

选项卡

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>选项卡</title>
  8. <style>
  9. .hidden {
  10. }
  11. .active {
  12. display: block;
  13. }
  14. .type > *.active,
  15. .content > *.active {
  16. background-color: lightgreen;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="box">
  22. <!-- 1. 栏目组 -->
  23. <div class="type" style="display: flex"></div>
  24. <!-- 2. 内容组 -->
  25. <div class="content"></div>
  26. </div>
  27. <script type="module">
  28. // 导入模块
  29. import { createTab, setBtnStatus, setContentStatus } from "./modules/zy01.js";
  30. // 1. 获取栏目,内容容器元素
  31. const type = document.querySelector(".type");
  32. const content = document.querySelector(".content");
  33. // 2. 页面加载完成,创建栏目和对应的内容
  34. window.onload = () => createTab(type, content);
  35. // 3. 点击栏目时,设置按钮的状态,与按钮对应的内容的状态
  36. type.onclick = (ev) => {
  37. // 1. 当前按钮高亮
  38. setBtnStatus(ev);
  39. // 2. 与按钮对应的内容显示出来
  40. setContentStatus(ev, ev.target);
  41. };
  42. </script>
  43. </body>
  44. </html>
  1. //todo 选项卡的数据以及方法
  2. // 栏目数据
  3. const cates = [
  4. { cid: 1, cname: "国际新闻" },
  5. { cid: 2, cname: "中国新闻" },
  6. { cid: 3, cname: "江苏新闻" },
  7. ];
  8. //列表数据
  9. const details = [
  10. {
  11. Key: 1,
  12. cid: 1,
  13. content: [
  14. {
  15. title: "饿罗斯退兵",
  16. url: "https://www.360kuai.com/pc/detail?url=http%3A%2F%2Fzm.news.so.com%2Fdaef26b096bf7944c16970f96c1ac169&check=76bc8294131d84fb&refer_scene=&tj_url=&ucheck=f5cbfc4258ec56b26c922b3c24b93e82&sign=360_c109fef1&uid=&scene=49001",
  17. },
  18. ],
  19. },
  20. {
  21. Key: 2,
  22. cid: 2,
  23. content: [
  24. {
  25. title: "中国崛起",
  26. url: "https://www.360kuai.com/pc/detail?url=http%3A%2F%2Fzm.news.so.com%2Fdaef26b096bf7944c16970f96c1ac169&check=76bc8294131d84fb&refer_scene=&tj_url=&ucheck=f5cbfc4258ec56b26c922b3c24b93e82&sign=360_c109fef1&uid=&scene=49001",
  27. },
  28. ],
  29. },
  30. {
  31. Key: 3,
  32. cid: 3,
  33. content: [
  34. {
  35. title: "无锡新闻",
  36. url: "https://www.360kuai.com/pc/detail?url=http%3A%2F%2Fzm.news.so.com%2Fdaef26b096bf7944c16970f96c1ac169&check=76bc8294131d84fb&refer_scene=&tj_url=&ucheck=f5cbfc4258ec56b26c922b3c24b93e82&sign=360_c109fef1&uid=&scene=49001",
  37. },
  38. ],
  39. },
  40. ];
  41. // 创建样目和对应的内容区
  42. function createTab(type, content) {
  43. // 1. 生成样目
  44. for (let i = 0; i < cates.length; i++) {
  45. // (1) 创建一个按钮
  46. const btn = document.createElement("button");
  47. // (2) 设置按钮的文本
  48. btn.textContent = cates[i].cname;
  49. // (3) 给按钮添加一个自定义 data-key, 主要是为了一内容id绑定
  50. btn.dataset.key = cates[i].cid;
  51. // (4) 默认高亮第1个,所以第一个加上class="active"
  52. if (i === 0) btn.classList.add("active");
  53. // (5) 将新按钮, 添加到栏目容器元素中 type
  54. type.append(btn);
  55. }
  56. // 2. 生成内容
  57. for (let i = 0; i < details.length; i++) {
  58. // (1) 创建列表 <ul>
  59. const ul = document.createElement("ul");
  60. // (2) 添加列表索引<ul data-key>
  61. ul.dataset.key = details[i].cid;
  62. // (3) 默认显示第1个,其它隐藏
  63. ul.classList.add(i === 0 ? "active" : "hidden");
  64. // (4) 生成子元素<li><a>用于显示每一条数据
  65. for (let k = 0; k < details[i].content.length; k++) {
  66. // 1. 生成 <li>
  67. const li = document.createElement("li");
  68. // 2. 生成 <a>
  69. const a = document.createElement("a");
  70. // 3. a.href
  71. a.href = details[i].content[k].url;
  72. // 4. a.textContent
  73. a.textContent = details[i].content[k].title;
  74. // 5. 将<a>添加到<li>
  75. li.append(a);
  76. // 6. <li>添加到<ul>
  77. ul.append(li);
  78. // 7. <ul>添加到内容容器中.content
  79. content.append(ul);
  80. }
  81. }
  82. }
  83. // 设置按钮高亮
  84. function setBtnStatus(ev) {
  85. // 1. 当前按钮
  86. const currentBtn = ev.target;
  87. // 2. 去掉所有按钮的active, 遍历
  88. [...ev.currentTarget.children].forEach((btn) => btn.classList.remove("active"));
  89. // 3. 设置当前按钮高亮
  90. currentBtn.classList.add("active");
  91. }
  92. // 设置内容状态: 与按钮对应的内容显示出来
  93. function setContentStatus(ev, currentBtn) {
  94. // 1. 获取所有列表
  95. const lists = document.querySelectorAll(".content > ul");
  96. // 2. 去掉所有列表active,换成hidden
  97. lists.forEach((list) => list.classList.replace("active", "hidden"));
  98. // 3. 找到与栏目key对应的列表
  99. const currentList = [...lists].find((list) => list.dataset.key == currentBtn.dataset.key);
  100. // 4. 将要显示的列表,加上active,显示出来
  101. currentList.classList.replace("hidden", "active");
  102. }
  103. export { createTab, setBtnStatus, setContentStatus };

国际新闻中国新闻无锡新闻

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议