Blogger Information
Blog 18
fans 0
comment 0
visits 7901
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
鼠标悬停,向前和向后翻页,实例演示 xhr对象
只如初见
Original
575 people have browsed it

鼠标悬停,向前和向后翻页

代码

  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: 810px;
  12. min-width: 320px;
  13. margin: auto;
  14. padding: 0 10px;
  15. position: relative;
  16. z-index: 9;
  17. }
  18. .slider .imgs {
  19. /* 图片容器必须要有高度,否则下面图片不能正常显示 */
  20. height: 400px;
  21. }
  22. .slider .imgs img {
  23. /* 图片完全充满父级空间显示 */
  24. height: 100%;
  25. width: 100%;
  26. /* 图片带有圆角 */
  27. border-radius: 10px;
  28. /* 默认图片全部隐藏,只有有active的图片才显示 */
  29. display: none;
  30. }
  31. .slider .imgs img:hover {
  32. cursor: pointer;
  33. }
  34. /* 默认显示第一张 */
  35. .slider .imgs img.active {
  36. display: block;
  37. }
  38. /* 轮播图按钮组 */
  39. .slider .btns {
  40. /* 按钮水平一排显示,用flex,且水平居中 */
  41. display: flex;
  42. place-content: center;
  43. }
  44. .slider .btns span {
  45. /* 按钮宽高相同,确定显示成一个正圆 */
  46. width: 8px;
  47. height: 8px;
  48. /* 加上红色背景和数字是为了布局时可以看到,一会更去掉 */
  49. background-color: rgba(255, 255, 255, 0.4);
  50. /* 50%可确保显示为正圆 */
  51. border-radius: 50%;
  52. /* 按钮上外边距负值,可将它上移,可移动到图片中下方 */
  53. margin: -12px 3px 5px;
  54. }
  55. .slider .btns span.active {
  56. background-color: #fff;
  57. }
  58. .button {
  59. position: absolute;
  60. z-index: 10;
  61. left: 0;
  62. top: 50%;
  63. margin-top: -30px;
  64. width: 100%;
  65. display: flex;
  66. justify-content: space-between
  67. }
  68. .button img {
  69. width: 60px;
  70. height: 60px;
  71. cursor: pointer;
  72. }
  73. .button .butleft {
  74. margin-left: 8px;
  75. }
  76. .button .butright {
  77. margin-right: 8px;
  78. }
  79. </style>
  80. </head>
  81. <body>
  82. <div class="slider">
  83. <!--
  84. 图片容器
  85. 1. 图片组
  86. 2. 按钮组
  87. 注: 按钮数组与图片数量是一样的
  88. -->
  89. <div class="imgs" onmouseover="stopSlide()" onmouseout="goSlide()">
  90. <!-- 轮播图默认从第一张开始显示 -->
  91. <img src="./images/banner1.jpg" alt="" data-index="1" class="active" />
  92. <img src="./images/banner2.jpg" alt="" data-index="2" />
  93. <img src="./images/banner3.png" alt="" data-index="3" />
  94. </div>
  95. <!-- 切换按钮数量与图片数量必须一致 -->
  96. <div class="btns">
  97. <span data-index="1" class="active" onclick="setActive()"></span>
  98. <span data-index="2" onclick="setActive()"></span>
  99. <span data-index="3" onclick="setActive()"></span>
  100. </div>
  101. <div class="button">
  102. <div class="butleft" onclick="sideclick()"><img src="./images/left.png" /></div>
  103. <div class="butright" onclick="sideclick()"><img src="./images/right.png" /></div>
  104. </div>
  105. </div>
  106. <script>
  107. //获取全部图片和按钮
  108. const imgs = document.querySelectorAll('.slider .imgs img')
  109. const btns = document.querySelectorAll('.slider .btns span')
  110. let dataList = [...imgs].map((item) => item.dataset.index);
  111. const tempIndex = dataList.shift();
  112. dataList.push(tempIndex);
  113. let playMain = null;
  114. //设置激活状态
  115. function setActive() {
  116. // 1. 清空图片和所有按钮的激活状态
  117. imgs.forEach(img => img.classList.remove('active'));
  118. btns.forEach(btn => btn.classList.remove('active'));
  119. // 2. 根据按钮的索引data-index来确定应该将哪一张图片显示出来class=active
  120. event.target.classList.add('active')
  121. imgs.forEach(img => {
  122. if (img.dataset.index === event.target.dataset.index) {
  123. img.classList.add('active')
  124. }
  125. })
  126. }
  127. //自动播放
  128. function autoPlay() {
  129. playMain = setInterval((arr) => {
  130. // 1. 头部删除
  131. let i = arr.shift()
  132. // 2. 尾部追加
  133. arr.push(i);
  134. // 3. 事件派发: 模拟用户点击
  135. btns[i].dispatchEvent(new Event('click'))
  136. }, 2000, Object.keys(btns));
  137. }
  138. //鼠标移上停止
  139. function stopSlide() {
  140. clearInterval(playMain);
  141. }
  142. // 鼠标移开,自动轮播
  143. function goSlide() {
  144. autoPlay();
  145. }
  146. //前后按钮点击
  147. function sideclick() {
  148. stopSlide();
  149. if (event.currentTarget.className === "butleft") {
  150. const tempIndex = dataList.pop();
  151. //console.log(tempIndex);
  152. dataList.unshift(tempIndex);
  153. autoPlayRender(tempIndex);
  154. } else {
  155. const tempIndex = dataList.shift();
  156. //console.log(tempIndex);
  157. dataList.push(tempIndex);
  158. autoPlayRender(tempIndex);
  159. }
  160. }
  161. //传入当前索引值,然后自动轮播
  162. function autoPlayRender(tempIndex) {
  163. cleanActive();
  164. [...imgs].filter((item) => {
  165. if (item.dataset.index === tempIndex) {
  166. item.classList.add("active");
  167. }
  168. });
  169. [...btns].filter((item) => {
  170. if (item.dataset.index === tempIndex) {
  171. item.classList.add("active");
  172. }
  173. });
  174. }
  175. //清除之前选中样式
  176. function cleanActive() {
  177. // 清空active
  178. [...imgs].forEach((item) => item.classList.remove("active"));
  179. [...btns].forEach((item) => item.classList.remove("active"));
  180. }
  181. window.onload = autoPlay();
  182. // 作业1: 实现鼠标悬停时自动停止播放, 离开时又自动播放
  183. // 作业2[可选]: 给这个轮播图加上翻页按按钮,实现向前和向后翻页播放
  184. </script>
  185. </body>
  186. </html>

效果

实例演示 xhr对象

  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>Ajax-XHR</title>
  8. </head>
  9. <body>
  10. <button onclick="getUser(this)">用户信息: xhr</button>
  11. <script>
  12. function getUser(btn) {
  13. // 1. 创建xhr对象
  14. const xhr = new XMLHttpRequest;
  15. // 2. 响应类型
  16. xhr.responseType = 'json'
  17. // 3. 配置参数
  18. let url = 'http://127.0.0.21/users.php?id=3'
  19. xhr.open('GET', url);
  20. // 4. 请求成功的回调
  21. xhr.onload = () => {
  22. // 返回的值
  23. console.log(xhr.response);
  24. }
  25. // 5. 请求失败的回调
  26. xhr.onerror = () => {
  27. // 返回的值
  28. console.log('请求错误');
  29. }
  30. // 6. 发起xhr请求
  31. xhr.send(null)
  32. }
  33. </script>
  34. </body>
  35. </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
1 comments
弦知音 2022-06-02 21:05:39
请问这个效果动图是用什么工具制作的
1 floor
Author's latest blog post