Blogger Information
Blog 38
fans 0
comment 0
visits 18872
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
鼠标悬停时自动停止播放, 离开时又自动播放 ,给这个轮播图加上翻页按按钮,实现向前和向后翻页播放 ,演示 xhr对象的使用
Blackeye
Original
512 people have browsed it

1
2

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. transition: width 0.5s;
  16. }
  17. .slider .imgs {
  18. /* 图片容器必须要有高度,否则下面图片不能正常显示 */
  19. height: 200px;
  20. }
  21. .slider .imgs img {
  22. /* 图片完全充满父级空间显示 */
  23. height: 100%;
  24. width: 100%;
  25. /* 图片带有圆角 */
  26. border-radius: 10px;
  27. /* 默认图片全部隐藏,只有有active的图片才显示 */
  28. display: none;
  29. }
  30. .slider .imgs img:hover {
  31. cursor: pointer;
  32. }
  33. /* 默认显示第一张 */
  34. .slider .imgs img.active {
  35. display: block;
  36. }
  37. /* 轮播图按钮组 */
  38. .slider .btns {
  39. /* 按钮水平一排显示,用flex,且水平居中 */
  40. display: flex;
  41. place-content: center;
  42. }
  43. .slider .btns span {
  44. /* 按钮宽高相同,确定显示成一个正圆 */
  45. width: 8px;
  46. height: 8px;
  47. /* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */
  48. background-color: rgba(255, 255, 255, 0.4);
  49. /* 50%可确保显示为正圆 */
  50. border-radius: 50%;
  51. /* 按钮上外边距负值,可将它上移,可移动到图片中下方 */
  52. margin: -12px 3px 5px;
  53. }
  54. .slider .btns span.active {
  55. background-color: #fff;
  56. }
  57. .slider .side {
  58. box-sizing: border-box;
  59. display: flex;
  60. place-content: space-between;
  61. font-size: 20px;
  62. color: #fff;
  63. }
  64. .slider .side .leftside {
  65. display: flex;
  66. align-items: center;
  67. place-content: center;
  68. width: 25px;
  69. height: 40px;
  70. border-radius: 8px;
  71. background-color: rgba(255, 255, 255, 0.4);
  72. margin: -120px 1px 0px;
  73. }
  74. .slider .side .leftside:hover {
  75. cursor: pointer;
  76. background-color: red;
  77. }
  78. .slider .side .rightside {
  79. display: flex;
  80. align-items: center;
  81. place-content: center;
  82. width: 25px;
  83. height: 40px;
  84. border-radius: 8px;
  85. background-color: rgba(255, 255, 255, 0.4);
  86. margin: -120px 1px 0px;
  87. }
  88. .slider .side .rightside:hover {
  89. cursor: pointer;
  90. background-color: red;
  91. }
  92. </style>
  93. </head>
  94. <body>
  95. <div class="slider">
  96. <!--
  97. 图片容器
  98. 1. 图片组
  99. 2. 按钮组
  100. 注: 按钮数组与图片数量是一样的
  101. -->
  102. <div class="imgs" onmouseleave="autoPlay()" onmouseover="pause()">
  103. <!-- 轮播图默认从第一张开始显示 -->
  104. <img src="./images/banner1.jpg" alt="" data-index="1" class="active" />
  105. <img src="./images/banner2.jpg" alt="" data-index="2" />
  106. <img src="./images/banner3.png" alt="" data-index="3" />
  107. </div>
  108. <!-- 切换按钮数量与图片数量必须一致 -->
  109. <div class="btns" onmouseleave="autoPlay()" onmouseover="pause()">
  110. <span data-index="1" class="active" onclick="setActive()"></span>
  111. <span data-index="2" onclick="setActive()"></span>
  112. <span data-index="3" onclick="setActive()"></span>
  113. </div>
  114. <!-- 左右切换按钮 -->
  115. <div class="side" onmouseleave="autoPlay()" onmouseover="pause()">
  116. <div class="leftside" onclick="sideclick()">&lt;</div>
  117. <div class="rightside" onclick="sideclick()">&gt;</div>
  118. </div>
  119. </div>
  120. </body>
  121. <script>
  122. const imgList = document.querySelectorAll(".imgs img");
  123. const btnList = document.querySelectorAll(".btns span");
  124. let dataList = [...imgList].map((item) => item.dataset.index);
  125. const tempIndex = dataList.shift();
  126. dataList.push(tempIndex);
  127. let playMain = null;
  128. function cleanActive() {
  129. // 清空active
  130. [...imgList].forEach((item) => item.classList.remove("active"));
  131. [...btnList].forEach((item) => item.classList.remove("active"));
  132. }
  133. function setActive() {
  134. cleanActive();
  135. // 切换active
  136. [...imgList].filter((item) => {
  137. if (item.dataset.index === event.currentTarget.dataset.index) {
  138. item.classList.add("active");
  139. }
  140. });
  141. [...btnList].filter((item) => {
  142. if (item.dataset.index === event.currentTarget.dataset.index) {
  143. item.classList.add("active");
  144. }
  145. });
  146. }
  147. function pause() {
  148. clearInterval(playMain);
  149. }
  150. function autoPlayRender(tempIndex){
  151. cleanActive();
  152. [...imgList].filter((item) => {
  153. if (item.dataset.index === tempIndex) {
  154. item.classList.add("active");
  155. }
  156. });
  157. [...btnList].filter((item) => {
  158. if (item.dataset.index === tempIndex) {
  159. item.classList.add("active");
  160. }
  161. });
  162. }
  163. function autoPlay() {
  164. playMain = setInterval(() => {
  165. const tempIndex = dataList.shift();
  166. // console.log(typeof parseInt(tempIndex));
  167. dataList.push(tempIndex);
  168. autoPlayRender(tempIndex);
  169. }, 3000);
  170. }
  171. function sideclick(){
  172. if(event.currentTarget.className==="leftside"){
  173. const tempIndex = dataList.pop();
  174. dataList.unshift(tempIndex);
  175. autoPlayRender(tempIndex);
  176. }else{
  177. const tempIndex = dataList.shift();
  178. dataList.push(tempIndex);
  179. autoPlayRender(tempIndex);
  180. }
  181. }
  182. window.onload = autoPlay;
  183. </script>
  184. </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>Ajax-XHR</title>
  8. </head>
  9. <body>
  10. <button onclick="getUser(this)">用户信息:xhr</button>
  11. </body>
  12. <script>
  13. function getUser() {
  14. // 1.创建xhr对象
  15. const xhr = new XMLHttpRequest();
  16. const url = "http://xhr411.edu/users.php";
  17. // 2.响应类型
  18. xhr.responseType = "json";
  19. // 3.配置参数
  20. xhr.open("GET", url, true);
  21. // 4.请求成功的回调
  22. xhr.onload = () => {
  23. console.log(xhr.response);
  24. };
  25. // 5.请求失败的回调
  26. xhr.onerror = () => {
  27. console.log("请求错误");
  28. };
  29. // 6.返回的值
  30. xhr.send(null);
  31. }
  32. </script>
  33. </html>
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!