jQuery 平滑响应选项卡/Accodion UI

  • 源码大小:9.94KB
  • 所需积分:1积分
  • 源码编号:19JP-3616
  • 浏览次数:1183次
  • 最后更新:2023年07月02日
  • 所属栏目:手风琴效果
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

如果设计得当,选项卡可以是一个非常方便和有用的界面元素。然而,当构建在移动设备等小屏幕上工作的响应式网站时,基于选项卡的界面可能会成为一个主要问题,尤其是当很难通过CSS修改其外观时。

您可以尝试通过插入媒体查询来修改选项卡的HTML代码。然而,这是一项非常困难的任务,因为您需要手动更改HTML,这在您考虑时有点疯狂。

或者,您可以使用jQuery响应式选项卡我将在本教程中介绍的脚本,将选项卡变成移动设备等小屏幕上的手风琴式界面,这样您就可以使用所有空间来获取更重要的信息。

如何使用它:

1.按照如下所示的HTML结构,将基于按钮的选项卡导航和选项卡式内容添加到选项卡UI中。

  1. <section class="accordion">
  2.  
  3. <!-- Tabs -->
  4. <section class="accordion-tabs">
  5. <button class="accordion-tab accordion-active" data-actab-group="0" data-actab-id="0">Tab 1</button>
  6. <button class="accordion-tab" data-actab-group="0" data-actab-id="1">Tab 2</button>
  7. <button class="accordion-tab" data-actab-group="0" data-actab-id="2">Tab 3</button>
  8. ... more tabs here ...
  9. </section>
  10.  
  11. <!-- Tabbed Content -->
  12. <section class="accordion-content">
  13. <article class="accordion-item accordion-active" data-actab-group="0" data-actab-id="0">
  14. <h4 class="accordion-item__label">Title 1</h4>
  15. <div class="accordion-item__container">
  16. <p>Content 1</p>
  17. </div>
  18. </article>
  19. <article class="accordion-item" data-actab-group="0" data-actab-id="1">
  20. <h4 class="accordion-item__label">Title 2</h4>
  21. <div class="accordion-item__container">
  22. <p>Content 2</p>
  23. </div>
  24. </article>
  25. <article class="accordion-item" data-actab-group="0" data-actab-id="2">
  26. <h4 class="accordion-item__label">Title 3</h4>
  27. <div class="accordion-item__container">
  28. <p>Content 3</p>
  29. </div>
  30. </article>
  31. ... more tabbed content here ...
  32. </section>
  33. </section>

2.选项卡UI的主要CSS样式。

  1. .accordion {
  2. width: 96%;
  3. overflow: hidden;
  4. background: white;
  5. border-radius: 10px;
  6. box-shadow: 0 10px 20px -10px rgba(0, 0, 0, 0.3);
  7. }
  8.  
  9. .accordion-tabs {
  10. display: none;
  11. }
  12.  
  13. .accordion-tabs :focus {
  14. outline: none;
  15. }
  16.  
  17. .accordion-item {
  18. border-bottom: 1px solid #c1d7e2;
  19. }
  20.  
  21. .accordion-item:last-child {
  22. border: none;
  23. }
  24.  
  25. .accordion-item__label {
  26. position: relative;
  27. margin: 0;
  28. padding: 20px;
  29. cursor: pointer;
  30. transition: padding 0.2s ease;
  31. }
  32.  
  33. .accordion-item__label::after {
  34. content: "";
  35. position: absolute;
  36. top: -4px;
  37. right: 20px;
  38. bottom: 0;
  39. width: 6px;
  40. height: 6px;
  41. margin: auto;
  42. transform: rotate(45deg);
  43. opacity: 1;
  44. transition: opacity 0.1s ease;
  45. border-radius: 2px;
  46. border: 5px solid transparent;
  47. border-color: transparent #003852 #003852 transparent;
  48. }
  49.  
  50. .accordion-item__label:hover {
  51. background: #c1d7e2;
  52. }
  53.  
  54. .accordion-item__container {
  55. height: 0;
  56. padding: 0 20px;
  57. overflow: hidden;
  58. opacity: 0;
  59. transition: padding 0.2s ease, opacity 0.5s 0.15s ease;
  60. }
  61.  
  62. .accordion-active {
  63. background: #d2e2ea;
  64. }
  65.  
  66. .accordion-active .accordion-item__label {
  67. padding-bottom: 0;
  68. color: #003852;
  69. cursor: inherit;
  70. }
  71.  
  72. .accordion-active .accordion-item__label:hover {
  73. background: none;
  74. }
  75.  
  76. .accordion-active .accordion-item__label::after {
  77. opacity: 0;
  78. }
  79.  
  80. .accordion-active .accordion-item__container {
  81. height: auto;
  82. padding: 20px;
  83. opacity: 1;
  84. }
  85.  
  86. .accordion-active .accordion-item__container p,
  87. .accordion-active .accordion-item__container h4 {
  88. color: #7baac1;
  89. }
  90.  
  91. .accordion-active .accordion-item__container p:first-child,
  92. .accordion-active .accordion-item__container h4:first-child, {
  93. margin-top: 0;
  94. }
  95.  
  96. .accordion-active .accordion-item__container p:last-child,
  97. .accordion-active .accordion-item__container h4:last-child, {
  98. margin-bottom: 0;
  99. }

