Blogger Information
Blog 52
fans 1
comment 1
visits 38782
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图和懒加载
小丑0o鱼
Original
789 people have browsed it
  1. 1.懒加载原理
  2. 一张图片就是一个<img>标签,浏览器发起图片请求,请求是根据<img>的src属性加载图片,所以实现懒加载的关键就是,在图片没有进入可视区域时,先不给<img>的src赋值,这样浏览器就加载不到图片了,等到图片进入可视区域再给src赋值。
  3. 2.懒加载思路及实现
  4. 实现懒加载有四个步骤,如下:
  5. 1.加载loading定位图片
  6. 2.判断哪些图片要加载【重点】
  7. 3.隐形加载图片
  8. 4.替换真图片
  9. 懒加载原理
  10. 视口高度:document.documentElement.clientHeight
  11. 当前元素相对于其 offsetParent 元素的顶部的距离:HTMLElement.offsetTop
  12. 滚动条移动高度:document.documentElement.scrollTop
  13. 如上图所示,当图片距离顶部的距离offsetTop等于可视区域clientHeight和滚动区域高度scrollTop之和时说明图片马上就要进入可视区了,就是说当offsetTop<=clientHeight + scrollTop时,图片在可视区。
  14. 示例
  15. 懒加载
  16. css
  17. <style>
  18. .container {
  19. width: 500px;
  20. display: grid;
  21. grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  22. }
  23. .container img {
  24. width: 100%;
  25. }
  26. </style>
  27. html
  28. <div class="container">
  29. <img src="images/temp.jpg" alt="" data-src="images/img-1.jpg">
  30. <img src="images/temp.jpg" alt="" data-src="images/img-2.jpg">
  31. <img src="images/temp.jpg" alt="" data-src="images/img-3.jpg">
  32. <img src="images/temp.jpg" alt="" data-src="images/img-4.jpg">
  33. <img src="images/temp.jpg" alt="" data-src="images/img-5.jpg">
  34. <img src="images/temp.jpg" alt="" data-src="images/img-6.jpg">
  35. <img src="images/temp.jpg" alt="" data-src="images/img-7.jpg">
  36. <img src="images/temp.jpg" alt="" data-src="images/img-8.jpg">
  37. <img src="images/temp.jpg" alt="" data-src="images/img-9.jpg">
  38. <img src="images/temp.jpg" alt="" data-src="images/img-10.jpg">
  39. <img src="images/temp.jpg" alt="" data-src="images/img-11.jpg">
  40. <img src="images/temp.jpg" alt="" data-src="images/img-12.jpg">
  41. <img src="images/temp.jpg" alt="" data-src="images/img-13.jpg">
  42. <img src="images/temp.jpg" alt="" data-src="images/img-14.jpg">
  43. <img src="images/temp.jpg" alt="" data-src="images/img-15.jpg">
  44. <img src="images/temp.jpg" alt="" data-src="images/img-16.jpg">
  45. <img src="images/temp.jpg" alt="" data-src="images/img-17.jpg">
  46. <img src="images/temp.jpg" alt="" data-src="images/img-18.jpg">
  47. <img src="images/temp.jpg" alt="" data-src="images/img-19.jpg">
  48. <img src="images/temp.jpg" alt="" data-src="images/img-20.jpg">
  49. <img src="images/temp.jpg" alt="" data-src="images/img-21.jpg">
  50. <img src="images/temp.jpg" alt="" data-src="images/img-22.jpg">
  51. <img src="images/temp.jpg" alt="" data-src="images/img-23.jpg">
  52. <img src="images/temp.jpg" alt="" data-src="images/img-24.jpg">
  53. <img src="images/temp.jpg" alt="" data-src="images/img-25.jpg">
  54. <img src="images/temp.jpg" alt="" data-src="images/img-26.jpg">
  55. <img src="images/temp.jpg" alt="" data-src="images/img-27.jpg">
  56. <img src="images/temp.jpg" alt="" data-src="images/img-28.jpg">
  57. <img src="images/temp.jpg" alt="" data-src="images/img-29.jpg">
  58. <img src="images/temp.jpg" alt="" data-src="images/img-30.jpg">
  59. <img src="images/temp.jpg" alt="" data-src="images/img-31.jpg">
  60. <img src="images/temp.jpg" alt="" data-src="images/img-32.jpg">
  61. <img src="images/temp.jpg" alt="" data-src="images/img-33.jpg">
  62. <img src="images/temp.jpg" alt="" data-src="images/img-34.jpg">
  63. <img src="images/temp.jpg" alt="" data-src="images/img-35.jpg">
  64. <img src="images/temp.jpg" alt="" data-src="images/img-36.jpg">
  65. <img src="images/temp.jpg" alt="" data-src="images/img-37.jpg">
  66. <img src="images/temp.jpg" alt="" data-src="images/img-38.jpg">
  67. <img src="images/temp.jpg" alt="" data-src="images/img-39.jpg">
  68. <img src="images/temp.jpg" alt="" data-src="images/img-40.jpg">
  69. <img src="images/temp.jpg" alt="" data-src="images/img-41.jpg">
  70. <img src="images/temp.jpg" alt="" data-src="images/img-42.jpg">
  71. <img src="images/temp.jpg" alt="" data-src="images/img-43.jpg">
  72. <img src="images/temp.jpg" alt="" data-src="images/img-44.jpg">
  73. <img src="images/temp.jpg" alt="" data-src="images/img-45.jpg">
  74. <img src="images/temp.jpg" alt="" data-src="images/img-46.jpg">
  75. <img src="images/temp.jpg" alt="" data-src="images/img-47.jpg">
  76. <img src="images/temp.jpg" alt="" data-src="images/img-48.jpg">
  77. <img src="images/temp.jpg" alt="" data-src="images/img-49.jpg">
  78. <img src="images/temp.jpg" alt="" data-src="images/img-50.jpg">
  79. <img src="images/temp.jpg" alt="" data-src="images/img-51.jpg">
  80. <img src="images/temp.jpg" alt="" data-src="images/img-52.jpg">
  81. <img src="images/temp.jpg" alt="" data-src="images/img-53.jpg">
  82. <img src="images/temp.jpg" alt="" data-src="images/img-54.jpg">
  83. <img src="images/temp.jpg" alt="" data-src="images/img-55.jpg">
  84. <img src="images/temp.jpg" alt="" data-src="images/img-56.jpg">
  85. <img src="images/temp.jpg" alt="" data-src="images/img-57.jpg">
  86. <img src="images/temp.jpg" alt="" data-src="images/img-58.jpg">
  87. <img src="images/temp.jpg" alt="" data-src="images/img-59.jpg">
  88. <img src="images/temp.jpg" alt="" data-src="images/img-60.jpg">
  89. <img src="images/temp.jpg" alt="" data-src="images/img-61.jpg">
  90. <img src="images/temp.jpg" alt="" data-src="images/img-62.jpg">
  91. <img src="images/temp.jpg" alt="" data-src="images/img-63.jpg">
  92. <img src="images/temp.jpg" alt="" data-src="images/img-64.jpg">
  93. <img src="images/temp.jpg" alt="" data-src="images/img-65.jpg">
  94. <img src="images/temp.jpg" alt="" data-src="images/img-66.jpg">
  95. <img src="images/temp.jpg" alt="" data-src="images/img-67.jpg">
  96. <img src="images/temp.jpg" alt="" data-src="images/img-68.jpg">
  97. <img src="images/temp.jpg" alt="" data-src="images/img-69.jpg">
  98. <img src="images/temp.jpg" alt="" data-src="images/img-70.jpg">
  99. </div>
  100. js
  101. // 所有图片
  102. const imgs = document.querySelectorAll('.container > img');
  103. // 图片加载函数
  104. function imgLoad() {
  105. // 视口高度
  106. let viewHeight = document.documentElement.clientHeight;
  107. // 滚动距离
  108. let scrollTop = document.documentElement.scrollTop;
  109. imgs.forEach((img) => {
  110. // 判断是否进入视口
  111. if (img.offsetTop <= viewHeight + scrollTop) {
  112. setTimeout(() => {
  113. img.src = img.dataset.src;
  114. }, 500);
  115. }
  116. });
  117. }
  118. // 加载时
  119. window.addEventListener('load', imgLoad);
  120. // 滚动条
  121. window.addEventListener('scroll', imgLoad);
  122. // 视口变化时
  123. window.addEventListener('resize', imgLoad);
  124. 轮播图
  125. css
  126. /* 初始化 */
  127. * {
  128. margin: 0;
  129. padding: 0;
  130. box-sizing: border-box;
  131. }
  132. a {
  133. text-decoration: none;
  134. }
  135. /* 轮播图的容器 */
  136. .container {
  137. width: 62.5em;
  138. height: 22em;
  139. margin: 1em auto;
  140. /* 转为定位元素/定位父级 */
  141. position: relative;
  142. }
  143. /* 图片组 */
  144. .container > .imgs img {
  145. width: 100%;
  146. height: 100%;
  147. /* 默认全部隐藏 */
  148. display: none;
  149. /* 将所有的图片进行绝对定位,确保每一次只看到一张,所有图片共享这个容器 */
  150. position: absolute;
  151. left: 0;
  152. top: 0;
  153. }
  154. /* 设置默认显示的图片(第一张) */
  155. .container > .imgs img.active {
  156. display: block;
  157. }
  158. /* 按钮组(独立按钮) */
  159. .container > .btns {
  160. position: absolute;
  161. left: 0;
  162. right: 0;
  163. bottom: 0;
  164. /* 水平居中 */
  165. text-align: center;
  166. }
  167. .container > .btns a {
  168. /* 转成行内块元素: 即能水平排列,双支持宽度设置 */
  169. display: inline-block;
  170. padding: 0.5em;
  171. margin: 0 0.2em;
  172. background-color: #fff;
  173. border-radius: 50%;
  174. }
  175. .container > .btns a.active {
  176. background-color: #000;
  177. }
  178. /* 翻页按钮 */
  179. .container .skip a {
  180. position: absolute;
  181. width: 2.5rem;
  182. height: 5rem;
  183. line-height: 5rem;
  184. text-align: center;
  185. opacity: 0.3;
  186. top: 9rem;
  187. font-weight: lighter;
  188. font-size: 2rem;
  189. background-color: #ccc;
  190. }
  191. .container .skip .prev {
  192. left: 0;
  193. }
  194. .container .skip .next {
  195. right: 0;
  196. }
  197. .container .skip *:hover {
  198. opacity: 0.6;
  199. color: #666;
  200. }
  201. html
  202. <div class="container">
  203. <!-- 1. 图片组 -->
  204. <nav class="imgs">
  205. <a href="#"><img src="banner/banner1.jpg" alt="" data-index="1" class="active" /></a>
  206. <a href="#"><img src="banner/banner2.jpg" alt="" data-index="2" /></a>
  207. <a href="#"><img src="banner/banner3.jpg" alt="" data-index="3" /></a>
  208. <a href="#"><img src="banner/banner4.jpg" alt="" data-index="4" /></a>
  209. </nav>
  210. <!-- 2. 图片中下部的小按钮 -->
  211. <nav class="btns">
  212. </nav>
  213. <!-- 3. 翻页 -->
  214. <nav class="skip">
  215. <a href="#" class="prev">&lt;</a>
  216. <a href="#" class="next">&gt;</a>
  217. </nav>
  218. </div>
  219. javascript
  220. // 获得元素
  221. const imgs = document.querySelectorAll('.container >.imgs img');
  222. const btnGroup = document.querySelector('.container > .btns');
  223. const skips = document.querySelector('.container > .skip');
  224. // 创建小按钮的函数
  225. function createBtns(ele,imgLength) {
  226. // 临时父类
  227. let tempBtns = document.createDocumentFragment();
  228. for (let i = 0; i < imgLength; i++) {
  229. let a = document.createElement('a');
  230. a.href = '#';
  231. a.dataset.index = `${i + 1}`;
  232. if ( i === 0) {
  233. a.classList.add('active');
  234. }
  235. tempBtns.append(a);
  236. }
  237. ele.append(tempBtns);
  238. }
  239. // 创建按钮
  240. createBtns(btnGroup,imgs.length);
  241. const btns = document.querySelectorAll('.container > .btns > *');
  242. // 获得activeEle
  243. function getActiveEle(eles) {
  244. let activeEle = [...eles].filter((ele)=>ele.classList.contains('active'));
  245. return activeEle[0];
  246. }
  247. // 设置active元素
  248. function setActiveEle(Index) {
  249. [imgs,btns].forEach((items) =>{
  250. getActiveEle(items).classList.remove('active');
  251. items.forEach((item)=>{
  252. if (item.dataset.index == Index) {
  253. item.classList.add('active');
  254. }
  255. });
  256. });
  257. }
  258. // 小按钮点击事件
  259. btns.forEach((item)=>{item.addEventListener('click',(ev)=>{setActiveEle(ev.target.dataset.index)})});
  260. // 获得当前active元素index
  261. let currentEleIndex = getActiveEle(imgs).dataset.index;
  262. // 上页 下页
  263. skips.querySelector('.next').addEventListener('click', nextEle);
  264. skips.querySelector('.prev').addEventListener('click', prevEle);
  265. // 下页函数
  266. function nextEle(ev) {
  267. if ( ++currentEleIndex > imgs.length) {
  268. currentEleIndex = imgs[0].dataset.index;
  269. }
  270. setActiveEle(currentEleIndex);
  271. }
  272. // 上页函数
  273. function prevEle(ev){
  274. if ( --currentEleIndex < imgs[0].dataset.index) {
  275. currentEleIndex = imgs[imgs.length - 1].dataset.index;
  276. }
  277. setActiveEle(currentEleIndex);
  278. }
  279. // 轮播
  280. const container = document.querySelector('.container');
  281. container.addEventListener('mouseover', stopRun, false);
  282. container.addEventListener('mouseout', startRun, false);
  283. container.addEventListener('run', nextEle);
  284. // 定时器
  285. let timer = [];
  286. const evt = new Event('run');
  287. function startRun(ev) {
  288. timer = setInterval('container.dispatchEvent(evt)',2000);
  289. }
  290. function stopRun(ev) {
  291. clearInterval(timer);
  292. }
Correction status:Uncorrected

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