Blogger Information
Blog 28
fans 0
comment 0
visits 11629
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选项卡案例
子墨吖ฅฅ
Original
457 people have browsed it
  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,
  18. .content > *.active {
  19. background-color: lightgreen;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="box">
  25. <div class="type" style="display: flex"></div>
  26. <div class="content"></div>
  27. </div>
  28. <script type="module">
  29. import * as tabs from './modules/tabs.js';
  30. const type = document.querySelector('.type');
  31. const content = document.querySelector('.content');
  32. window.onload = () => tabs.createTab(type, content);
  33. type.onclick = ev => {
  34. tabs.setContentStatus(ev, ev.target);
  35. };
  36. </script>
  37. </body>
  38. </html>

JS代码部分

  1. // * 栏目数据
  2. const cates = [
  3. { cid: 1, cname: '国际新闻' },
  4. { cid: 2, cname: '中国新闻' },
  5. { cid: 3, cname: '安徽新闻' },
  6. ];
  7. // * 列表数据
  8. const details = [
  9. {
  10. key: 1,
  11. cid: 1,
  12. content: [
  13. {
  14. title: '俄罗斯宣布从赫尔松部分地区撤军',
  15. url: 'https://news.ifeng.com/c/8KoK54urn1k',
  16. },
  17. {
  18. title: '俄罗斯宣布从赫尔松部分地区撤军',
  19. url: 'https://news.ifeng.com/c/8KoK54urn1k',
  20. },
  21. {
  22. title: '俄罗斯宣布从赫尔松部分地区撤军',
  23. url: 'https://news.ifeng.com/c/8KoK54urn1k',
  24. },
  25. ],
  26. },
  27. {
  28. key: 2,
  29. cid: 2,
  30. content: [
  31. {
  32. title: '空战隐身无人僚机亮相!',
  33. url: 'https://news.ifeng.com/c/8KoeDFJXF1b',
  34. },
  35. {
  36. title: '空战隐身无人僚机亮相!',
  37. url: 'https://news.ifeng.com/c/8KoeDFJXF1b',
  38. },
  39. {
  40. title: '空战隐身无人僚机亮相!',
  41. url: 'https://news.ifeng.com/c/8KoeDFJXF1b',
  42. },
  43. ],
  44. },
  45. {
  46. key: 3,
  47. cid: 3,
  48. content: [
  49. {
  50. title: '安康码”上新!家庭成员核酸情况查询更便捷',
  51. url: 'https://ah.ifeng.com/c/8KkGUDhAZNZ',
  52. },
  53. {
  54. title: '安康码”上新!家庭成员核酸情况查询更便捷',
  55. url: 'https://ah.ifeng.com/c/8KkGUDhAZNZ',
  56. },
  57. {
  58. title: '安康码”上新!家庭成员核酸情况查询更便捷',
  59. url: 'https://ah.ifeng.com/c/8KkGUDhAZNZ',
  60. },
  61. ],
  62. },
  63. ];
  64. function createTab(type, content) {
  65. for (let i = 0; i < cates.length; i++) {
  66. const btn = document.createElement('button');
  67. btn.textContent = cates[i].cname;
  68. btn.dataset.key = cates[i].cid;
  69. if (i === 0) btn.classList.add('active');
  70. type.append(btn);
  71. }
  72. // 2. 生成内容
  73. for (let i = 0; i < details.length; i++) {
  74. const ul = document.createElement('ul');
  75. ul.dataset.key = details[i].cid;
  76. ul.classList.add(i === 0 ? 'active' : 'hidden');
  77. for (let k = 0; k < details[i].content.length; k++) {
  78. const li = document.createElement('li');
  79. const a = document.createElement('a');
  80. a.href = details[i].content[k].url;
  81. a.textContent = details[i].content[k].title;
  82. li.append(a);
  83. ul.append(li);
  84. content.append(ul);
  85. }
  86. }
  87. }
  88. function setBtnStatus(ev) {
  89. const currentBtn = ev.target;
  90. [...ev.currentTarget.children].forEach(btn => btn.classList.remove('active'));
  91. currentBtn.classList.add('active');
  92. }
  93. function setContentStatus(ev, currentBtn) {
  94. const lists = document.querySelectorAll('.content > ul');
  95. lists.forEach(list => list.classList.replace('active', 'hidden'));
  96. const currentList = [...lists].find(list => list.dataset.key == currentBtn.dataset.key);
  97. currentList.classList.replace('hidden', 'active');
  98. }
  99. export { createTab, setBtnStatus, setContentStatus };
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:完成的不错
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post