Blogger Information
Blog 17
fans 0
comment 0
visits 14800
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js基础知识:图片懒加载及导航图手动轮播、自动轮播实例应用
未来星
Original
784 people have browsed it

一、 图片懒加载

对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载。当载入页面时,先把可视区域内的img标签的data-src属性值赋给src加载图片,然后监听滚动事件,把进入可视区域的图片src赋值加载。这样便实现了懒加载。
<img src="images/temp.jpg" data-src="images/img-1.jpg" />

元素属性 说明
clientHeight 视口高度
scrollTop 滚动距离
offsetTop 图片距父级上偏移

当图片的offsetTop属性小于clientHeight+scrollTop属性之和时,即处于可视区域内。

进入可视区域内的图片按照设置延时进行img.src替换。

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. .container {
  10. width: 400px;
  11. display: grid;
  12. gap: 10px;
  13. grid-template-columns: repeat(4, 1fr);
  14. }
  15. .container img {
  16. width: 100%;
  17. border-radius: 20px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <img src="images2/temp.jpg" alt="" data-src="images2/img-1.jpg" />
  24. <img src="images2/temp.jpg" alt="" data-src="images2/img-2.jpg" />
  25. <img src="images2/temp.jpg" alt="" data-src="images2/img-3.jpg" />
  26. <img src="images2/temp.jpg" alt="" data-src="images2/img-4.jpg" />
  27. <img src="images2/temp.jpg" alt="" data-src="images2/img-5.jpg" />
  28. <img src="images2/temp.jpg" alt="" data-src="images2/img-6.jpg" />
  29. <img src="images2/temp.jpg" alt="" data-src="images2/img-7.jpg" />
  30. <img src="images2/temp.jpg" alt="" data-src="images2/img-8.jpg" />
  31. <img src="images2/temp.jpg" alt="" data-src="images2/img-9.jpg" />
  32. <img src="images2/temp.jpg" alt="" data-src="images2/img-10.jpg" />
  33. ......
  34. </div>
  35. <script>
  36. // 三个重点:视口高度clientHeight、滚动距离scrollTop、图片距父级上偏移offsetTop
  37. // 获取图片元素集
  38. const imgs = document.querySelectorAll(".container >img");
  39. // console.log(imgs);
  40. const clientHeight = document.documentElement.clientHeight;
  41. // 监听窗口事件,初次载入时、滚动时
  42. window.addEventListener("load", lazyLoad);
  43. window.addEventListener("scroll", lazyLoad);
  44. // 懒加载回调函数
  45. function lazyLoad() {
  46. let scrollTop = document.documentElement.scrollTop;
  47. // 遍历所有图片,判断加载条件
  48. imgs.forEach((img) => {
  49. if (img.offsetTop < clientHeight + scrollTop) {
  50. // 延时处理
  51. setTimeout(() => {
  52. img.src = img.dataset.src;
  53. }, 600);
  54. }
  55. });
  56. }
  57. </script>
  58. </body>
  59. </html>

二、导航图轮播

利用事件监听addEventListener()、属性修改remove()/add()、定时器setInterval()、事件派发dispatchEvent()等知识点实现轮播功能:

  • 根据图片数量动态生成底部圆点按钮;
  • 底部圆点按钮控制切换图片;
  • 左右两侧按钮控制切换图片;
  • 加载后自动开启轮播图片;
  • 响应鼠标动作停止/开启自动轮播;

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. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. }
  14. a {
  15. text-decoration: none;
  16. }
  17. /* 容器设置 */
  18. .container {
  19. width: 50em;
  20. height: 20em;
  21. margin: 1em auto;
  22. position: relative; //定位元素,父级
  23. }
  24. /* 图片组 */
  25. .container > .imgs img {
  26. width: 100%;
  27. height: 100%;
  28. position: absolute;
  29. left: 0;
  30. top: 0;
  31. display: none;
  32. }
  33. .container > .imgs img.active {
  34. display: block;
  35. }
  36. /* 下方圆点按钮 */
  37. .container > .btns {
  38. position: absolute;
  39. left: 0;
  40. right: 0;
  41. bottom: 0;
  42. text-align: center;
  43. }
  44. .container > .btns a {
  45. display: inline-block;
  46. padding: 0.5em;
  47. margin: 0 0.2em;
  48. background-color: white;
  49. border-radius: 50%;
  50. }
  51. .container > .btns a.active {
  52. background-color: black;
  53. }
  54. /* 两侧翻页按钮 */
  55. .container > .skip a {
  56. position: absolute;
  57. width: 2.5rem;
  58. height: 5rem;
  59. top: 7rem;
  60. opacity: 0.3;
  61. line-height: 4.6rem;
  62. font-size: 2rem;
  63. font-weight: lighter;
  64. text-align: center;
  65. background-color: white;
  66. }
  67. .container > .skip > .prev {
  68. left: 0;
  69. }
  70. .container > .skip > .next {
  71. right: 0;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="container">
  77. <!-- 轮播图片 -->
  78. <nav class="imgs">
  79. <a href="#"><img src="images/banner1.jpg" data-index="1" class="active" /></a>
  80. <a href="#"><img src="images/banner2.jpg" data-index="2" /></a>
  81. <a href="#"><img src="images/banner3.jpg" data-index="3" /></a>
  82. <a href="#"><img src="images/banner4.jpg" data-index="4" /></a>
  83. </nav>
  84. <!-- 图片下方对应小按钮 -->
  85. <nav class="btns"></nav>
  86. <!-- 图片左右侧切换按钮 -->
  87. <nav class="skip">
  88. <a href="#" class="prev">&lt;</a>
  89. <a href="#" class="next">&gt;</a>
  90. </nav>
  91. </div>
  92. <script>
  93. // 获取元素
  94. const imgs = document.querySelectorAll(".imgs img");
  95. const btnG = document.querySelector(".btns");
  96. // 根据图片数量创建下方圆点按钮
  97. function autoCreatebtns(ele, imgLength) {
  98. // 复用文档片断来简化编程
  99. const frag = document.createDocumentFragment();
  100. for (i = 0; i < imgLength; i++) {
  101. const a = document.createElement("a");
  102. a.href = "#";
  103. a.dataset.index = i + 1;
  104. if (i == 0) a.classList.add("active");
  105. frag.append(a);
  106. }
  107. ele.append(frag);
  108. }
  109. autoCreatebtns(btnG, imgs.length);
  110. // 1、 圆点按钮事件
  111. const btns = document.querySelectorAll(".btns >*");
  112. // console.log(btns);
  113. // 拿到激活元素
  114. function getActiveEle(ele) {
  115. let activeEles = [...ele].filter((arr) => arr.classList.contains("active"));
  116. // console.log(activeEles[0]);
  117. return activeEles[0];
  118. }
  119. // 设置激活
  120. function setActiveEle(btnindex) {
  121. [imgs, btns].forEach((arr) => {
  122. getActiveEle(arr).classList.remove("active");
  123. arr.forEach((item) => {
  124. if (item.dataset.index == btnindex) {
  125. item.classList.add("active");
  126. }
  127. });
  128. });
  129. }
  130. // 圆点按钮提交 , 监听点击动作
  131. btns.forEach((btn) => btn.addEventListener("click", (ev) => setActiveEle(ev.target.dataset.index)));
  132. // 2、两侧按钮事件
  133. // 获取按钮元素集
  134. const skip = document.querySelectorAll(".skip a");
  135. // 拿到当前激活元素index
  136. function getActiveIndx(ele) {
  137. let activeEles = [...ele].filter((arr) => arr.classList.contains("active"));
  138. return activeEles[0].dataset.index;
  139. }
  140. skip.forEach((item) => {
  141. item.addEventListener("click", (ev) => {
  142. let index = getActiveIndx(imgs) * 1;
  143. switch (ev.target.className) {
  144. case "prev":
  145. // 左侧按钮图片激活index递减,到1循环
  146. index = index == 1 ? imgs.length : index - 1;
  147. setActiveEle(index);
  148. break;
  149. case "next":
  150. //右侧按钮图片激活index递增,到length循环
  151. index = index == imgs.length ? 1 : index + 1;
  152. setActiveEle(index);
  153. break;
  154. }
  155. });
  156. });
  157. // 3、自动循环轮播
  158. // 轮播函数
  159. function autoPlay(speed) {
  160. // 创建定时器
  161. timer = setInterval(() => {
  162. const ev = new Event("click");
  163. //给右侧按钮派发click事件
  164. document.querySelector(".next").dispatchEvent(ev);
  165. }, speed);
  166. }
  167. // 页面加载后开始轮播
  168. window.onload = () => autoPlay(2000);
  169. //鼠标移入时关闭轮播
  170. document.querySelector(".container").onmouseover = () => clearInterval(timer);
  171. //鼠标移出时启动轮播
  172. document.querySelector(".container").onmouseout = () => autoPlay(2000);
  173. //
  174. </script>
  175. </body>
  176. </html>
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