Blogger Information
Blog 7
fans 0
comment 0
visits 2142
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模仿完成选项卡
P粉276126820
Original
238 people have browsed it

选项卡用模块形式


HTML

  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. display: none;
  11. }
  12. .active{
  13. display:block
  14. }
  15. .type > *.active,
  16. .content > *.active{
  17. background-color: burlywood;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <div class="type" style="display: flex">
  24. </div>
  25. <div class="content">
  26. </div>
  27. </div>
  28. </body>
  29. <script type="module">
  30. import * as tabs from './tabs.js'
  31. // console.log(cates)
  32. // console.log(details)
  33. const type = document.querySelector('.type');
  34. const content = document.querySelector('.content');
  35. window.onload = tabs.createTab(type,content)
  36. type.onclick = ev => {
  37. // 1. 设置栏目高亮
  38. console.log(111);
  39. tabs.setBtnStatus(ev)
  40. // 2. 激活与当前栏目对应的新闻列表
  41. tabs.setContentStatus(ev.target)
  42. }
  43. </script>
  44. </html>

JS

  1. // 1. 栏目
  2. const cates = [
  3. { cid: 1, cname: '中国新闻' },
  4. { cid: 2, cname: '国际新闻' },
  5. { cid: 3, cname: '安徽新闻' },
  6. ]
  7. // 2. 内容
  8. // 内容的key 必须与 栏目的id绑定
  9. const details = [
  10. {
  11. key: 1,
  12. cid: 1,
  13. content: [
  14. {
  15. title: '蔡英文窜美,美官员警告中国不要过度反应,外交部回应',
  16. url: 'https://news.ifeng.com/c/8OYFjT9c3KM',
  17. },
  18. {
  19. title: '马英九:从来没有一次像今天受到这么大的冲击',
  20. url: 'https://news.ifeng.com/c/8OYI5PZyyRs',
  21. },
  22. {
  23. title: '美媒:王毅与沙利文进行电话交谈',
  24. url: 'https://news.ifeng.com/c/8OXw5JWvnRM',
  25. },
  26. ],
  27. },
  28. {
  29. key: 2,
  30. cid: 2,
  31. content: [
  32. {
  33. title: '美核航母抵韩,半岛局势会失控吗?',
  34. url: 'https://news.ifeng.com/c/8OYApVnV1mN',
  35. },
  36. {
  37. title: '西班牙法律允许强占住房:有华人3年没回西班牙,房子被吉普赛人占了',
  38. url: 'https://news.ifeng.com/c/8OXfZLPso8P',
  39. },
  40. {
  41. title: '拜登首次就普京拟在白俄部署核武器表态',
  42. url: 'https://news.ifeng.com/c/8OXdmTTNQD6',
  43. },
  44. ],
  45. },
  46. {
  47. key: 3,
  48. cid: 3,
  49. content: [
  50. {
  51. title: '省级党政代表团密集赴皖考察!安徽究竟有何看点?',
  52. url: 'https://ah.ifeng.com/c/8OXtD8eq0pA',
  53. },
  54. {
  55. title: '合肥、蚌埠、亳州、安庆、宣城最新人事任免!',
  56. url: 'https://ah.ifeng.com/c/8OXnxW9z3K5',
  57. },
  58. {
  59. title: '下月起合肥坐高铁到香港!最快只需7时26分',
  60. url: 'https://ah.ifeng.com/c/8OXheuq5n55',
  61. },
  62. ],
  63. },
  64. ]
  65. // 3. 创建栏目和内容
  66. function createTab(type,content){
  67. // 1. 生成栏目
  68. for(let i=0;i<cates.length;i++){
  69. let typ_bnt = document.createElement('button')
  70. typ_bnt.dataset.key=cates[i].cid
  71. typ_bnt.textContent=cates[i].cname
  72. // 第一个按钮应该是高亮显示
  73. if(i===0){
  74. typ_bnt.classList.add('active')
  75. }
  76. type.append(typ_bnt)
  77. }
  78. for(let i=0; i<details.length; i++){
  79. //创建ul
  80. const ulList = document.createElement('ul')
  81. // 为每个<ul>添加自定义属性data-key
  82. ulList.dataset.key = details[i].cid
  83. // 全部内容加载时,默认全隐藏,只要显示第一组新闻列表
  84. ulList.classList.add(i===0 ? 'active' : 'hidden')
  85. // 循环: 将与列表对应的数据全部渲染出来
  86. for(let j=0; j < details[i].content.length; j++){
  87. const liList = document.createElement('li')
  88. const a = document.createElement('a')
  89. a.textContent = details[i].content[j].title
  90. a.href = details[i].content[j].url
  91. liList.append(a)
  92. ulList.append(liList)
  93. content.append(ulList)
  94. }
  95. }
  96. }
  97. // 4. 自动设置栏目高亮
  98. function setBtnStatus(ev){
  99. // 去掉所有激活样式
  100. [...ev.currentTarget.children].forEach(btn=>btn.classList.remove('active'))
  101. // 2. 将当前用户正在点击的按钮添加active
  102. ev.target.classList.add('active')
  103. }
  104. // 5. 设置与栏目对应的内容的激活状态
  105. function setContentStatus(currentBtn){
  106. // 所有的新闻列表<ul>
  107. const ulList = document.querySelectorAll('.content > ul')
  108. // 1. 将激活的列表全部隐藏 active -> hidden
  109. ulList.forEach(list=>list.classList.replace('active','hidden'))
  110. // 2. 找到与栏目ID相同(对应的)新闻列表<ul>
  111. const curList = [...ulList].find(list=>list.dataset.key===currentBtn.dataset.key)
  112. // 3. 设置当前列表为激活active
  113. curList.classList.replace('hidden','active')
  114. }
  115. export {cates, details, 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