ASCII码 ASCII码

轮播图实战

发布于:2022-04-12 09:58:28  栏目:技术文档

轮播图

  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. .img {
  10. position: absolute;
  11. top: 100px;
  12. left: 50%;
  13. width: 480px;
  14. height: 270px;
  15. margin-left: -240px;
  16. color: #FFFFFF;
  17. /*设置图片默认透明度为0 */
  18. opacity: 0;
  19. /*设置图片过度效果, 会出现淡入淡出效果 */
  20. transition: all .8s;
  21. }
  22. .img:nth-child(1){
  23. background: url("/lesson-19/0411/images/1.jpg");
  24. background-size:100%;
  25. border-radius: 10px;
  26. }
  27. .img:nth-child(2){
  28. background: url("/lesson-19/0411/images/2.jpg");
  29. background-size: 100%;
  30. border-radius: 10px;
  31. }
  32. .img:nth-child(3){
  33. background: url("/lesson-19/0411/images/3.jpg");
  34. background-size: 100%;
  35. border-radius: 10px;
  36. }
  37. .prev {
  38. position: absolute;
  39. top: 185px;
  40. left: 50%;
  41. width: 20px;
  42. height: 30px;
  43. border: 1px solid rgba(255, 255, 255, .2);
  44. border-radius: 0 5px 5px 0;
  45. margin-top: 30px;
  46. margin-left: -240px;
  47. color: #c2c1cb;
  48. text-align: center;
  49. background-color: rgba(255, 255, 255, .2);
  50. cursor: pointer;
  51. }
  52. .next {
  53. position: absolute;
  54. top: 185px;
  55. left: 50%;
  56. width: 20px;
  57. height: 30px;
  58. border: 1px solid rgba(255, 255, 255, .2);
  59. border-radius: 5px 0 0 5px;
  60. margin-top: 30px;
  61. margin-left: 218px;
  62. color: #c2c1cb;
  63. text-align: center;
  64. background-color: rgba(255, 255, 255, .2);
  65. cursor: pointer;
  66. }
  67. .next:hover,
  68. .prev:hover {
  69. background-color: rgba(1, 1, 1, 0.73);
  70. color: #FFFFFF;
  71. }
  72. .div1 {
  73. position: absolute;
  74. top: 350px;
  75. left: 50%;
  76. margin-left: -40px;
  77. }
  78. .little {
  79. float: left;
  80. width: 10px;
  81. height: 10px;
  82. border-radius: 100%;
  83. background-color: rgba(255, 245, 244, .3);
  84. margin-left: 20px;
  85. cursor: pointer;
  86. border: 1px solid #464646;
  87. }
  88. .show{
  89. /*通过添加show,改变图片透明度,从而实现切换效果*/
  90. opacity: 1;
  91. }
  92. .little_show {
  93. background-color: #FFFFFF;
  94. }
  95. </style>
  96. </head>
  97. <body>
  98. <div id="Box">
  99. <div class="img show"></div>
  100. <div class="img"></div>
  101. <div class="img"></div>
  102. <!--上一张按钮-->
  103. <span class="prev"><</span>
  104. <!--下一张按钮-->
  105. <span class="next">></span>
  106. <div class="div1">
  107. <span class="little little_show" data-index="0"></span>
  108. <span class="little" data-index="1"></span>
  109. <span class="little" data-index="2"></span>
  110. </div>
  111. </div>
  112. <script>
  113. let img = document.getElementsByClassName("img"); //获取所有轮播图片 集合
  114. let next = document.querySelector(".next"); //获取下一张 按钮
  115. let prev = document.querySelector(".prev");//获取上一张 按钮
  116. let box = document.getElementById("Box");//获取整个盒子
  117. let little = document.getElementsByClassName("little"); //获取所有轮播圆点按钮 集合
  118. let index = 0;
  119. let time = 0;
  120. // 重置所有class
  121. let clear = function () {
  122. for (let i = 0;i < img.length; i++){
  123. img[i].className = "img";
  124. }
  125. for (let i = 0;i < little.length;i++){
  126. little[i].className = "little";
  127. }
  128. };
  129. let show = function () {
  130. img[index].className = "img show";
  131. little[index].className = "little little_show";
  132. time = 0;
  133. };
  134. // 下一张
  135. let nextBtn = function () {
  136. clear();
  137. index++;
  138. if(index >img.length-1){
  139. index = 0;
  140. }
  141. show();
  142. };
  143. next.addEventListener("click",function () {
  144. nextBtn();
  145. });
  146. // 上一张
  147. let prevBtn = function () {
  148. clear();
  149. index--;
  150. if(index < 0){
  151. index = img.length-1;
  152. }
  153. show();
  154. };
  155. prev.addEventListener("click",function () {
  156. prevBtn();
  157. });
  158. // 按钮
  159. for (let i = 0; i < little.length;i++){
  160. little[i].addEventListener("click",function () {
  161. clear();
  162. index = this.getAttribute("data-index");
  163. show();
  164. })
  165. }
  166. let timer;//全局变量,接收定时器
  167. function autoTime(){
  168. //timer接收定时器
  169. timer = setInterval(function () {
  170. time++;
  171. if(time === 3){
  172. nextBtn();
  173. }
  174. else if(time > 3){
  175. time = 0;
  176. }
  177. // console.log("time = "+time);
  178. },500);
  179. }
  180. // 调用轮播图自动播放方法
  181. autoTime();
  182. // 当鼠标移入imgBox时,停止轮播,清除定时器
  183. box.onmouseover = function () {
  184. clearInterval(timer);
  185. };
  186. // 当鼠标移出imgBox时,重新调用定时器并重置time
  187. box.onmouseout = function () {
  188. time = 0;
  189. autoTime();
  190. };
  191. </script>
  192. </body>
  193. </html>

效果

相关推荐
阅读 +