Blogger Information
Blog 43
fans 0
comment 3
visits 27032
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
轮播图:1: 实现鼠标悬停时自动停止播放, 离开时又自动播放 2 加上翻页按按钮,实现向前和向后翻页播放 。 实例演示 xhr对象的使用
Time
Original
1216 people have browsed it

轮播图

图片:

lunbo

代码

  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. /* ! 3. 轮播图 */
  10. .slider {
  11. max-width: 750px;
  12. min-width: 320px;
  13. margin: auto;
  14. padding: 0 10px;
  15. }
  16. .slider .imgs {
  17. /* 图片容器必须要有高度,否则下面图片不能正常显示 */
  18. height: 150px;
  19. }
  20. .slider .imgs img {
  21. /* 图片完全充满父级空间显示 */
  22. height: 100%;
  23. width: 100%;
  24. /* 图片带有圆角 */
  25. border-radius: 10px;
  26. /* 默认图片全部隐藏,只有有active的图片才显示 */
  27. display: none;
  28. }
  29. .slider .imgs img:hover {
  30. cursor: pointer;
  31. }
  32. /* 默认显示第一张 */
  33. .slider .imgs img.active {
  34. display: block;
  35. }
  36. /* 轮播图按钮组 */
  37. .slider .btns {
  38. /* 按钮水平一排显示,用flex,且水平居中 */
  39. display: flex;
  40. place-content: center;
  41. }
  42. .slider .btns span {
  43. /* 按钮宽高相同,确定显示成一个正圆 */
  44. width: 8px;
  45. height: 8px;
  46. /* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */
  47. background-color: rgba(255, 255, 255, 0.4);
  48. /* 50%可确保显示为正圆 */
  49. border-radius: 50%;
  50. /* 按钮上外边距负值,可将它上移,可移动到图片中下方 */
  51. margin: -12px 3px 5px;
  52. }
  53. .slider .btns span.active {
  54. background-color: #fff;
  55. }
  56. .slider .page {
  57. display: flex;
  58. place-content: space-between;
  59. margin-top: -88px;
  60. opacity: 0.3;
  61. }
  62. .slider .page button {
  63. border: none;
  64. }
  65. </style>
  66. <link
  67. rel="stylesheet"
  68. href="//at.alicdn.com/t/font_3280335_j9wms97sque.css"
  69. />
  70. </head>
  71. <body>
  72. <div class="slider">
  73. <div class="imgs">
  74. <img src="images/1.jpg" alt="" data-index="1" class="active" />
  75. <img src="images/2.jpg" alt="" data-index="2" />
  76. <img src="images/3.jpg" alt="" data-index="3" />
  77. <img src="images/4.jpg" alt="" data-index="4" />
  78. <img src="images/5.jpg" alt="" data-index="5" />
  79. </div>
  80. <div class="btns">
  81. <span data-index="1" class="active" onclick="setActive()"></span>
  82. <span data-index="2" onclick="setActive()"></span>
  83. <span data-index="3" onclick="setActive()"></span>
  84. <span data-index="4" onclick="setActive()"></span>
  85. <span data-index="5" onclick="setActive()"></span>
  86. </div>
  87. <div class="page">
  88. <button class="prev" onclick="setPage(1)">
  89. <i class="iconfont icon-shangyige"></i>
  90. </button>
  91. <button class="next" onclick="setPage(2)">
  92. <i class="iconfont icon-xiayige"></i>
  93. </button>
  94. </div>
  95. </div>
  96. <script>
  97. const imgs = document.querySelectorAll(".slider .imgs img");
  98. const btns = document.querySelectorAll(".slider .btns span");
  99. let iNow = 0;
  100. //清空class
  101. function claerClass() {
  102. imgs.forEach((img) => img.classList.remove("active"));
  103. btns.forEach((btn) => btn.classList.remove("active"));
  104. }
  105. //添加样式:翻页、自动播放、点击btn按钮
  106. function setActive(iNow) {
  107. //调用清空class自定义函数
  108. claerClass();
  109. //if判断iNow !== undefined :翻页、自动播放;else:点击btn按钮
  110. if (iNow !== undefined) {
  111. [...imgs].filter((img, index) => {
  112. if (iNow === index) {
  113. img.classList.add("active");
  114. }
  115. });
  116. [...btns].filter((btn, index) => {
  117. if (iNow === index) {
  118. btn.classList.add("active");
  119. }
  120. });
  121. } else {
  122. event.target.classList.add("active");
  123. imgs.forEach((img) => {
  124. if (img.dataset.index === event.target.dataset.index) {
  125. img.classList.add("active");
  126. }
  127. });
  128. }
  129. }
  130. //自动播放函数
  131. function setInteCanshu() {
  132. iNow++;
  133. if (iNow == btns.length) iNow = 0;
  134. setActive(iNow);
  135. }
  136. //设置翻页
  137. function setPage(ele) {
  138. // ele :1 上一页 2 下一页
  139. if (ele === 2) {
  140. iNow++;
  141. if (iNow == imgs.length) iNow = 0;
  142. setActive(iNow);
  143. } else {
  144. iNow--;
  145. if (iNow == -1) iNow = imgs.length - 1;
  146. setActive(iNow);
  147. }
  148. }
  149. //间隔2秒执行一次自动函数
  150. let setInte = setInterval(setInteCanshu, 2000);
  151. //获取整个div
  152. let slider = document.querySelector(".slider");
  153. //鼠标悬停时自动停止播放
  154. slider.onmouseover = () => clearInterval(setInte);
  155. //离开时又自动播放
  156. slider.onmouseout = () => (setInte = setInterval(setInteCanshu, 2000));
  157. </script>
  158. </body>
  159. </html>

实例演示ajax:xhr

代码:

  1. <script>
  2. let url = "";
  3. //ajax:xhr
  4. function getUser() {
  5. //创建对象
  6. const xhr = new XMLHttpRequest();
  7. //请求类型
  8. xhr.response("json");
  9. //配置参数
  10. xhr.open("GET", url);
  11. //请求成功
  12. xhr.onload = () => {
  13. console.log(xhr.response);
  14. };
  15. //请求失败
  16. xhr.onerror = () => {
  17. console.log("请求失败");
  18. };
  19. //发起请求
  20. xhr.send(null);
  21. }
  22. </script>
Correcting teacher:PHPzPHPz

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