欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

选项卡、轮播图实例

程序员文章站 2022-03-27 08:26:50
...

<!-- 1. 实例演示选项卡 2. 完成购物车的选择计算功能(选做) 3. 完成经典轮播图案例 -->

选项卡实例

  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>JS选项卡实例</title>
  8. <style>
  9. * {
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. .container {
  15. width: 28em;
  16. height: 18em;
  17. display: flex;
  18. flex-direction: column;
  19. margin: auto;
  20. background-color: #eef5d2;
  21. }
  22. .container > div {
  23. margin-top: 1.125rem;
  24. background-color: #fff;
  25. display: grid;
  26. grid-template-columns: repeat(3, 8rem);
  27. justify-content: center;
  28. gap: 1.5rem;
  29. }
  30. .container > div > span {
  31. margin-right: 1.125rem;
  32. }
  33. .container > div > span:nth-of-type(2) {
  34. background-color: #dfa1fc;
  35. }
  36. .container .content {
  37. display: none;
  38. }
  39. .container .content.active {
  40. display: grid;
  41. grid-template-columns: repeat(3, 8rem);
  42. justify-content: center;
  43. gap: 1.5rem;
  44. }
  45. .container .menu button.active {
  46. background-color: #8bf8ea;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="container">
  52. <!-- 1. 标签 -->
  53. <div class="menu" onclick="show()">
  54. <button type="button" data-index="1" class="active">手机</button>
  55. <button type="button" data-index="2">平板</button>
  56. <button type="button" data-index="3">电脑</button>
  57. </div>
  58. <!-- 2. 内容 -->
  59. <div class="content active" data-index="1">
  60. <span><a href="">iphone11</a></span>
  61. <span><a href="">iphone12</a></span>
  62. <span><a href="">iphone13</a></span>
  63. </div>
  64. <div class="content" data-index="2">
  65. <span><a href="">iPad mini</a></span>
  66. <span><a href="">iPad mini</a></span>
  67. <span><a href="">iPad mini</a></span>
  68. </div>
  69. <div class="content" data-index="3">
  70. <span><a href="">MacBook Air</a></span>
  71. <span><a href="">MacBook Pro</a></span>
  72. <span><a href="">MacBook Pro 2022</a></span>
  73. </div>
  74. </div>
  75. <script>
  76. function show() {
  77. // 1. 将原高亮的标签去掉高亮样式,并把当前点击的标签设置为高亮
  78. [...event.currentTarget.children].forEach((span) =>
  79. span.classList.remove("active")
  80. );
  81. event.target.classList.add("active");
  82. // 2. 根据标签的自定义索引属性来确定应该显示哪个列表
  83. // const uls = document.querySelectorAll(".content");
  84. document
  85. .querySelectorAll(".content")
  86. .forEach((span) => span.classList.remove("active"));
  87. // data-index === 左面的标签的data-index
  88. // const content = [...uls].find(ul => ul.dataset.index === li.dataset.index);
  89. [...document.querySelectorAll(".content")]
  90. .find((div) => div.dataset.index === event.target.dataset.index)
  91. .classList.add("active");
  92. // content.classList.add("active");
  93. }
  94. </script>
  95. </body>
  96. </html>

http://help10086.cn/%E9%80%89%E9%A1%B9%E5%8D%A1%E3%80%81%E8%BD%AE%E6%92%AD%E5%9B%BE%E5%AE%9E%E4%BE%8B/demo1.html

轮播图实例

  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. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. .swiper {
  15. width: 1226px;
  16. height: 460px;
  17. margin: 0 auto;
  18. position: relative;
  19. }
  20. .swiper > .imgList {
  21. width: 100%;
  22. height: 100%;
  23. position: relative;
  24. }
  25. .swiper > .imgList > .imgItem {
  26. width: 100%;
  27. height: 100%;
  28. background-size: 100% 100%;
  29. position: absolute;
  30. left: 0;
  31. top: 0;
  32. opacity: 0;
  33. transition: all 0.5s;
  34. }
  35. .swiper > .imgList > .imgItem.active {
  36. opacity: 1;
  37. }
  38. .swiper .btn.pre {
  39. width: 80px;
  40. height: 60px;
  41. line-height: 60px;
  42. text-align: center;
  43. background-color: #00000099;
  44. color: #fff;
  45. position: absolute;
  46. top: calc(50% - 30px);
  47. font-size: 30px;
  48. }
  49. .swiper .btn.next {
  50. width: 80px;
  51. height: 60px;
  52. line-height: 60px;
  53. text-align: center;
  54. background-color: #00000099;
  55. color: #fff;
  56. position: absolute;
  57. top: calc(50% - 30px);
  58. right: 0;
  59. font-size: 30px;
  60. }
  61. /* 设置4个小圆 */
  62. .swiper > .circleList {
  63. width: 100%;
  64. height: 80px;
  65. display: flex;
  66. padding: 0 30px;
  67. justify-content: flex-end;
  68. align-items: center;
  69. position: absolute;
  70. left: 0;
  71. bottom: 0;
  72. }
  73. .swiper > .circleList > .circleItem {
  74. width: 10px;
  75. height: 10px;
  76. border: 2px solid #999;
  77. background-color: #666;
  78. border-radius: 5px;
  79. margin: 0 3px;
  80. }
  81. /* 第一个圆默认,填充颜色 */
  82. .swiper > .circleList > .circleItem.active {
  83. background-color: #ccc;
  84. border: 2px solid #666;
  85. }
  86. </style>
  87. </head>
  88. <body>
  89. <div class="swiper">
  90. <div class="imgList">
  91. <img class="imgItem active" src="images/img_1.jpg" alt="" />
  92. <img class="imgItem" src="images/img_1.jpg" alt="" />
  93. <img class="imgItem" src="images/img_2.jpg" alt="" />
  94. <img class="imgItem" src="images/img_3.jpg" alt="" />
  95. <img class="imgItem" src="images/img_4.jpg" alt="" />
  96. <img class="imgItem" src="images/img_5.jpg" alt="" />
  97. </div>
  98. <div class="btnList">
  99. <div class="btn pre"><</div>
  100. <div class="btn next">></div>
  101. </div>
  102. <!-- 4个小圆形 -->
  103. <div class="circleList">
  104. <div id="c0" class="circleItem active"></div>
  105. <div id="c1" class="circleItem"></div>
  106. <div id="c2" class="circleItem"></div>
  107. <div id="c3" class="circleItem"></div>
  108. <div id="c4" class="circleItem"></div>
  109. </div>
  110. </div>
  111. <script type="text/javascript">
  112. /* 按钮(上) */
  113. let preBtn = document.querySelector(".swiper .pre");
  114. /* 按钮(下) */
  115. let nextBtn = document.querySelector(".swiper .next");
  116. /* 图片列表 */
  117. let imgListDivs = document.querySelectorAll(".swiper .imgItem");
  118. /* 圆点列表 */
  119. let circleDivs = document.querySelectorAll(".swiper .circleItem");
  120. let currentImg = 0; /* 记录当前位置*/
  121. console.log(imgListDivs);
  122. //下一张图
  123. nextBtn.onclick = function () {
  124. currentImg = currentImg + 1;
  125. if (currentImg >= imgListDivs.length) {
  126. currentImg = 0;
  127. }
  128. renderNumImg();
  129. };
  130. //上一张图
  131. preBtn.onclick = function () {
  132. currentImg = currentImg - 1;
  133. if (currentImg < 0) {
  134. currentImg = imgListDivs.length - 1;
  135. }
  136. renderNumImg();
  137. };
  138. function renderNumImg() {
  139. //初始化,将所有的img列表的activ去掉
  140. imgListDivs.forEach(function (item, i) {
  141. item.classList.remove("active");
  142. });
  143. //去掉小圆
  144. circleDivs.forEach(function (item, i) {
  145. item.classList.remove("active"); /* 移除*/
  146. });
  147. //圆点跟着,切换(添加)
  148. imgListDivs[currentImg].classList.add("active");
  149. circleDivs[currentImg].classList.add("active");
  150. //设置点小圆,图片也跟着同步
  151. circleDivs.forEach(function (item, i) {
  152. console.log(item);
  153. item.onclick = function () {
  154. currentImg = i; //把当前的的位置给currentImg
  155. console.log(i);
  156. renderNumImg(); //再调用函数
  157. };
  158. });
  159. }
  160. </script>
  161. </body>
  162. </html>

http://help10086.cn/%E9%80%89%E9%A1%B9%E5%8D%A1%E3%80%81%E8%BD%AE%E6%92%AD%E5%9B%BE%E5%AE%9E%E4%BE%8B/demo3.html