LoopSlider.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. var LoopSlider = {
  3. currentIndex: 0,
  4. moveDis: 0,
  5. selector: "",
  6. max: 0,
  7. delay:800,
  8. duration: 300,
  9. easing: "ease",
  10. init: function(obj, play) {
  11. var _t = this;
  12. _t.selector = obj.el;
  13. $(function() {
  14. if (obj.easing != null)
  15. _t.easing = obj.easing;
  16. _t.duration = obj.duration;
  17. //initstall elements!
  18. if(play != 1){
  19. _t.delay = obj.autoplay;
  20. if(_t.delay||_t.delay<=0){
  21. _t.autoplay();
  22. }
  23. }
  24. const imgs = $(obj.el).children();
  25. _t.moveDis = $(obj.el).width();
  26. _t.max = imgs.length;
  27. imgs.wrap("<div class='slide-frame'></div>");
  28. const f_clone = $(".slide-frame:first").clone();
  29. f_clone.insertAfter($(".slide-frame:last"));
  30. $(".slide-frame").wrapAll("<div class='slider-bar'></div>");
  31. $(".slider-bar").wrap("<div class='slider-shade'></div>")
  32. $(".slider-bar").css({
  33. overflow: "hidden",
  34. position: "absolute",
  35. left: 0,
  36. height: "100%",
  37. width: `${_t.moveDis*(imgs.length + 2)}px`
  38. });
  39. $(".slide-frame").css({
  40. width: `${_t.moveDis}px`,
  41. overflow: "hidden",
  42. height: "100%",
  43. float: "left"
  44. });
  45. $(obj.el).append("<div class='" + obj.navigator.nextEl.replace(".", "") + "'> > </div>");
  46. $(obj.el).append("<div class='" + obj.navigator.prevEl.replace(".", "") + "'> < </div>")
  47. $(obj.el).append("<div class='points'></div>");
  48. for (let i = 0; i < imgs.length; i++) {
  49. if (i == 0) {
  50. $(".points").append("<li class='active'></li>");
  51. } else {
  52. $(".points").append("<li></li>");
  53. }
  54. }
  55. _t.selector = $(".slider-bar");
  56. var EventListen = () => {
  57. $(obj.navigator.nextEl).click(() => {
  58. _t.next(_t);
  59. });
  60. $(obj.navigator.prevEl).click(() => {
  61. _t.prev(_t)
  62. });
  63. }
  64. EventListen();
  65. _t.pointsClick(_t);
  66. });
  67. $(window).resize(function(){
  68. $(".slider-bar").css({
  69. transition:"all 0s"
  70. });
  71. });
  72. },
  73. time :null,
  74. id:null,
  75. autoplay:function(){
  76. var _t = this;
  77. $(_t.selector).hover(function(){
  78. clearTimeout(_t.time);
  79. cancelAnimationFrame(_t.id);
  80. },function(){
  81. play();
  82. });
  83. var play = function(){
  84. _t.time = setTimeout(function(){
  85. _t.id = requestAnimationFrame(play);
  86. _t.next(_t);
  87. },_t.delay);
  88. }
  89. play();
  90. },
  91. prev: function(t) {
  92. t.currentIndex--;
  93. if (t.currentIndex < 0) {
  94. t.backCall(true, t);
  95. t.currentIndex = t.max - 1;
  96. } else {
  97. t.slideGoTo(t.currentIndex, t)
  98. }
  99. t.aimPoint(t.currentIndex, false);
  100. },
  101. next: (t) => {
  102. t.currentIndex++;
  103. let pageIndex = t.currentIndex;
  104. if (t.currentIndex > t.max) {
  105. t.backCall(false, t);
  106. t.currentIndex = 1;
  107. } else {
  108. t.slideGoTo(t.currentIndex, t)
  109. }
  110. t.aimPoint(t.currentIndex, true);
  111. },
  112. backCall: (os, _this) => {
  113. if (os) {
  114. _this.selector.css({
  115. transform: `translate3d(-${_this.max*_this.moveDis}px,0px,0px)`,
  116. transition: "all 0s"
  117. });
  118. _this.slideTo(_this, (_this.max - 1) * _this.moveDis)
  119. } else {
  120. //Right
  121. _this.selector.css({
  122. transform: `translate3d(0px,0px,0px)`,
  123. transition: "all 0s"
  124. });
  125. _this.slideTo(_this, _this.moveDis);
  126. }
  127. },
  128. slideTo: (t, backposition) => {
  129. requestAnimationFrame(() => {
  130. requestAnimationFrame(() => {
  131. t.selector.css({
  132. transform: `translate3d(-${backposition}px,0px,0px)`,
  133. transition: `all ${t.duration}ms ${t.easing}`
  134. });
  135. })
  136. })
  137. },
  138. perIndex: 0,
  139. aimPoint: function(index, bool) {
  140. if (bool) {
  141. this.perIndex += 1;
  142. } else {
  143. this.perIndex -= 1;
  144. }
  145. if (this.perIndex >= this.max) {
  146. this.perIndex = 0;
  147. } else if (this.perIndex < 0) {
  148. this.perIndex = this.max - 1;
  149. }
  150. this.activePoint(this.perIndex);
  151. },
  152. activePoint: function(index) {
  153. $(".points li").eq(index).addClass("active").siblings("li").removeClass("active");
  154. },
  155. pointsClick: function(t) {
  156. $(".points li").click(function() {
  157. t.slideGoTo($(this).index(), t);
  158. });
  159. },
  160. slideGoTo: function(index, t) {
  161. t.selector.css({
  162. transform: `translate3d(-${index * t.moveDis}px,0px,0px)`,
  163. transition: `all ${t.duration}ms ${t.easing}`
  164. });
  165. t.activePoint(index);
  166. },
  167. }