当前位置:   article > 正文

jQuery实现轮播图_jquery.superslide.2.1.1

jquery.superslide.2.1.1

css样式

  1. <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>//导入jQuery文件
  2. <style>
  3. *{margin: 0;padding: 0}
  4. li{list-style: none}
  5. .bnner{
  6. width: 820px;
  7. position: relative;
  8. margin: 0 auto;
  9. }
  10. .nav{
  11. width: 820px;
  12. height: 340px;
  13. border: 1px solid;
  14. overflow: hidden;
  15. }
  16. .nav_img{
  17. float: left;
  18. margin-left: 0px;
  19. width: 4120px;
  20. font-size: 0;
  21. }
  22. .nav_xiao{
  23. position: absolute;
  24. bottom: 0;
  25. left: 50%;
  26. transform: translateX(-50%);
  27. }
  28. .nav_xiao>ul{
  29. display: flex;
  30. }
  31. .nav_xiao>ul>li{
  32. width: 30px;
  33. height: 30px;
  34. border-radius: 50%;
  35. background: #0da0b4;
  36. margin-left: 5px;
  37. }
  38. .nav_xiao>ul .mos{
  39. background: red;
  40. }
  41. .nav_left>div:nth-child(1){
  42. position: absolute;
  43. left: 0;
  44. top: 50%;
  45. font-size: 35px;
  46. border: 1px solid red;
  47. }
  48. .nav_left>div:nth-child(2){
  49. position: absolute;
  50. right: 0;
  51. top: 50%;
  52. font-size: 35px;
  53. border: 1px solid red;
  54. }
  55. </style>

html结构

  1. <div class="bnner">
  2. <div class="nav">
  3. <div class="nav_img">
  4. <img src="imgs/1.jpeg" alt="">
  5. <img src="imgs/2.jpeg" alt="">
  6. <img src="imgs/3.jpeg" alt="">
  7. <img src="imgs/2.jpeg" alt="">
  8. <img src="imgs/3.jpeg" alt="">
  9. </div>
  10. </div>
  11. <div class="nav_xiao">
  12. <ul>
  13. <li class="mos"></li>
  14. <li></li>
  15. <li></li>
  16. <li></li>
  17. <li></li>
  18. </ul>
  19. </div>
  20. <div class="nav_left">
  21. <div></div>
  22. <div></div>
  23. </div>
  24. </div>

jQuery代码

  1. <script>
  2. //图片
  3. let ss=-820;//图片大小
  4. let sum=0; //自定义的索引下标
  5. function move(){ //开启定时器
  6. sum++; //自定义的索引下标自增
  7. if (sum>=5){ //图片就五张不能让他大于5
  8. sum=0 //否则归零
  9. }
  10. $('.nav_xiao li').css('background','#0da0b4') //清空小圆点的颜色
  11. $('.nav_xiao li').eq(sum).css('background','red')//让当前跟图片对应的小圆点显示
  12. $('.nav_img').animate({ //animate动画用于过度
  13. 'margin-left':ss*sum //让当前下标乘图片宽 等于现在的位置
  14. },500) //500毫秒的延迟
  15. }
  16. //右
  17. $('.nav_left div:nth-child(2)').on('click',function(){ //右击按钮
  18. sum++ //拿到当前自定义的索引下标 每点一下就让他加一
  19. if (sum>=5){ //如果大于等于五就让他归零
  20. sum=0
  21. }
  22. $('.nav_img').animate({ //让宽度乘以现在的下标索引
  23. 'margin-left':ss*sum
  24. },500)
  25. $('.nav_xiao li').css('background','#0da0b4') //小圆点的样式,初始化
  26. $('.nav_xiao li').eq(sum).css('background','red')
  27. })
  28. $('.bnner').on('mouseenter',function(){ //移入停止定时器
  29. clearInterval(time)
  30. }).on('mouseleave',function(){ //移出开启定时器
  31. time = setInterval(move,indexs)
  32. })
  33. //左
  34. $('.nav_left div:nth-child(1)').on('click',function(){
  35. sum-- //拿到当前自定义的索引下标 每点一下就让他减一
  36. if (sum<0){ //同上判断
  37. sum=4 //满足条件重新赋值
  38. }
  39. $('.nav_img').animate({
  40. 'margin-left':ss*sum //让宽度乘以现在的下标索引
  41. },500)
  42. $('.nav_xiao li').css('background','#0da0b4')
  43. $('.nav_xiao li').eq(sum).css('background','red')
  44. })
  45. //小圆点
  46. $('.nav_xiao li').each(function(index,ele){ //用each循环当前小圆点
  47. $(this).on('mouseenter',function(){ //每次移入当前移入的小圆点时触发的事件,这里的事件可以换成click
  48. $('.nav_xiao li').css('background','#0da0b4') //初始化样式
  49. $(this).css('background','red') //让当前鼠标移入的小圆点样式变更
  50. $('.nav_img').css('margin-left',ss*index) //让当前移入的小圆点的索引乘以宽度
  51. sum=index //把当前触发索引给sum自定义下标索引
  52. })
  53. })
  54. let time=setInterval(move,2000)
  55. </script>

 

