Blogger Information
Blog 19
fans 1
comment 0
visits 15274
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图、选项卡实战
海阔天空
Original
771 people have browsed it

轮播图 选项卡实战

轮播图代码

  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. <span class="point" data-index="4"></span> -->
  91. </div>
  92. <span class="skip prev">&lt;</span>
  93. <span class="skip next">&gt;</span>
  94. </div>
  95. <script>
  96. var cl = console.log.bind(console);
  97. //轮播图片组
  98. var imgs = document.querySelectorAll("img");
  99. cl(imgs);
  100. var pointList = document.querySelector(".point-list");
  101. //动态生成小圆点
  102. imgs.forEach(function (img, index) {
  103. var span = document.createElement("span");
  104. if (index === 0) span.classList.add("point", "active");
  105. span.classList.add("point");
  106. //将小圆点与当前显示的图片的索引进行关联
  107. span.dataset.index = img.dataset.index;
  108. pointList.appendChild(span);
  109. });
  110. // cl(pointList);
  111. //为了正确的设置小圆点高亮,需要获取到所有的小圆点
  112. var points = document.querySelectorAll(".point");
  113. //事件代理/委托:给小圆点添加点击事件
  114. pointList.addEventListener(
  115. "click",
  116. function (ev) {
  117. // cl(ev.target);
  118. imgs.forEach(function (img) {
  119. if (img.dataset.index === ev.target.dataset.index) {
  120. //先取消所有图片的激活属性
  121. imgs.forEach(function (img) {
  122. img.classList.remove("active");
  123. });
  124. //设置当前图片的激活样式
  125. img.classList.add("active");
  126. //公共函数:设置小圆点当前的高亮,必须与图片一一对应,同步
  127. setPointActive(img.dataset.index);
  128. }
  129. });
  130. },
  131. false
  132. );
  133. //公共函数:setPointActive
  134. function setPointActive(imgIndex) {
  135. //清除原来的所有的小圆点上的高亮
  136. points.forEach(function (point) {
  137. point.classList.remove("active");
  138. });
  139. //为当前小黑点设置高亮
  140. points.forEach(function (point) {
  141. if (point.dataset.index === imgIndex) point.classList.add("active");
  142. });
  143. }
  144. //获取翻页按钮
  145. var skip = document.querySelectorAll(".skip");
  146. //添加事件
  147. skip.item(0).addEventListener("click", skipImg, false);
  148. skip.item(1).addEventListener("click", skipImg, false);
  149. //翻页事件回调
  150. function skipImg(ev) {
  151. //遍历并找到当前正在显示的图片
  152. var currentImg = null;
  153. imgs.forEach(function (img) {
  154. if (img.classList.contains("active")) currentImg = img;
  155. });
  156. //1.判断是否是点击了显示前一张
  157. if (ev.target.classList.contains("prev")) {
  158. currentImg.classList.remove("active");
  159. currentImg = currentImg.previousElementSibling;
  160. // cl(currentImg);
  161. //如果存在前一张
  162. if (currentImg !== null && currentImg.nodeName === "IMG")
  163. //显示当前图片
  164. currentImg.classList.add("active");
  165. else {
  166. //否则就显示最后一张,形成循环
  167. currentImg = imgs[imgs.length - 1];
  168. currentImg.classList.add("active");
  169. }
  170. }
  171. //2.判断是否点击了显示下一张
  172. if (ev.target.classList.contains("next")) {
  173. currentImg.classList.remove("active");
  174. currentImg = currentImg.nextElementSibling;
  175. // cl(currentImg);
  176. //如果存在下一张
  177. if (currentImg !== null && currentImg.nodeName === "IMG")
  178. //显示当前图片
  179. currentImg.classList.add("active");
  180. else {
  181. //否则就显示第一张,形成循环
  182. currentImg = imgs[0];
  183. currentImg.classList.add("active");
  184. }
  185. }
  186. //调用公共函数:设置小圆点当前的高亮,与图片一一对应
  187. setPointActive(currentImg.dataset.index);
  188. }
  189. var box = document.querySelector(".box");
  190. var timer = null;
  191. //1.当鼠标移出轮播图区域时,启动定时器,每2秒自动切换图片
  192. box.addEventListener("mouseout", startTimer, false);
  193. //2.当鼠标移入轮播图区域时,清除定时器,由用户点击操作
  194. box.addEventListener("mouseover", clearTimer, false);
  195. //启动定时器
  196. function startTimer() {
  197. var click = new Event("click");
  198. //setInterval():间歇式执行,单位毫秒
  199. if (timer != null) {
  200. //判断计时器是否为空
  201. clearInterval(timer);
  202. timer = null;
  203. }
  204. timer = setInterval(function () {
  205. skip.item(1).dispatchEvent(click);
  206. }, 2000); //启动计时器,调用overs函数,
  207. }
  208. //清除定时器
  209. function clearTimer() {
  210. clearInterval(timer);
  211. }
  212. </script>
  213. </body>
  214. </html>

