博客列表 >JS经典案例:懒加载和轮播图

JS经典案例:懒加载和轮播图

李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰原创
2020年05月28日 22:58:231170浏览

案例一:懒加载

1.代码

  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. .images {
  9. width: 1200px;
  10. margin: 10px auto;
  11. display: grid;
  12. grid-template-columns: repeat(2, 1fr);
  13. }
  14. .images > img {
  15. margin: 10px auto;
  16. width: 500px;
  17. height: 400px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="images">
  23. <img src="images/temp.jpg" alt="" data-index="images/img-1.jpg" />
  24. <img src="images/temp.jpg" alt="" data-index="images/img-2.jpg" />
  25. <img src="images/temp.jpg" alt="" data-index="images/img-3.jpg" />
  26. <img src="images/temp.jpg" alt="" data-index="images/img-4.jpg" />
  27. <img src="images/temp.jpg" alt="" data-index="images/img-5.jpg" />
  28. <img src="images/temp.jpg" alt="" data-index="images/img-6.jpg" />
  29. <img src="images/temp.jpg" alt="" data-index="images/img-7.jpg" />
  30. <img src="images/temp.jpg" alt="" data-index="images/img-8.jpg" />
  31. <img src="images/temp.jpg" alt="" data-index="images/img-9.jpg" />
  32. <img src="images/temp.jpg" alt="" data-index="images/img-10.jpg" />
  33. <img src="images/temp.jpg" alt="" data-index="images/img-11.jpg" />
  34. <img src="images/temp.jpg" alt="" data-index="images/img-12.jpg" />
  35. <img src="images/temp.jpg" alt="" data-index="images/img-13.jpg" />
  36. <img src="images/temp.jpg" alt="" data-index="images/img-14.jpg" />
  37. <img src="images/temp.jpg" alt="" data-index="images/img-15.jpg" />
  38. <img src="images/temp.jpg" alt="" data-index="images/img-16.jpg" />
  39. <img src="images/temp.jpg" alt="" data-index="images/img-17.jpg" />
  40. <img src="images/temp.jpg" alt="" data-index="images/img-18.jpg" />
  41. <img src="images/temp.jpg" alt="" data-index="images/img-19.jpg" />
  42. <img src="images/temp.jpg" alt="" data-index="images/img-20.jpg" />
  43. <img src="images/temp.jpg" alt="" data-index="images/img-21.jpg" />
  44. <img src="images/temp.jpg" alt="" data-index="images/img-22.jpg" />
  45. <img src="images/temp.jpg" alt="" data-index="images/img-23.jpg" />
  46. <img src="images/temp.jpg" alt="" data-index="images/img-24.jpg" />
  47. <img src="images/temp.jpg" alt="" data-index="images/img-25.jpg" />
  48. <img src="images/temp.jpg" alt="" data-index="images/img-26.jpg" />
  49. <img src="images/temp.jpg" alt="" data-index="images/img-27.jpg" />
  50. <img src="images/temp.jpg" alt="" data-index="images/img-28.jpg" />
  51. <img src="images/temp.jpg" alt="" data-index="images/img-29.jpg" />
  52. <img src="images/temp.jpg" alt="" data-index="images/img-30.jpg" />
  53. <img src="images/temp.jpg" alt="" data-index="images/img-31.jpg" />
  54. <img src="images/temp.jpg" alt="" data-index="images/img-32.jpg" />
  55. <img src="images/temp.jpg" alt="" data-index="images/img-33.jpg" />
  56. <img src="images/temp.jpg" alt="" data-index="images/img-34.jpg" />
  57. <img src="images/temp.jpg" alt="" data-index="images/img-35.jpg" />
  58. <img src="images/temp.jpg" alt="" data-index="images/img-36.jpg" />
  59. <img src="images/temp.jpg" alt="" data-index="images/img-37.jpg" />
  60. <img src="images/temp.jpg" alt="" data-index="images/img-38.jpg" />
  61. <img src="images/temp.jpg" alt="" data-index="images/img-39.jpg" />
  62. <img src="images/temp.jpg" alt="" data-index="images/img-40.jpg" />
  63. <img src="images/temp.jpg" alt="" data-index="images/img-41.jpg" />
  64. <img src="images/temp.jpg" alt="" data-index="images/img-42.jpg" />
  65. <img src="images/temp.jpg" alt="" data-index="images/img-43.jpg" />
  66. <img src="images/temp.jpg" alt="" data-index="images/img-44.jpg" />
  67. <img src="images/temp.jpg" alt="" data-index="images/img-45.jpg" />
  68. <img src="images/temp.jpg" alt="" data-index="images/img-46.jpg" />
  69. <img src="images/temp.jpg" alt="" data-index="images/img-47.jpg" />
  70. <img src="images/temp.jpg" alt="" data-index="images/img-48.jpg" />
  71. <img src="images/temp.jpg" alt="" data-index="images/img-49.jpg" />
  72. <img src="images/temp.jpg" alt="" data-index="images/img-50.jpg" />
  73. <img src="images/temp.jpg" alt="" data-index="images/img-51.jpg" />
  74. <img src="images/temp.jpg" alt="" data-index="images/img-52.jpg" />
  75. <img src="images/temp.jpg" alt="" data-index="images/img-53.jpg" />
  76. <img src="images/temp.jpg" alt="" data-index="images/img-54.jpg" />
  77. <img src="images/temp.jpg" alt="" data-index="images/img-55.jpg" />
  78. <img src="images/temp.jpg" alt="" data-index="images/img-56.jpg" />
  79. <img src="images/temp.jpg" alt="" data-index="images/img-57.jpg" />
  80. <img src="images/temp.jpg" alt="" data-index="images/img-58.jpg" />
  81. <img src="images/temp.jpg" alt="" data-index="images/img-59.jpg" />
  82. <img src="images/temp.jpg" alt="" data-index="images/img-60.jpg" />
  83. <img src="images/temp.jpg" alt="" data-index="images/img-61.jpg" />
  84. <img src="images/temp.jpg" alt="" data-index="images/img-62.jpg" />
  85. <img src="images/temp.jpg" alt="" data-index="images/img-63.jpg" />
  86. <img src="images/temp.jpg" alt="" data-index="images/img-64.jpg" />
  87. <img src="images/temp.jpg" alt="" data-index="images/img-65.jpg" />
  88. <img src="images/temp.jpg" alt="" data-index="images/img-66.jpg" />
  89. <img src="images/temp.jpg" alt="" data-index="images/img-67.jpg" />
  90. <img src="images/temp.jpg" alt="" data-index="images/img-68.jpg" />
  91. <img src="images/temp.jpg" alt="" data-index="images/img-69.jpg" />
  92. <img src="images/temp.jpg" alt="" data-index="images/img-70.jpg" />
  93. </div>
  94. </body>
  95. <script>
  96. var cl = console.log.bind(console);
  97. var imgs = document.querySelectorAll("img");
  98. var viewheight = document.documentElement.clientHeight; //文档可视区域大小
  99. cl(window);
  100. //监听浏览器窗口滚动
  101. window.addEventListener(
  102. "scroll",
  103. function () {
  104. var scroll = document.documentElement.scrollTop; //文档滚动大小
  105. imgs.forEach(function (item) {
  106. if (item.offsetTop <= viewheight + scroll)
  107. item.src = item.dataset.index;
  108. });
  109. },
  110. false
  111. );
  112. </script>
  113. </html>

2.运行结果图:

案例二:轮播图

1.代码:

  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. body {
  9. /*background-color: #808080;*/
  10. }
  11. .slider {
  12. width: 800px;
  13. height: 280px;
  14. margin: 10px auto;
  15. position: relative;
  16. }
  17. .slider > .images > .image {
  18. width: 800px;
  19. height: 280px;
  20. display: none;
  21. }
  22. .slider > .images > .active {
  23. display: block;
  24. }
  25. .slider > .skip {
  26. display: inline-block;
  27. width: 40px;
  28. height: 60px;
  29. background-color: RGB(255, 255, 255, 0.2);
  30. text-align: center;
  31. line-height: 60px;
  32. font-size: 30px;
  33. color: RGB(0, 0, 0, 0.2);
  34. position: absolute;
  35. top: 100px;
  36. }
  37. .slider > .skip:hover {
  38. cursor: pointer;
  39. background-color: RGB(255, 255, 255, 0.6);
  40. color: RGB(0, 0, 0, 0.8);
  41. }
  42. .slider > .prev {
  43. position: absolute;
  44. left: 0;
  45. }
  46. .slider > .next {
  47. position: absolute;
  48. right: 0;
  49. }
  50. .slider > .points {
  51. width: 550px;
  52. /* margin: 0 auto; */
  53. text-align: center;
  54. position: absolute;
  55. bottom: 10px;
  56. left: 150px;
  57. }
  58. .slider > .points > .point {
  59. display: inline-block;
  60. width: 12px;
  61. height: 12px;
  62. margin: 0 5px;
  63. background-color: white;
  64. border-radius: 100%;
  65. }
  66. .slider > .points > .point:hover {
  67. cursor: pointer;
  68. }
  69. .slider > .points > .active {
  70. background-color: black;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <div class="slider">
  76. <div class="images">
  77. <img
  78. class="image active"
  79. src="banner/banner1.jpg"
  80. alt=""
  81. data-index="1"
  82. />
  83. <img class="image" src="banner/banner2.jpg" alt="" data-index="2" />
  84. <img class="image" src="banner/banner3.jpg" alt="" data-index="3" />
  85. <img class="image" src="banner/banner4.jpg" alt="" data-index="4" />
  86. </div>
  87. <span class="skip prev">&lt;</span>
  88. <span class="skip next">&gt;</span>
  89. <div class="points">
  90. <span class="point active" data-index="1"></span>
  91. <span class="point" data-index="2"></span>
  92. <span class="point" data-index="3"></span>
  93. <span class="point" data-index="4"></span>
  94. </div>
  95. </div>
  96. </body>
  97. <script>
  98. var cl = console.log.bind(console);
  99. var imgs = document.querySelectorAll("img");
  100. var prev = document.querySelector(".prev");
  101. // cl(prev);
  102. var next = document.querySelector(".next");
  103. var pointparent = document.querySelector(".points");
  104. var points = document.querySelectorAll(".point");
  105. //小圆点切换
  106. pointparent.addEventListener(
  107. "click",
  108. function (ev) {
  109. // cl(ev.target.nodeType);
  110. if (ev.target.nodeType === 1 && ev.target != pointparent) {
  111. arr = Array.from(pointparent.children);
  112. arr.forEach(function (item) {
  113. item.classList.remove("active");
  114. });
  115. ev.target.classList.add("active");
  116. imgs.forEach(function (item) {
  117. item.classList.remove("active");
  118. if (item.dataset.index === ev.target.dataset.index) {
  119. item.classList.add("active");
  120. }
  121. });
  122. }
  123. },
  124. false
  125. );
  126. //向前翻页
  127. prev.addEventListener(
  128. "click",
  129. function () {
  130. var active = document.querySelector(".slider>.images>.active");
  131. imgs.forEach(function (item) {
  132. item.classList.remove("active");
  133. });
  134. if (active.previousElementSibling) {
  135. pre = active.previousElementSibling;
  136. } else {
  137. pre = imgs[imgs.length - 1];
  138. }
  139. pre.classList.add("active");
  140. points.forEach(function (item) {
  141. item.classList.remove("active");
  142. if (item.dataset.index === pre.dataset.index) {
  143. item.classList.add("active");
  144. }
  145. });
  146. },
  147. false
  148. );
  149. // 向后翻页
  150. next.addEventListener(
  151. "click",
  152. function () {
  153. var active = document.querySelector(".slider>.images>.active");
  154. imgs.forEach(function (item) {
  155. item.classList.remove("active");
  156. });
  157. if (active.nextElementSibling) {
  158. nex = active.nextElementSibling;
  159. } else {
  160. nex = imgs[0];
  161. }
  162. nex.classList.add("active");
  163. points.forEach(function (item) {
  164. item.classList.remove("active");
  165. // cl(item.dataset.index);
  166. if (item.dataset.index === nex.dataset.index) {
  167. item.classList.add("active");
  168. }
  169. });
  170. },
  171. false
  172. );
  173. // //自动播放
  174. var slider = document.querySelector(".slider");
  175. // cl(slider);
  176. //移出自动播放
  177. slider.addEventListener("mouseout", timer, false);
  178. //定时器
  179. var timer = setInterval(function () {
  180. var click = new Event("click");
  181. next.dispatchEvent(click);
  182. }, 2000);
  183. //移入清除定时器
  184. slider.addEventListener(
  185. "mouseover",
  186. function () {
  187. clearInterval(timer);
  188. },
  189. false
  190. );
  191. </script>
  192. </html>

2.代码运行效果:

总结:

1、经典案例主要操作是DOM操作,创建元素、获取元素、修改元素属性、删除元素
2、parentElement:父级元素;ul.insertBefore(li1,li2);把li1插入ul中li2之前;
3、事件委托时:触发事件,要区分非元素节点和父级元素以及子的元素
4、document.documentElement.clientHeight:获取文档可视区域大小;window.addEventListener();窗口监听函数;document.documentElement.scrollTop; 文档滚动大小;
5、new Event()创建模拟事件;setInterval(callback,time)定时执行函数;

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