博客列表 >经典选项卡

经典选项卡

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