JS实现轮播图效果

深藏阁楼爱情的钟 2022-03-17 14:14 502阅读 0赞

一、用JS实现轮播图,简单分为五大步,具体看代码块注释
1.自动播放
2.鼠标进入停止播放
3.鼠标离开继续播放
4.点豆豆跳转到对应图片
5.给图片添加超链接
二、示例图
在这里插入图片描述

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>简单的轮播图</title>
  6. <style type="text/css">
  7. body{
  8. margin:0;
  9. padding:0;
  10. }
  11. #box{
  12. width:500px;
  13. height:300px;
  14. position:relative;
  15. top:150px;
  16. left:400px;
  17. /*border:1px solid red;*/
  18. }
  19. #ulId{
  20. list-style:none;
  21. position:absolute;
  22. right:15px;
  23. bottom:10px;
  24. z-index:3;
  25. }
  26. #ulId li{
  27. width:10px;
  28. height:10px;
  29. border-radius:50%;
  30. background:greenyellow;
  31. margin:0 5px;
  32. float:left;
  33. z-index:3;
  34. }
  35. #ulId li:nth-child(1){
  36. background:green;
  37. }
  38. #box img{
  39. width:100%;
  40. height:100%;
  41. position:absolute;
  42. left:0;
  43. top:0;
  44. z-index:1;
  45. }
  46. #box img:nth-child(1){
  47. z-index:2;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="box">
  53. <img src="img/1.jpg"/>
  54. <img src="img/2.jpg"/>
  55. <img src="img/3.png"/>
  56. <img src="img/4.jpg"/>
  57. <img src="img/5.jpg"/>
  58. <img src="img/6.jpg"/>
  59. <img src="img/7.jpg"/>
  60. <img src="img/8.jpg"/>
  61. <img src="img/9.jpg"/>
  62. <ul id="ulId">
  63. <li></li>
  64. <li></li>
  65. <li></li>
  66. <li></li>
  67. <li></li>
  68. <li></li>
  69. <li></li>
  70. <li></li>
  71. <li></li>
  72. </ul>
  73. </div>
  74. </body>
  75. </html>
  76. <script type="text/javascript">
  77. function $(str){
  78. if(str.startsWith("#")){
  79. return document.getElementById(str.substring(1));
  80. }else if(str.startsWith(".")){
  81. return document.getElementsByClassName(str.substring(1));
  82. }else{
  83. return document.getElementsByTagName(str);
  84. }
  85. }
  86. //1.自动播放
  87. let currIndex=0;
  88. let myTime;
  89. function autoPlay(){
  90. //给所有li添加自定义属性
  91. let liDoms=$("li");
  92. for(let i=0;i<liDoms.length;i++){
  93. liDoms[i].setAttribute("index",i);
  94. }
  95. //一、数据处理
  96. myTime=setInterval(()=>{
  97. //1.改变数据
  98. currIndex++;
  99. //2.边界处理
  100. if(currIndex>=9){
  101. currIndex=0;
  102. }
  103. //二、改变样式
  104. //1.改图片
  105. //让所有img的zIndex为1
  106. for(let i=0;i<$("img").length;i++){
  107. $("img")[i].style.zIndex=1;
  108. }
  109. //让当前img的zIndex为2
  110. $("img")[currIndex].style.zIndex=2;
  111. //2.改豆豆
  112. //让所有li的background为greenyellow
  113. let liDoms=$("#ulId").children;
  114. for(let i=0;i<liDoms.length;i++){
  115. liDoms[i].style.background="greenyellow";
  116. }
  117. //让当前li的background为green
  118. liDoms[currIndex].style.background="green";
  119. },1000);
  120. }
  121. //2.停止播放
  122. function stopPlay(){
  123. window.clearInterval(myTime);
  124. }
  125. //3.继续播放
  126. //4.点豆豆改变图片
  127. function goImg(index){
  128. currIndex=index;
  129. if(currIndex<0 || currIndex>9){
  130. currIndex=0;
  131. }
  132. //二、改变样式
  133. //1.改图片
  134. //让所有img的zIndex为1
  135. for(let i=0;i<$("img").length;i++){
  136. $("img")[i].style.zIndex=1;
  137. }
  138. //让当前img的zIndex为2
  139. $("img")[currIndex].style.zIndex=2;
  140. //2.改豆豆
  141. //让所有li的background为greenyellow
  142. let liDoms=$("#ulId").children;
  143. for(let i=0;i<liDoms.length;i++){
  144. liDoms[i].style.background="greenyellow";
  145. }
  146. //让当前li的background为green
  147. liDoms[currIndex].style.background="green";
  148. }
  149. //5.超链接
  150. window.function(){
  151. //1.自动播放
  152. autoPlay();
  153. //2.停止播放
  154. $("#box").onmouseover=function(){
  155. stopPlay();
  156. }
  157. //3.继续播放
  158. $("#box").onmouseout=function(){
  159. autoPlay();
  160. }
  161. //4.点豆豆改变图片
  162. let liDoms=$("#ulId").children;
  163. for(let i=0;i<liDoms.length;i++){
  164. liDoms[i].onclick=function(){
  165. goImg(this.getAttribute("index"));
  166. }
  167. }
  168. }
  169. </script>

发表评论

表情:
评论列表 (有 0 条评论,502人围观)

还没有评论,来说两句吧...

相关阅读