博客列表 >JS选项卡与轮播图实战

JS选项卡与轮播图实战

蔚蓝世纪
蔚蓝世纪原创
2020年05月27日 16:04:31745浏览

一、JS实现选项卡
代码输出效果在线预览:
选项卡效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>选项卡</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. a {
  13. text-decoration: none;
  14. color: lightseagreen;
  15. }
  16. a:hover {
  17. text-decoration: underline;
  18. color: red;
  19. }
  20. li {
  21. list-style: none;
  22. }
  23. li:hover {
  24. cursor: default;
  25. }
  26. .tabs {
  27. width: 300px;
  28. height: 200px;
  29. margin: 30px;
  30. background-color: #efefef;
  31. display: flex;
  32. flex-direction: column;
  33. }
  34. .tab {
  35. height: 36px;
  36. display: flex;
  37. }
  38. .tab li {
  39. flex: auto;
  40. text-align: center;
  41. line-height: 36px;
  42. background-color: lightseagreen;
  43. }
  44. .tab li.active {
  45. background-color: #ddd;
  46. }
  47. .item {
  48. padding: 20px;
  49. display: none;
  50. }
  51. .item.active {
  52. display: block;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div class="tabs">
  58. <!-- 导航 -->
  59. <ul class="tab">
  60. <li class="active" data-index="1">声乐</li>
  61. <li data-index="2">舞蹈</li>
  62. <li data-index="3">器乐</li>
  63. </ul>
  64. <!-- 详情1 -->
  65. <ul class="item active" data-index="1">
  66. <li><a href="">美声唱法</a></li>
  67. <li><a href="">通俗唱法</a></li>
  68. <li><a href="">民族唱法</a></li>
  69. </ul>
  70. <!-- 详情2 -->
  71. <ul class="item" data-index="2">
  72. <li><a href="">古典舞</a></li>
  73. <li><a href="">现代舞</a></li>
  74. <li><a href="">民族舞</a></li>
  75. </ul>
  76. <!-- 详情2 -->
  77. <ul class="item" data-index="3">
  78. <li><a href="">古筝</a></li>
  79. <li><a href="">琵琶</a></li>
  80. <li><a href="">扬琴</a></li>
  81. </ul>
  82. </div>
  83. <script>
  84. // 导航:事件委托
  85. var tab = document.querySelector(".tab");
  86. // 获取列表
  87. var items = document.querySelectorAll(".item");
  88. // 给导航绑定点击事件
  89. tab.addEventListener("click", show, false);
  90. // 给导航绑定鼠标移动事件
  91. tab.addEventListener("mouseover", show, false);
  92. // 事件回调函数
  93. function show(ev) {
  94. console.log(ev.target);
  95. // 1. 清空导航原有的激活状态
  96. ev.target.parentNode.childNodes.forEach(function (item) {
  97. if (item.nodeType === 1) item.classList.remove("active");
  98. });
  99. // 2. 将当前用户正在点击的导航设置为激活状态
  100. ev.target.classList.toggle("active");
  101. // 3. 清空列表原有的激活状态
  102. items.forEach(function (item) {
  103. item.classList.remove("active");
  104. });
  105. // 4. 在列表中查询data-index与导航的data-index相等的内容,将它设置为激活
  106. items.forEach(function (item) {
  107. if (item.dataset.index === ev.target.dataset.index)
  108. item.classList.add("active");
  109. });
  110. }
  111. </script>
  112. </body>
  113. </html>

输出效果:

二、JS实现轮播图
代码输出效果在线预览
轮播图效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>轮播图</title>
  6. <style>
  7. ul,
  8. li {
  9. margin: 0;
  10. padding: 0;
  11. list-style: none;
  12. }
  13. .box {
  14. /*定位父级*/
  15. position: relative;
  16. width: 1000px;
  17. height: 350px;
  18. margin: 0 auto;
  19. }
  20. .box .slider {
  21. width: 1000px;
  22. height: 350px;
  23. display: none;
  24. }
  25. .box .slider.active {
  26. display: block;
  27. }
  28. .box .point-list {
  29. position: absolute;
  30. /*绝对定位的环境下的水平居中方式*/
  31. left: 50%;
  32. margin-left: -38px;
  33. top: 310px;
  34. }
  35. .box .point-list .point {
  36. display: inline-block;
  37. width: 12px;
  38. height: 12px;
  39. margin: 0 5px;
  40. background-color: white;
  41. border-radius: 100%;
  42. }
  43. .box .point-list .point.active {
  44. background-color: black;
  45. }
  46. .box .point-list .point:hover {
  47. cursor: pointer;
  48. }
  49. .skip {
  50. position: absolute;
  51. top: 140px;
  52. display: inline-block;
  53. width: 40px;
  54. height: 80px;
  55. text-align: center;
  56. line-height: 80px;
  57. background-color: lightgray;
  58. color: white;
  59. opacity: 0.2;
  60. font-size: 36px;
  61. }
  62. .box .prev {
  63. left: 0;
  64. }
  65. .box .next {
  66. right: 0;
  67. }
  68. .box .skip:hover {
  69. cursor: pointer;
  70. opacity: 0.5;
  71. color: black;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="box">
  77. <img
  78. src="banner/banner1.jpg"
  79. alt=""
  80. data-index="1"
  81. class="slider active"
  82. />
  83. <img src="banner/banner2.jpg" alt="" data-index="2" class="slider" />
  84. <img src="banner/banner3.jpg" alt="" data-index="3" class="slider" />
  85. <img src="banner/banner4.jpg" alt="" data-index="4" class="slider" />
  86. <div class="point-list">
  87. <!-- <span class="point active" data-index="1"></span>
  88. <span class="point" data-index="2"></span>
  89. <span class="point" data-index="3"></span> -->
  90. </div>
  91. <span class="skip prev">&lt;</span>
  92. <span class="skip next">&gt;</span>
  93. </div>
  94. <script>
  95. // 获取轮播图片组
  96. var imgs = document.querySelectorAll("img");
  97. var pointList = document.querySelector(".point-list");
  98. // 动态生成小圆点
  99. imgs.forEach(function (img, index) {
  100. var span = document.createElement("span");
  101. if (index === 0) span.classList.add("point", "active");
  102. span.classList.add("point");
  103. // 将小圆点与当前显示的图片的索引进行关联
  104. span.dataset.index = img.dataset.index;
  105. pointList.appendChild(span);
  106. });
  107. // 为了正确的设置小圆点高亮显示, 这里需要获取到所有的小圆点
  108. var points = document.querySelectorAll(".point");
  109. // 事件代理/委托: 给小圆点添加点击事件
  110. pointList.addEventListener(
  111. "click",
  112. function (ev) {
  113. console.log(ev.target);
  114. imgs.forEach(function (img) {
  115. if (img.dataset.index === ev.target.dataset.index) {
  116. imgs.forEach(function (img) {
  117. img.classList.remove("active"); //移除原来图片上的激活样式
  118. });
  119. img.classList.add("active"); // 设置当前图片的激活样式
  120. // 公共函数: 设置小圆点当前的高亮, 必须与图片一一对应, 或者同步
  121. setPointActive(img.dataset.index);
  122. }
  123. });
  124. },
  125. false
  126. );
  127. // 公共函数:setPointActive
  128. function setPointActive(imgIndex) {
  129. // 清除原来的所有的小圆点上的高亮激活样式
  130. points.forEach(function (point) {
  131. point.classList.remove("active");
  132. });
  133. points.forEach(function (point) {
  134. if (point.dataset.index === imgIndex) point.classList.add("active");
  135. });
  136. }
  137. // 获取翻页按钮
  138. var skip = document.querySelectorAll(".skip");
  139. // 添加事件
  140. skip.item(0).addEventListener("click", skipImg, false);
  141. skip.item(1).addEventListener("click", skipImg, false);
  142. // 翻页事件回调skipImg
  143. function skipImg(ev) {
  144. // 遍历并找到当前正在显示的图片
  145. var currentImg = null;
  146. imgs.forEach(function (img) {
  147. if (img.classList.contains("active")) currentImg = img;
  148. });
  149. // 1. 判断是否是点击了显示前一张
  150. if (ev.target.classList.contains("prev")) {
  151. currentImg.classList.remove("active");
  152. currentImg = currentImg.previousElementSibling;
  153. console.log(currentImg);
  154. // 如果存在前一张
  155. if (currentImg !== null && currentImg.nodeName === "IMG")
  156. // 将当前图片显示
  157. currentImg.classList.add("active");
  158. else {
  159. //否则就显示最后一张,形成循环
  160. currentImg = imgs[imgs.length - 1];
  161. currentImg.classList.add("active");
  162. }
  163. }
  164. // 2. 判断是否是点击了显示下一张
  165. if (ev.target.classList.contains("next")) {
  166. currentImg.classList.remove("active");
  167. currentImg = currentImg.nextElementSibling;
  168. console.log(currentImg);
  169. // 如果存在前一张
  170. if (currentImg !== null && currentImg.nodeName === "IMG")
  171. // 将当前图片显示
  172. currentImg.classList.add("active");
  173. else {
  174. //否则就显示最后一张,形成循环
  175. currentImg = imgs[0];
  176. currentImg.classList.add("active");
  177. }
  178. }
  179. // 公共函数: 设置小圆点当前的高亮, 必须与图片一一对应, 同步
  180. setPointActive(currentImg.dataset.index);
  181. }
  182. //获取整个轮播图
  183. var box = document.querySelector(".box");
  184. //创建定时器
  185. var timer = null;
  186. // 1. 当鼠标移出轮播图区域时, 启动定时器,每2秒自动切换图片
  187. box.addEventListener("mouseout", startTimer, false);
  188. // 2. 当鼠标移入轮播图区域时,清除定时器,由用户点击操作
  189. box.addEventListener("mouseover", clearTimer, false);
  190. // 启动定时器
  191. function startTimer() {
  192. var click = new Event("click");
  193. //setInterval(): 间歇式执行
  194. setInterval(function () {
  195. skip.item(1).dispatchEvent(click);
  196. }, 2000);
  197. }
  198. // 清除定时器
  199. function clearTimer() {
  200. clearInterval(timer);
  201. }
  202. </script>
  203. </body>
  204. </html>

输出效果:

三、总结
平时浏览网页常见的选项卡和轮播图效果,原来实现起来的步骤确实简单,但是切记每实现一个步骤的代码要及时打印检查,及时纠正,之前学习过的零散知识点,只是死记着,做案例时才明白原来是要用在哪里。真的是理论来源于实践,实践反过来又验证了理论。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议