410-virtual-slides.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Swiper demo</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1" />
  7. <!-- Link Swiper's CSS -->
  8. <link
  9. rel="stylesheet"
  10. href="../swiper-bundle.min.css"
  11. />
  12. <!-- Demo styles -->
  13. <style>
  14. body {
  15. background: #eee;
  16. font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  17. font-size: 14px;
  18. color: #000;
  19. margin: 0;
  20. padding: 0;
  21. }
  22. .swiper {
  23. width: 100%;
  24. height: 300px;
  25. margin: 20px auto;
  26. }
  27. .swiper-slide {
  28. text-align: center;
  29. font-size: 18px;
  30. background: #fff;
  31. /* Center slide text vertically */
  32. display: -webkit-box;
  33. display: -ms-flexbox;
  34. display: -webkit-flex;
  35. display: flex;
  36. -webkit-box-pack: center;
  37. -ms-flex-pack: center;
  38. -webkit-justify-content: center;
  39. justify-content: center;
  40. -webkit-box-align: center;
  41. -ms-flex-align: center;
  42. -webkit-align-items: center;
  43. align-items: center;
  44. }
  45. .append-buttons {
  46. text-align: center;
  47. margin-top: 20px;
  48. }
  49. .append-buttons a {
  50. display: inline-block;
  51. border: 1px solid #007aff;
  52. color: #007aff;
  53. text-decoration: none;
  54. padding: 4px 10px;
  55. border-radius: 4px;
  56. margin: 0 10px;
  57. font-size: 13px;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <!-- Swiper -->
  63. <div class="swiper">
  64. <div class="swiper-wrapper"></div>
  65. <!-- Add Pagination -->
  66. <div class="swiper-pagination"></div>
  67. <!-- Add Arrows -->
  68. <div class="swiper-button-next"></div>
  69. <div class="swiper-button-prev"></div>
  70. </div>
  71. <p class="append-buttons">
  72. <a href="#" class="prepend-2-slides">Prepend 2 Slides</a>
  73. <a href="#" class="slide-1">Slide 1</a>
  74. <a href="#" class="slide-250">Slide 250</a>
  75. <a href="#" class="slide-500">Slide 500</a>
  76. <a href="#" class="append-slide">Append Slide</a>
  77. </p>
  78. <!-- Swiper JS -->
  79. <script src="../swiper-bundle.min.js"></script>
  80. <!-- Initialize Swiper -->
  81. <script>
  82. let appendNumber = 600;
  83. let prependNumber = 1;
  84. const swiper = new Swiper('.swiper', {
  85. slidesPerView: 3,
  86. centeredSlides: true,
  87. spaceBetween: 30,
  88. pagination: {
  89. el: '.swiper-pagination',
  90. type: 'fraction',
  91. },
  92. navigation: {
  93. nextEl: '.swiper-button-next',
  94. prevEl: '.swiper-button-prev',
  95. },
  96. virtual: {
  97. slides: (function () {
  98. const slides = [];
  99. for (var i = 0; i < 600; i += 1) {
  100. slides.push('Slide ' + (i + 1));
  101. }
  102. return slides;
  103. })(),
  104. },
  105. });
  106. document
  107. .querySelector('.slide-1')
  108. .addEventListener('click', function (e) {
  109. e.preventDefault();
  110. swiper.slideTo(0, 0);
  111. });
  112. document
  113. .querySelector('.slide-250')
  114. .addEventListener('click', function (e) {
  115. e.preventDefault();
  116. swiper.slideTo(249, 0);
  117. });
  118. document
  119. .querySelector('.slide-500')
  120. .addEventListener('click', function (e) {
  121. e.preventDefault();
  122. swiper.slideTo(499, 0);
  123. });
  124. document
  125. .querySelector('.prepend-2-slides')
  126. .addEventListener('click', function (e) {
  127. e.preventDefault();
  128. swiper.virtual.prependSlide([
  129. 'Slide ' + --prependNumber,
  130. 'Slide ' + --prependNumber,
  131. ]);
  132. });
  133. document
  134. .querySelector('.append-slide')
  135. .addEventListener('click', function (e) {
  136. e.preventDefault();
  137. swiper.virtual.appendSlide('Slide ' + ++appendNumber);
  138. });
  139. </script>
  140. </body>
  141. </html>