轮播图效果图

选项卡代码

  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: #555;
  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: 300px;
  29. margin: 30px;
  30. background-color: #ddd;
  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: #fff;
  43. }
  44. .tab li.active {
  45. background-color: #ddd;
  46. }
  47. /* 默认所有选项卡只有一个显示,其它隐藏 */
  48. .item {
  49. padding: 20px;
  50. display: none;
  51. }
  52. .item.active {
  53. display: block;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="tabs">
  59. <!-- 导航 -->
  60. <ul class="tab">
  61. <li class="active" data-index="1">水果</li>
  62. <li data-index="2">手机</li>
  63. <li data-index="3">汽车</li>
  64. </ul>
  65. <!-- 详情1 -->
  66. <ul class="item active" data-index="1">
  67. <li><a href="">哈密瓜</a></li>
  68. <li><a href="">葡萄</a></li>
  69. <li><a href="">香蕉</a></li>
  70. </ul>
  71. <!-- 详情2 -->
  72. <ul class="item" data-index="2">
  73. <li><a href="">华为</a></li>
  74. <li><a href="">小米</a></li>
  75. <li><a href="">OPPO</a></li>
  76. </ul>
  77. <!-- 详情2 -->
  78. <ul class="item" data-index="3">
  79. <li><a href="">吉利</a></li>
  80. <li><a href="">奇瑞</a></li>
  81. <li><a href="https://www.baidu.com">长城</a></li>
  82. </ul>
  83. </div>
  84. <script>
  85. var cl = console.log.bind(console);
  86. //导航:事件委托
  87. var tab = document.querySelector(".tab");
  88. //列表
  89. var items = document.querySelectorAll(".item");
  90. //给导航绑定点击事件
  91. tab.addEventListener("click", show, false);
  92. //给导航绑定鼠标移动事件
  93. tab.addEventListener("mouseover", show, false);
  94. //事件回调函数
  95. function show(ev) {
  96. cl(ev.target);
  97. //1、清空导航原有的激活
  98. ev.target.parentNode.childNodes.forEach(function (item) {
  99. if (item.nodeType === 1) item.classList.remove("active");
  100. });
  101. //2、将当前用户正在点击的导航设置为激活
  102. ev.target.classList.add("active");
  103. //3、清空列表的原有激活
  104. items.forEach(function (item) {
  105. item.classList.remove("active");
  106. });
  107. //4、在列表中查询data-index与导航的data-index相等的内容,将它设置为激活
  108. items.forEach(function (item) {
  109. if (item.dataset.index === ev.target.dataset.index)
  110. item.classList.add("active");
  111. });
  112. }
  113. </script>
  114. </body>
  115. </html>

选项卡效果图

总结:
1.评论、选项卡、轮播图、懒加载是常用功能,老师讲得内容非常实用。
2.js变量、函数区分大小写。编程时需注意。
3.在使用定时器功能时,图片转换时快时慢,鼠标停留在图片上时,不能正常停止播放。对setInterval方法返回的值做了一个判断,判断是否是空值,若不是空值,则要停止定时器并将值设为空,再重新启动,运行完美。

Correcting teacher:天蓬老师天蓬老师

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
Author's latest blog post