下面这个是后来加的 无缝滚动轮播图 在最后面 加一张 第一张图片

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  7. </head>
  8. <style>
  9. *{margin: 0;padding: 0}
  10. li{list-style: none}
  11. .bnner{
  12. width: 820px; //图片多大这里就设多大
  13. position: relative;
  14. margin: 0 auto;
  15. }
  16. .nav{
  17. width: 820px; //图片多大这里就设多大
  18. height: 340px;
  19. border: 1px solid;
  20. overflow: hidden;
  21. }
  22. .nav_img{
  23. float: left;
  24. margin-left: 0px;
  25. width: 4950px; //总长度
  26. font-size: 0;
  27. }
  28. .nav_xiao{
  29. position: absolute;
  30. bottom: 0;
  31. left: 50%;
  32. transform: translateX(-50%);
  33. }
  34. .nav_xiao>ul{
  35. display: flex;
  36. }
  37. .nav_xiao>ul>li{
  38. width: 30px;
  39. height: 30px;
  40. border-radius: 50%;
  41. background: #0da0b4;
  42. margin-left: 5px;
  43. }
  44. .nav_xiao>ul .mos{
  45. background: red;
  46. }
  47. .nav_left>div:nth-child(1){
  48. position: absolute;
  49. left: 0;
  50. top: 50%;
  51. font-size: 35px;
  52. border: 1px solid red;
  53. }
  54. .nav_left>div:nth-child(2){
  55. position: absolute;
  56. right: 0;
  57. top: 50%;
  58. font-size: 35px;
  59. border: 1px solid red;
  60. }
  61. .bnner_er ul>li>img{
  62. width: 120px;
  63. height: 120px;
  64. }
  65. .bnner_er>div{
  66. width: 250px;
  67. overflow: hidden;
  68. }
  69. .bnner_er ul{
  70. width: 1008px;
  71. /*display: flex;*/
  72. /*overflow: hidden;*/
  73. }
  74. .bnner_er ul>li{
  75. width: 250px;
  76. height: 120px;
  77. float: left;
  78. border: 1px solid red;
  79. }
  80. </style>
  81. <body>
  82. <div class="bnner">
  83. <div class="nav">
  84. <div class="nav_img">
  85. <img src="imgs/1.jpeg" alt="">
  86. <img src="imgs/2.jpeg" alt="">
  87. <img src="imgs/3.jpeg" alt="">
  88. <img src="imgs/2.jpeg" alt="">
  89. <img src="imgs/3.jpeg" alt="">
  90. <img src="imgs/1.jpeg" alt="">
  91. </div>
  92. </div>
  93. <div class="nav_xiao">
  94. <ul>
  95. <li class="mos"></li>
  96. <li></li>
  97. <li></li>
  98. <li></li>
  99. <li></li>
  100. </ul>
  101. </div>
  102. <div class="nav_left">
  103. <div></div>
  104. <div></div>
  105. </div>
  106. </div>
  107. <script>
  108. function f(indexs) {
  109. $('.bnner').on('mouseenter',function(){
  110. clearInterval(time)
  111. console.log(111)
  112. }).on('mouseleave',function(){
  113. time = setInterval(move,indexs)
  114. })
  115. //图片
  116. let ssd=$('.nav').width()
  117. console.log(ssd)
  118. let ss=-ssd;
  119. let sum=0;
  120. function move(){
  121. sum++;
  122. if (sum>=6){
  123. $('.nav_img').css({
  124. 'margin-left':0
  125. });
  126. sum=1
  127. }
  128. console.log(sum)
  129. $('.nav_xiao li').css('background','#0da0b4')
  130. $('.nav_xiao li').eq(sum).css('background','red')
  131. $('.nav_img').animate({
  132. 'margin-left':ss*sum
  133. },500)
  134. if (sum==5){
  135. $('.nav_xiao li').css('background','#0da0b4')
  136. $('.nav_xiao li').eq(0).css('background','red')
  137. }
  138. }
  139. //右
  140. $('.nav_left div:nth-child(2)').on('click',function(){
  141. // function mover(){
  142. sum++
  143. if (sum>=6){
  144. $('.nav_img').css({
  145. 'margin-left':0
  146. });
  147. sum=1
  148. }
  149. $('.nav_img').stop().animate({
  150. 'margin-left':ss*sum
  151. },500)
  152. $('.nav_xiao li').css('background','#0da0b4')
  153. $('.nav_xiao li').eq(sum).css('background','red')
  154. // }
  155. if (sum==5){
  156. $('.nav_xiao li').css('background','#0da0b4')
  157. $('.nav_xiao li').eq(0).css('background','red')
  158. }
  159. })
  160. //左
  161. $('.nav_left div:nth-child(1)').on('click',function(){
  162. sum--;
  163. if (sum<0){
  164. $('.nav_img').css({
  165. 'margin-left':-4100 //最后一张图片的起始距离 注意并不是总长度 总长度-一张的长度
  166. })
  167. sum=4
  168. }
  169. console.log(sum)
  170. $('.nav_img').animate({
  171. 'margin-left':ss*sum
  172. })
  173. $('.nav_xiao li').css('background','#0da0b4')
  174. $('.nav_xiao li').eq(sum).css('background','red')
  175. });
  176. //小圆点
  177. $('.nav_xiao li').each(function(index,ele){
  178. $(this).on('click',function(){
  179. $('.nav_xiao li').css('background','#0da0b4')
  180. $(this).css('background','red')
  181. $('.nav_img').animate({
  182. 'margin-left':ss*index
  183. })
  184. sum=index
  185. })
  186. })
  187. let time = setInterval(move,indexs)
  188. }
  189. f(2000);
  190. </script>
  191. </body>
  192. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/120700
推荐阅读
相关标签
  

闽ICP备14008679号