基本功能

  • 轮回播放
  • 自定义过渡时间、间隔时间
  • 上一张、下一张

完整代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. * {
  8. padding: 0;
  9. margin: 0;
  10. }
  11. .container {
  12. width: 126px;
  13. height: 86px;
  14. overflow: hidden;
  15. margin: 100px auto;
  16. background-color: aquamarine;
  17. }
  18. .swiper {
  19. list-style: none;
  20. display: flex;
  21. /* transition: all 400ms ease-in-out; */
  22. }
  23. .swiper:hover {
  24. animation-play-state: paused;
  25. }
  26. .item {
  27. flex-basis: 126px;
  28. flex-shrink: 0;
  29. width: 126px;
  30. height: 86px;
  31. background: rgba(11, 22, 11, .4);
  32. box-sizing: border-box;
  33. border: 2px solid red;
  34. }
  35. .img {
  36. width: 100%;
  37. height: 100%;
  38. overflow: hidden;
  39. }
  40. .control {
  41. display: flex;
  42. justify-content: center;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="container">
  48. <ul class="swiper">
  49. <li class="item">
  50. <!-- 1 -->
  51. <img class="img" src="images/1.jpg" alt="" />
  52. </li>
  53. <li class="item">
  54. <!-- 2 -->
  55. <img class="img" src="images/2.jpg" alt="" />
  56. </li>
  57. <li class="item">
  58. <!-- 3 -->
  59. <img class="img" src="images/3.jpg" alt="" />
  60. </li>
  61. <li class="item">
  62. <!-- 4 -->
  63. <img class="img" src="images/4.jpg" alt="" />
  64. </li>
  65. <li class="item">
  66. <!-- 5 -->
  67. <img class="img" src="images/5.jpg" alt="" />
  68. </li>
  69. <li class="item">
  70. <!-- 6 -->
  71. <img class="img" src="images/6.jpg" alt="" />
  72. </li>
  73. <li class="item">
  74. <!-- 7 -->
  75. <img class="img" src="images/7.jpg" alt="" />
  76. </li>
  77. </ul>
  78. </div>
  79. <div class="control">
  80. <div>
  81. 轮播间隔:
  82. <input id="input1" type="text" value="800">
  83. <br />
  84. 过渡时间:
  85. <input id="input2" type="text" value="400">
  86. <br />
  87. <button id="btn">重置轮播</button>
  88. <br />
  89. <button id="pre">上一张</button>
  90. <button id="next">下一张</button>
  91. </div>
  92. </div>
  93. <script>
  94. window.onload = function () {
  95. const swiper = document.querySelectorAll('.swiper')[0]
  96. const count = document.querySelectorAll('.item').length
  97. let timer
  98. let mark = -1
  99. let i = 0
  100. const getI = (i) => {
  101. if (i < 1 || (i < count - 1 && i > mark)) {
  102. mark = i
  103. return ++i
  104. } else {
  105. --i
  106. mark = i
  107. return i
  108. }
  109. }
  110. const refrence = () => {
  111. clearInterval(timer)
  112. const interval = document.querySelector('#input1').value
  113. const duration = document.querySelector('#input2').value
  114. console.log(interval, duration)
  115. swiper.style.transition = `all ${duration}ms ease-in-out`
  116. timer = setInterval(() => {
  117. i = getI(i)
  118. swiper.style.transform = `translate(${-126 * i}px)`
  119. }, interval)
  120. }
  121. refrence()
  122. document.querySelector('#btn').onclick = refrence
  123. document.querySelector('#pre').onclick = () => {
  124. i = getI(i)
  125. swiper.style.transform = `translate(${-126 * i}px)`
  126. }
  127. document.querySelector('#next').onclick = () => {
  128. i = getI(i)
  129. swiper.style.transform = `translate(${-126 * i}px)`
  130. }
  131. }
  132. </script>
  133. </body>
  134. </html>

样图

image.png

依赖

优化

上一张、下一张有问题

完整代码

https://github.com/withwz/swiper