3.使用CSS媒体查询,当屏幕大小小于600px时,将选项卡UI变成手风琴。

  1. @media (min-width: 600px) {
  2. .accordion {
  3. width: 100%;
  4. max-width: 600px;
  5. }
  6. }
  7.  
  8. @media (min-width: 600px) {
  9. .accordion-tabs {
  10. display: flex;
  11. background: #d2e2ea;
  12. }
  13. .accordion-tabs .accordion-tab {
  14. flex: 1;
  15. padding: 20px;
  16. font: inherit;
  17. border: none;
  18. cursor: pointer;
  19. color: #003852;
  20. background: #d2e2ea;
  21. transition: background 0.1s ease;
  22. }
  23. .accordion-tabs .accordion-tab:hover {
  24. background: #c1d7e2;
  25. }
  26. .accordion-tabs .accordion-tab:last-child {
  27. border-right: 0;
  28. }
  29. .accordion-tabs .accordion-tab.accordion-active {
  30. color: #006b99;
  31. background: white;
  32. }
  33. .accordion-item {
  34. display: none;
  35. min-height: 260px;
  36. padding: 30px;
  37. border: none;
  38. background: white;
  39. }
  40. .accordion-item__label,
  41. .accordion-item__container {
  42. padding: 0;
  43. transition: inherit;
  44. }
  45. .accordion-item__label {
  46. margin-bottom: 20px;
  47. }
  48. .accordion-item.accordion-active {
  49. display: block;
  50. }
  51. .accordion-item.accordion-active .accordion-item__container {
  52. padding: 0;
  53. }
  54. }

4.在文档末尾加载必要的jQuery库(slim构建)。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>

5.启用选项卡(可折叠标题)以在选项卡式内容(可折叠面板)之间切换。

  1. $(document).ready(function () {
  2. console.log("document ready");
  3.  
  4. const labels = document.querySelectorAll(".accordion-item__label");
  5. const tabs = document.querySelectorAll(".accordion-tab");
  6.  
  7. function toggleShow() {
  8. const target = this;
  9. const item = target.classList.contains("accordion-tab") ?
  10. target :
  11. target.parentElement;
  12. const group = item.dataset.actabGroup;
  13. const id = item.dataset.actabId;
  14.  
  15. tabs.forEach(function (tab) {
  16. if (tab.dataset.actabGroup === group) {
  17. if (tab.dataset.actabId === id) {
  18. tab.classList.add("accordion-active");
  19. } else {
  20. tab.classList.remove("accordion-active");
  21. }
  22. }
  23. });
  24.  
  25. labels.forEach(function (label) {
  26. const tabItem = label.parentElement;
  27.  
  28. if (tabItem.dataset.actabGroup === group) {
  29. if (tabItem.dataset.actabId === id) {
  30. tabItem.classList.add("accordion-active");
  31. } else {
  32. tabItem.classList.remove("accordion-active");
  33. }
  34. }
  35. });
  36. }
  37.  
  38. labels.forEach(function (label) {
  39. label.addEventListener("click", toggleShow);
  40. });
  41.  
  42. tabs.forEach(function (tab) {
  43. tab.addEventListener("click", toggleShow);
  44. });
  45.  
  46. });

预览截图