swiper.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. let tap = "ontouchstart" in window ? "tap" : "click";
  2. (function ($, fn) {
  3. $(document).ready(fn);
  4. }(jQuery, function () {
  5. var wrap = $(".swiperContainer");
  6. $.each(wrap, function (index, el) {
  7. var _this = $(this);
  8. (function (banner) {
  9. var navs, imgs, adnav, center, icur, prev, next, showIndex, btn_prev, btn_next, clickDown, bannerTimer, moveEnd;
  10. if (!banner[0]) return;
  11. imgs = banner.find(".swiperItem");
  12. adnav = banner.find(".swiperNav");
  13. btn_prev = banner.find(".prevbtn");
  14. btn_next = banner.find(".nextbtn");
  15. showIndex = 0;
  16. moveEnd = false;
  17. clickDown = false;
  18. function animateLeft(index) {
  19. if (moveEnd) return;
  20. moveEnd = true;
  21. showIndex = index;
  22. next = imgs.eq(showIndex);
  23. next.addClass("next");
  24. navs.removeClass("icur").eq(showIndex).addClass("icur");
  25. icur.stop().animate({
  26. "left": "-100%"
  27. }, 500, "swing");
  28. next.stop().animate({
  29. "left": "0"
  30. }, 500, "swing", function () {
  31. imgs.removeClass("icur").removeClass("prev").removeClass("next");
  32. icur = imgs.eq(showIndex);
  33. icur.addClass("icur");
  34. imgs.removeAttr("style");
  35. moveEnd = false;
  36. clickDown = false;
  37. });
  38. }
  39. function animateRight(index) {
  40. if (moveEnd) return;
  41. moveEnd = true;
  42. showIndex = index;
  43. prev = imgs.eq(showIndex);
  44. prev.addClass("prev");
  45. navs.removeClass("icur").eq(showIndex).addClass("icur");
  46. icur.stop().animate({
  47. "left": "100%"
  48. }, 500, "swing");
  49. prev.stop().animate({
  50. "left": "0"
  51. }, 500, "swing", function () {
  52. imgs.removeClass("icur").removeClass("prev").removeClass("next");
  53. icur = imgs.eq(showIndex);
  54. icur.addClass("icur");
  55. imgs.removeAttr("style");
  56. moveEnd = false;
  57. clickDown = false;
  58. });
  59. }
  60. function autoplay() {
  61. showIndex++;
  62. showIndex %= imgs.length;
  63. animateLeft(showIndex);
  64. }
  65. (
  66. function () {
  67. if (imgs.length < 2) return;
  68. center = $("<center></center>");
  69. for (var i = 0, len = imgs.length; i < len; i++) {
  70. var ovar = $("<span></span>");
  71. if (i == 0) {
  72. ovar.addClass("icur");
  73. }
  74. center.append(ovar);
  75. }
  76. adnav.append(center);
  77. }(),
  78. function () {
  79. navs = adnav.find("span");
  80. icur = imgs.eq(0);
  81. icur.addClass("icur");
  82. $.each(navs, function (index) {
  83. var _this = $(this);
  84. (function (nav, index) {
  85. nav.hover(function () {
  86. var poor = index - showIndex;
  87. if (index == showIndex) return false;
  88. poor > 0 ? animateLeft(index) : animateRight(index);
  89. });
  90. }(_this, index));
  91. });
  92. }(),
  93. function () {
  94. if (imgs.length < 2) return;
  95. bannerTimer = setInterval(autoplay, 4000);
  96. banner.on("mouseenter", function () {
  97. clearInterval(bannerTimer);
  98. });
  99. banner.on("mouseleave", function () {
  100. clearInterval(bannerTimer);
  101. bannerTimer = setInterval(autoplay, 4000);
  102. });
  103. banner.on("touchstart", function () {
  104. clearInterval(bannerTimer);
  105. bannerTimer = setInterval(autoplay, 4000);
  106. });
  107. banner.touchleft(function () {
  108. var num = 0;
  109. clearInterval(bannerTimer);
  110. num = showIndex + 1;
  111. num %= imgs.length;
  112. animateLeft(num);
  113. bannerTimer = setInterval(autoplay, 4000);
  114. });
  115. banner.touchright(function () {
  116. var num = 0;
  117. clearInterval(bannerTimer);
  118. num = showIndex - 1;
  119. num = num < 0 ? imgs.length - 1 : num;
  120. animateRight(num);
  121. bannerTimer = setInterval(autoplay, 4000);
  122. });
  123. btn_prev.on(tap, function () {
  124. var num = 0;
  125. if (clickDown) return;
  126. clickDown = true;
  127. clearInterval(bannerTimer);
  128. num = showIndex - 1;
  129. num = num < 0 ? imgs.length - 1 : num;
  130. animateRight(num);
  131. });
  132. btn_next.on(tap, function () {
  133. var num = 0;
  134. if (clickDown) return;
  135. clickDown = true;
  136. clearInterval(bannerTimer);
  137. num = showIndex + 1;
  138. num %= imgs.length;
  139. animateLeft(num);
  140. });
  141. }()
  142. );
  143. }(_this));
  144. });
  145. }));