carousel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. (function(window, undefined) {
  2. var Carousel = function(ele, obj) {
  3. console.log(obj)
  4. this.ele = ele;
  5. this.cardsList = this.ele.children();
  6. this.cardsNum = this.cardsList.length;
  7. this.direction = obj.direction ? obj.direction : 'horizontal';
  8. this.scale = obj.scale;
  9. this.transition = obj.transition || '500ms';
  10. this.transformOrigin = obj.transformOrigin;
  11. this.opacity = obj.opacity;
  12. this.offsetDistance = obj.offsetDistance;
  13. this.switchBtn = obj.switchBtn;
  14. this.isClickCard = obj.isClickCard || false;
  15. this.isAuto = obj.isAuto || false;
  16. this.interval = obj.interval || 5000;
  17. this.n = obj.n;
  18. this.mediant = Math.floor(this.cardsNum / 2);
  19. this.cardsListArr = [].slice.call(this.cardsList);
  20. this.timer = null;
  21. this.cssObj = {};
  22. this.showPicNumber = obj.showPicNumber || null;
  23. this.certainStyle();
  24. this.switchEffect();
  25. this.cardsClick();
  26. this.switchBtnClick();
  27. this.autoMove();
  28. this.switchTimer();
  29. this.dataList = obj.dataList;
  30. this.nav = obj.nav;
  31. };
  32. Carousel.prototype = {
  33. constructor: Carousel,
  34. switchTimer: function() {
  35. var carousel = this;
  36. if (this.isAuto) {
  37. this.cardsList.on('mouseover', function() {
  38. clearInterval(carousel.timer);
  39. });
  40. this.cardsList.on('mouseout', function() {
  41. carousel.autoMove();
  42. });
  43. };
  44. },
  45. certainStyle: function() {
  46. this.cssObj.position = 'absolute';
  47. if (this.scale) {
  48. this.scale.length == 1 && this.scale.push(this.scale[0]);
  49. };
  50. if (this.direction == 'horizontal') {
  51. this.cssObj.left = '50%';
  52. this.availSize = this.ele.outerWidth();
  53. this.cardSize = this.cardsList.eq(0).outerWidth();
  54. } else if (this.direction == 'vertical') {
  55. this.cssObj.top = '50%';
  56. this.availSize = this.ele.outerHeight();
  57. this.cardSize = this.cardsList.eq(0).outerHeight();
  58. // console.log(this.cssObj)
  59. };
  60. this.offsetDistance = this.offsetDistance || this.availSize / (this.cardsNum - 1);
  61. this.transformOrigin && (this.cssObj.transformOrigin = this.transformOrigin);
  62. this.cssObj.transition = this.transition;
  63. // console.log(this.cssObj)
  64. },
  65. dynamicStyle: function(offsetIndex, offsetIndexAbs, reset, showPicNumber) {
  66. if (this.direction == 'horizontal') {
  67. // console.log(this.cssObj)
  68. // console.log(this.cardSize, this.offsetDistance, offsetIndex, "this.cardSize")
  69. // console.log(window.innerWidth, "innerWidth")
  70. let arrLength = this.cardsListArr.length;
  71. // 根据轮播图的长度和屏幕宽度做相应偏移量调整
  72. switch (arrLength) {
  73. case 2:
  74. {
  75. if (window.innerWidth < 768 && offsetIndex !== 0) {
  76. if(offsetIndex > 0){
  77. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance / 2 * offsetIndex + (-offsetIndex) * 170 + 'px';
  78. }else{
  79. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance / 2 * offsetIndex + (-offsetIndex) * 167 + 'px';
  80. }
  81. } else if (window.innerWidth < 1200 && offsetIndex !== 0) {
  82. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance / 2 * offsetIndex + (-offsetIndex) * 60 + 'px';
  83. } else if (window.innerWidth >= 1200 && offsetIndex !== 0) {
  84. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance / 2 * offsetIndex + (-offsetIndex) * 10 + 'px';
  85. } else {
  86. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + 'px';
  87. }
  88. };
  89. break;
  90. case 3:
  91. case 4:
  92. {
  93. if (window.innerWidth < 768 && offsetIndex !== 0) {
  94. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance / 2 * offsetIndex + (-offsetIndex) * 15 + 'px';
  95. } else if (window.innerWidth < 1200 && offsetIndex !== 0) {
  96. if (offsetIndex == -1) {
  97. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + 52 + 'px';
  98. } else {
  99. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex - 60 + 'px';
  100. }
  101. } else if (window.innerWidth >= 1200 && offsetIndex !== 0) {
  102. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + (-offsetIndex) * 10 + 'px';
  103. } else {
  104. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + 'px';
  105. }
  106. };
  107. break;
  108. default:
  109. {
  110. if (window.innerWidth < 768) {
  111. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + (-offsetIndex) * 4 + 'px';
  112. } else if(window.innerWidth < 1200) {
  113. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + 'px';
  114. }else{
  115. this.cssObj.marginLeft = -this.cardSize / 2 + this.offsetDistance * offsetIndex + (offsetIndex) * 30 + 'px';
  116. }
  117. };
  118. break;
  119. }
  120. } else if (this.direction == 'vertical') {
  121. this.cssObj.marginTop = -this.cardSize / 2 + this.offsetDistance * offsetIndex + 'px';
  122. };
  123. this.cssObj.zIndex = reset;
  124. // this.opacity && (this.cssObj.opacity = Math.pow(this.opacity, offsetIndexAbs));
  125. // 根据要展示的图片数,来给定透明度,showPicNumber为1时展示主图,为3时展示3张图,不传时则默认所有都展示,其它则不展示
  126. if (this.opacity) {
  127. switch (showPicNumber) {
  128. case 1:
  129. {
  130. if (Math.pow(this.opacity, offsetIndexAbs) === 1) {
  131. this.cssObj.opacity = 1;
  132. } else {
  133. this.cssObj.opacity = 0;
  134. }
  135. };
  136. break;
  137. case 3:
  138. {
  139. if (Math.pow(this.opacity, offsetIndexAbs) >= 0.9) {
  140. this.cssObj.opacity = Math.pow(this.opacity, offsetIndexAbs)
  141. } else {
  142. this.cssObj.opacity = 0
  143. }
  144. };
  145. break;
  146. default:
  147. this.cssObj.opacity = Math.pow(this.opacity, offsetIndexAbs);
  148. break;
  149. }
  150. }
  151. this.scale && (this.cssObj.transform = 'scale(' + Math.pow(this.scale[0], offsetIndexAbs) + ',' + Math.pow(this.scale[1], offsetIndexAbs) + ')');
  152. this.cssObj.background = "rgba(0,0,0,1);"
  153. var show = "box-shadow"
  154. this.cssObj.show = '0px 8px 13px 0px rgba(23,28,46,0.88)'
  155. console.log(this.scale[1])
  156. // if(this.opacity == 0.9){
  157. // this.cssObj.background = 'red';
  158. // }
  159. // this.cssObj.background = 'red';
  160. },
  161. switchEffect: function() {
  162. var carousel = this;
  163. const showPicNumber = carousel.showPicNumber;
  164. var offsetNum = Math.abs(carousel.n - carousel.mediant);
  165. console.log('arousel.n>>>', carousel.n)
  166. console.log("carousel.mediant>>>", carousel.mediant)
  167. if (carousel.n == carousel.mediant && carousel.nav == 1) {
  168. return;
  169. } else if (carousel.n > carousel.mediant) {
  170. // alert("提示信息2")
  171. carousel.cardsListArr = carousel.cardsListArr.concat(carousel.cardsListArr.splice(0, offsetNum));
  172. } else {
  173. // alert("提示信息3")
  174. carousel.cardsListArr = carousel.cardsListArr.splice(carousel.cardsNum - offsetNum).concat(carousel.cardsListArr);
  175. };
  176. $(carousel.cardsListArr).each(function(i, e) {
  177. var offsetIndex = i - carousel.mediant,
  178. offsetIndexAbs = Math.abs(offsetIndex),
  179. reset = carousel.mediant - offsetIndexAbs;
  180. carousel.dynamicStyle(offsetIndex, offsetIndexAbs, reset, showPicNumber);
  181. // console.log(carousel.cssObj)
  182. $(e).css(carousel.cssObj);
  183. $(e).attr('n', i);
  184. });
  185. },
  186. cardsClick: function() {
  187. console.log(">>>>>>>>>s", this.isClickCard)
  188. if (this.isClickCard) {
  189. var carousel = this;
  190. $(carousel.cardsListArr).on('click', function() {});
  191. // $('.nav-gse').on('click',function(){
  192. // $(this).addClass('nav-gse-ave')
  193. // $(this).siblings().removeClass('nav-gse-ave')
  194. //// css({"background":"red"})
  195. // $(this).find('.nav-bg').find('.nav-mnai').removeClass('nav-mnai')
  196. // $(this).siblings().find('.nav-bg').find('.nav-buseqw').addClass('nav-mnai')
  197. // carousel.switchEffect();
  198. // })
  199. };
  200. },
  201. prev: function() {
  202. var carousel = this;
  203. carousel.n = carousel.mediant - 1;
  204. carousel.n < 0 && (carousel.n = carousel.cardsNum - 1);
  205. carousel.switchEffect();
  206. // $("#containerse li").each(function(i, e) {
  207. // if($(e).css("opacity") == 1){
  208. // indexs = i
  209. // }
  210. // });
  211. //
  212. // console.log("dataList>>>",carousel.dataList)
  213. },
  214. next: function() {
  215. var carousel = this;
  216. carousel.n = carousel.mediant + 1;
  217. carousel.n > carousel.cardsNum - 1 && (carousel.n = 0);
  218. carousel.switchEffect();
  219. },
  220. switchBtnClick: function() {
  221. var carousel = this;
  222. if (carousel.switchBtn) {
  223. carousel.switchBtn.eq(0).on('click', function() {
  224. carousel.prev();
  225. if (carousel.isAuto) {
  226. clearInterval(carousel.timer);
  227. carousel.cardsList.eq(carousel.n).one('transitionend', function() {
  228. carousel.autoMove();
  229. });
  230. };
  231. });
  232. carousel.switchBtn.eq(1).on('click', function() {
  233. carousel.next();
  234. if (carousel.isAuto) {
  235. clearInterval(carousel.timer);
  236. carousel.cardsList.eq(carousel.n).one('transitionend', function() {
  237. carousel.autoMove();
  238. });
  239. };
  240. });
  241. };
  242. },
  243. autoMove: function() {
  244. var carousel = this;
  245. if (carousel.isAuto) {
  246. clearInterval(carousel.timer);
  247. carousel.timer = setInterval(function() {
  248. carousel.next();
  249. }, carousel.interval);
  250. };
  251. },
  252. };
  253. window.Carousel = Carousel;
  254. }(window));