博客列表 >选项卡案例模仿

选项卡案例模仿

手机用户1594223549
手机用户1594223549原创
2022年11月14日 00:00:19470浏览

1.输出结果

2.代码部分

  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. /* 隐藏 */
  10. .hidden {
  11. display: none;
  12. }
  13. /* 显示 */
  14. .active {
  15. display: block;
  16. }
  17. .type > *.active,.content > *.active {
  18. background-color: lightcoral;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box">
  24. <!-- 1.栏目组 -->
  25. <div class="type" style="display: flex"></div>
  26. <!-- 2.内容组 -->
  27. <div class="content"></div>
  28. </div>
  29. <script type="module">
  30. // 导入模块
  31. import { createTab, setBtnStatus, setContentStatus } from './modules/tabs.js';
  32. // 1. 获取栏目,内容容器元素
  33. const type = document.querySelector('.type');
  34. const content = document.querySelector('.content');
  35. // 2.页面加载完成,创建栏目和对应的内容
  36. window.onload = () => createTab(type, content);
  37. // 3. 点击栏目时,设置按钮的状态,与按钮对应的内容状态
  38. type.onclick = ev => {
  39. // ev.currentTarget
  40. // 触发主体
  41. // ev.target
  42. // 绑定主体, 是触发主体父级元素
  43. // 1.1 当前按钮高亮
  44. setBtnStatus(ev);
  45. // 1.2 与按钮对应的内容显示出来
  46. setContentStatus(ev, ev.target);
  47. };
  48. </script>
  49. </body>
  50. </html>
  1. // ! 栏目数据
  2. const cates = [
  3. { cid: 1, cname: '国际新闻'},
  4. { cid: 2, cname: '中国新闻'},
  5. { cid: 3, cname: '北京新闻'},
  6. ];
  7. // ! 列表数据
  8. // 列表数据,必须与指定的栏目,一一对应
  9. const details = [
  10. {
  11. key: 1,
  12. // 查询匹配
  13. cid: 1,
  14. content: [
  15. {
  16. title: '俄罗斯宣布从赫尔松部分地区撤军',
  17. url: 'https://news.ifeng.com/c/8KoK54urn1k',
  18. },
  19. {
  20. title: '俄罗斯宣布从赫尔松部分地区撤军',
  21. url: 'https://news.ifeng.com/c/8KoK54urn1k',
  22. },
  23. {
  24. title: '俄罗斯宣布从赫尔松部分地区撤军',
  25. url: 'https://news.ifeng.com/c/8KoK54urn1k',
  26. },
  27. ],
  28. },
  29. {
  30. key: 2,
  31. cid: 2,
  32. content: [
  33. {
  34. title: '空战隐身无人僚机亮相!',
  35. url: 'https://news.ifeng.com/c/8KoeDFJXF1b',
  36. },
  37. {
  38. title: '空战隐身无人僚机亮相!',
  39. url: 'https://news.ifeng.com/c/8KoeDFJXF1b',
  40. },
  41. {
  42. title: '空战隐身无人僚机亮相!',
  43. url: 'https://news.ifeng.com/c/8KoeDFJXF1b',
  44. },
  45. ],
  46. },
  47. {
  48. key: 3,
  49. cid: 3,
  50. content: [
  51. {
  52. title: '安康码”上新!家庭成员核酸情况查询更便捷',
  53. url: 'https://ah.ifeng.com/c/8KkGUDhAZNZ',
  54. },
  55. {
  56. title: '安康码”上新!家庭成员核酸情况查询更便捷',
  57. url: 'https://ah.ifeng.com/c/8KkGUDhAZNZ',
  58. },
  59. {
  60. title: '安康码”上新!家庭成员核酸情况查询更便捷',
  61. url: 'https://ah.ifeng.com/c/8KkGUDhAZNZ',
  62. },
  63. ],
  64. },
  65. ];
  66. // 创建样目和对应的内容区
  67. function createTab(type, content) {
  68. // 1. 生成样目
  69. for (let i = 0; i < cates.length; i++) {
  70. // (1) 创建一个按钮
  71. const btn =document.createElement('button');
  72. // (2) 设置按钮的文本
  73. btn.textContent = cates[i].cname;
  74. // (3) 给按钮添加一个自定义的 date-key, 主要是为了一个内容id的绑定
  75. btn.dataset.key = cates[i].cid;
  76. // (4) 默认高亮第一个,所以第一个加上class="active"
  77. if (i === 0) btn.classList.add('active');
  78. // (5) 将新按钮,添加到栏目容器元素中type
  79. type.append(btn);
  80. }
  81. // 2. 生成内容
  82. for (let i = 0; i < details.length; i++) {
  83. // (1) 创建列表
  84. const ul =document.createElement('ul')
  85. // (2) 添加列表索引
  86. ul.dataset.key = details[i].cid;
  87. // (3) 默认显示第一个,其他的隐藏
  88. ul.classList.add(i === 0 ? 'active' : 'hidden');
  89. // (4) 生成子元素,用于显示第一条数据
  90. for (let k = 0; k < details[i].content.length; k++) {
  91. // 1. 生成 <li>
  92. const li = document.createElement('li');
  93. // 2. 生成 <a>
  94. const a = document.createElement('a');
  95. // 3. a.href
  96. a.href = details[i].content[k].url;
  97. // 4. a.textContent
  98. a.textContent = details[i].content[k].title;
  99. // 5. 将<a>添加到<li>
  100. li.append(a);
  101. // 6. <li>添加到<ul>
  102. ul.append(li);
  103. // 7. <ul>添加到内容容器中.content
  104. content.append(ul);
  105. }
  106. }
  107. }
  108. // 设置按钮高亮
  109. function setBtnStatus(ev) {
  110. // 1. 当前按钮
  111. const currentBnt = ev.target;
  112. // 2. 去掉所有的按钮的active, 遍历
  113. // ev.currentTarget: 事件绑定主体 , 父元素
  114. // 转为真数组
  115. [...ev.currentTarget.children].forEach(btn => btn.classList.remove('active'));
  116. // 3. 设置当前按钮高亮
  117. currentBnt.classList.add('active');
  118. }
  119. // 设置内容状态: 与按钮对应的内容显示出来
  120. function setContentStatus(ev, currentBnt) {
  121. // 1. 获取所有的列表
  122. const lists = document.querySelectorAll('.content > ul');
  123. // 2. 去掉所有列表的active,换成hidden
  124. lists.forEach(list => list.classList.replace('active', 'hidden'));
  125. // 3. 找到与栏目key对应的列表
  126. // lists: NodeList对象, 不是数组
  127. const currentList = [...lists].find(list => list.dataset.key == currentBtn.dataset.key);
  128. // 返回当前应该高亮显示的列表
  129. // 4. 将要显示的列表,加上active,显示出来
  130. currentList.classList.replace('hidden', 'active');
  131. }
  132. export { createTab, setBtnStatus, setContentStatus };
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议