LoopSlider_ksh.js 3.9 KB

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