slider.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ;(function($, window, document, undefined) {
  2. var Carousel = function(elem, options) {
  3. this.defaults = {
  4. curDisplay: 0,
  5. autoPlay: false,
  6. interval: 3000
  7. };
  8. this.opts = $.extend({}, this.defaults, options);
  9. var self = this;
  10. this.$carousel = elem;
  11. this.$aImg = this.$carousel.find('.jtfc_a');
  12. this.imgLen = this.$aImg.length;
  13. this.curDisplay = this.opts.curDisplay;
  14. this.autoPlay = this.opts.autoPlay;
  15. this.viewWidth = $(window).width() / 2;
  16. this.b_switch = true;
  17. this.iNow = this.opts.curDisplay;
  18. this.timer = null;
  19. this.interval = this.opts.interval;
  20. // 生成小点点
  21. var htmlNav = "<ul>";
  22. for (var i = 0; i < this.imgLen; i++) {
  23. if (this.curDisplay == i) {
  24. htmlNav += "<li class=on><a>" + i + "</a></li>";
  25. } else {
  26. htmlNav += "<li><a>" + i + "</a></li>";
  27. }
  28. }
  29. htmlNav += "</ul>";
  30. this.$carousel.parent().append('<div id=bannerNav>' + htmlNav + '</div>');
  31. this.$aNav = this.$carousel.siblings('#bannerNav').find('ul li');
  32. };
  33. var outerWidth = parseInt(document.body.offsetWidth);
  34. var middleWidth = 1920;
  35. var _height = outerWidth >= middleWidth ? 380 : 300;
  36. var _slideHeight = outerWidth >= middleWidth ? 330 : 260;
  37. Carousel.prototype = {
  38. play: function() {
  39. if (this.autoPlay) {
  40. if (this.iNow === this.imgLen - 1) {
  41. this.iNow = 0;
  42. } else {
  43. this.iNow++;
  44. }
  45. this.movingNext(this.iNow);
  46. }
  47. },
  48. movingPrev: function(index) {
  49. this.curDisplay = index;
  50. this.initalCarousel();
  51. },
  52. movingNext: function(index) {
  53. this.curDisplay = index;
  54. this.initalCarousel();
  55. },
  56. initalCarousel: function() {
  57. var self = this;
  58. var half_imgLen = Math.floor(this.imgLen / 2);
  59. var leftNum, rightNum;
  60. var k = 0;
  61. for (var i = 0; i < half_imgLen; i++) {
  62. leftNum = this.curDisplay - i - 1;
  63. if (k == 0) {
  64. this.$aImg.eq(leftNum).css({
  65. transform: 'translateX(' + (-500 * (i + 1)) + 'px) translateZ(-120px)',
  66. width:"auto",
  67. }).animate({
  68. height: _slideHeight + 'px',
  69. marginTop: -_slideHeight / 2 + 'px',
  70. opacity: '0.6'
  71. }, 500);
  72. this.$aImg.eq(leftNum).attr('onclick', null);
  73. rightNum = this.curDisplay + i + 1;
  74. if (rightNum > this.imgLen - 1) rightNum -= this.imgLen;
  75. this.$aImg.eq(rightNum).css({
  76. transform: 'translateX(' + (500 * (i + 1)) + 'px) translateZ(-120px) ',
  77. width:"auto"
  78. }).animate({
  79. height: _slideHeight + 'px',
  80. marginTop: -_slideHeight / 2 + 'px',
  81. opacity: '0.6'
  82. }, 500);
  83. this.$aImg.eq(rightNum).attr('onclick', null);
  84. k++;
  85. } else {
  86. this.$aImg.eq(leftNum).css({
  87. transform: 'translateX(0px) translateZ(-1000px) ',
  88. zIndex:-1
  89. });
  90. rightNum = this.curDisplay + i + 1;
  91. if (rightNum > this.imgLen - 1) rightNum -= this.imgLen;
  92. this.$aImg.eq(rightNum).css({
  93. transform: 'translateX(0px) translateZ(-1000px) ',
  94. zIndex:-1
  95. });
  96. }
  97. this.$aImg.removeClass('on');
  98. this.$aNav.removeClass('on');
  99. }
  100. // var _href = 'location.href=' + "'" + this.$aImg.eq(this.curDisplay).attr('data-url') + "'";
  101. this.$aImg.eq(this.curDisplay).css({
  102. transform: 'translateZ(0px)',
  103. zIndex:99999
  104. }).animate({
  105. height: _height + 'px',
  106. marginTop: -_height / 2 + 'px',
  107. opacity: '1',
  108. }, 500).addClass('on');
  109. this.$aNav.eq(this.curDisplay).addClass('on');
  110. this.$carousel.on('webkitTransitionEnd', function() {
  111. self.b_switch = true;
  112. });
  113. },
  114. inital: function() {
  115. var self = this;
  116. this.initalCarousel();
  117. this.$aImg.on('click', function(ev) {
  118. if (self.b_switch && !$(this).hasClass('on')) {
  119. self.iNow = $(this).index();
  120. self.b_switch = false;
  121. var direction = self.viewWidth < ev.clientX ? 'next' : 'prev';
  122. var index = $(this).index();
  123. if (direction === 'next') {
  124. self.movingNext(index);
  125. } else {
  126. self.movingPrev(index);
  127. }
  128. }
  129. }).hover(function() {
  130. clearInterval(self.timer);
  131. }, function() {
  132. self.timer = setInterval(function() {
  133. self.play();
  134. }, self.interval);
  135. });
  136. this.$aNav.on('click', function(ev) {
  137. if (self.b_switch && !$(this).hasClass('on')) {
  138. self.iNow = $(this).index();
  139. self.b_switch = false;
  140. var direction = self.viewWidth < ev.clientX ? 'next' : 'prev';
  141. var index = $(this).index();
  142. if (direction === 'next') {
  143. self.movingNext(index);
  144. } else {
  145. self.movingPrev(index);
  146. }
  147. }
  148. }).hover(function() {
  149. clearInterval(self.timer);
  150. }, function() {
  151. self.timer = setInterval(function() {
  152. self.play();
  153. }, self.interval);
  154. });
  155. this.timer = setInterval(function() {
  156. self.play();
  157. }, this.interval);
  158. this.$carousel.on('selectstart', function() {
  159. return false;
  160. });
  161. },
  162. constructor: Carousel
  163. };
  164. $.fn.carousel = function(options) {
  165. var carousel = new Carousel(this, options);
  166. return carousel.inital();
  167. };
  168. })(jQuery, window, document, undefined);