JQ实现简单的自动轮播图效果

傷城~ 2023-03-02 07:38 289阅读 0赞

在这里插入图片描述

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="author" content="Jxz">
  6. <title></title>
  7. <script src="../jquery-3.1.1.js"></script>
  8. <style>
  9. ul,li{
  10. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. }
  14. #box{
  15. width: 520px;
  16. height: 280px;
  17. margin: 100px auto;
  18. position: relative;
  19. }
  20. #box .list li{
  21. position: absolute;
  22. top: 0;
  23. left: 0;
  24. display: none;
  25. }
  26. #box .list li.current{
  27. display: block;
  28. }
  29. #box .count{
  30. position: absolute;
  31. left: 10px;
  32. bottom: 10px;
  33. }
  34. #box .count li{
  35. float: left;
  36. width: 20px;
  37. height: 20px;
  38. border-radius: 50%;
  39. background-color: #fa0;
  40. text-align: center;
  41. line-height: 20px;
  42. margin-left: 10px;
  43. color: #fff;
  44. opacity: 0.8;
  45. cursor: pointer;
  46. }
  47. #box .count li.current{
  48. background-color: #f60;
  49. opacity: 1;
  50. }
  51. #box .btn{
  52. position: absolute;
  53. bottom:10px;
  54. right: 15px;
  55. }
  56. #box .btn li{
  57. width: 50px;
  58. height: 50px;
  59. background-color: #ccc;
  60. float: right;
  61. margin-left: 15px;
  62. opacity: 0.8;
  63. cursor: pointer;
  64. text-align: center;
  65. line-height: 50px;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <div id="box">
  71. <ul class="list">
  72. <li class="current"><img src="img/01.jpg" alt=""></li>
  73. <li><img src="img/02.jpg" alt=""></li>
  74. <li><img src="img/03.jpg" alt=""></li>
  75. <li><img src="img/04.jpg" alt=""></li>
  76. <li><img src="img/05.jpg" alt=""></li>
  77. </ul>
  78. <ul class="count">
  79. <li class="current">1</li>
  80. <li>2</li>
  81. <li>3</li>
  82. <li>4</li>
  83. <li>5</li>
  84. </ul>
  85. <ul class="btn">
  86. <li class="right">></li>
  87. <li class="left"><</li>
  88. </ul>
  89. </div>
  90. </body>
  91. </html>
  92. <script>
  93. // var aLi=$('.list li');
  94. // var aNum=$('.count li');
  95. // 记录当前显示的图片的索引
  96. var current=0;
  97. // 保存定时器
  98. var timer=null;
  99. timer=setInterval(autoPlay,1000)
  100. // 自动播放
  101. function autoPlay(){
  102. current<$('.count li').length-1?current++:current=0;
  103. show()
  104. }
  105. function show(){
  106. // aLi.hide()
  107. $('.list li').hide()
  108. .eq(current).show();
  109. // aNum.removeClass()
  110. // .eq(current).addClass('current')
  111. $('.count li').eq(current).toggleClass('current').siblings().removeClass('current');
  112. }
  113. // 手动
  114. $('.count li').mouseenter(function(){
  115. current=$(this).index()
  116. show()
  117. })
  118. // 按钮控制选图
  119. $('#box .left').click(function(){
  120. current>0?current--:current=4;
  121. show()
  122. })
  123. $('#box .right').click(function(){
  124. current<$('.count li').length-1?current++:current=0;
  125. show()
  126. })
  127. // 鼠标进图自动暂停
  128. $('#box').hover(function(){
  129. clearInterval(timer);
  130. },function(){
  131. timer = setInterval(autoPlay,1000);
  132. })
  133. </script>

发表评论

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

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

相关阅读