Blogger Information
Blog 100
fans 8
comment 2
visits 149897
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选项卡案例(javacript原生代码)
lilove的博客
Original
1035 people have browsed it

JS原生代码选项卡

html部分

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>选项卡实例</title>
  7. </head>
  8. <style>
  9. @import url("css/demo1.css");
  10. </style>
  11. <body>
  12. <div class="container">
  13. <ul class="tabs">
  14. <li class="tab-active" data-index=1>选项卡1</li>
  15. <li data-index=2>选项卡2</li>
  16. <li data-index=3>选项卡3</li>
  17. </ul>
  18. <ul class="tab-body" data-index=1>
  19. <li><a href="">选项卡1内容</a></li>
  20. <li><a href="">选项卡1内容</a></li>
  21. <li><a href="">选项卡1内容</a></li>
  22. <li><a href="">选项卡1内容</a></li>
  23. </ul>
  24. <ul class="tab-body tab-hidden" data-index=2>
  25. <li><a href="https://www.php.cn" target="_blank">选项卡2内容</a></li>
  26. <li><a href="">选项卡2内容</a></li>
  27. <li><a href="">选项卡2内容</a></li>
  28. <li><a href="">选项卡2内容</a></li>
  29. </ul>
  30. <ul class="tab-body tab-hidden" data-index=3>
  31. <li><a href="">选项卡3内容</a></li>
  32. <li><a href="">选项卡3内容</a></li>
  33. <li><a href="">选项卡3内容</a></li>
  34. <li><a href="">选项卡3内容</a></li>
  35. </ul>
  36. </div>
  37. </body>
  38. <script src="js/demo1.js"></script>
  39. </html>

CSS部分

  1. /* 选项卡样式表 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. a {
  8. text-decoration: none;
  9. }
  10. li {
  11. list-style-type: none;
  12. }
  13. .container {
  14. width: 300px;
  15. margin: 100px auto;
  16. }
  17. .tabs {
  18. width: 300px;
  19. letter-spacing: -0.5em;
  20. word-spacing: -0.5em;
  21. }
  22. .tabs > li {
  23. width: 100px;
  24. height: 41px;
  25. color: white;
  26. display: inline-block;
  27. padding: 10px;
  28. text-align: center;
  29. background-color: dodgerblue;
  30. letter-spacing: 0;
  31. word-spacing: 0;
  32. }
  33. .tabs > li.tab-active {
  34. background-color: darkblue;
  35. }
  36. .tabs > li:hover {
  37. cursor: default;
  38. }
  39. .tab-body a {
  40. color: white;
  41. display: block;
  42. padding: 10px;
  43. background-color: darkblue;
  44. }
  45. .tab-body a:hover {
  46. color: navy;
  47. background-color: deepskyblue;
  48. }
  49. .tab-hidden {
  50. display: none;
  51. }

javascript部分

  1. // 选项卡js文件
  2. // 获取元素
  3. var tabs = document.querySelector(".tabs");
  4. var tabBody = document.querySelectorAll(".tab-body");
  5. // 给选项卡添加事件监听
  6. // tabs.addEventListener('click', show, false);
  7. tabs.addEventListener('mouseover', show, false);
  8. function show(ev) {
  9. // 1.清除事件默认行为
  10. ev.preventDefault();
  11. // 2.选项卡高亮
  12. ev.currentTarget.childNodes.forEach(function (item) {
  13. if (item.nodeType === 1) {
  14. // 清除其他选项卡高亮样式
  15. item.classList.remove("tab-active");
  16. } else {
  17. // 将点击的选项卡高亮显示
  18. ev.target.classList.add("tab-active");
  19. }
  20. });
  21. // 3.将选项卡对应内容切换(根据data-index属性值判断)
  22. tabBody.forEach(function (item) {
  23. // 只选择和当前点击的选项卡data-index属性相等的显示
  24. if (ev.target.dataset.index === item.dataset.index) {
  25. item.classList.remove('tab-hidden');
  26. } else {
  27. // 隐藏不是当前要显示的选项卡内容
  28. item.classList.add('tab-hidden');
  29. }
  30. });
  31. }

运行效果

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:知道吗?纯css也可以实现选项卡功能, 不写一行js
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