jquery.carousel.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Created by Zhangyx on 2015/10/15.
  3. */
  4. ;(function($){
  5. var Caroursel = function (caroursel){
  6. var self = this;
  7. this.caroursel = caroursel;
  8. this.posterList = caroursel.find(".poster-list");
  9. this.posterItems = caroursel.find(".poster-item");
  10. this.firstPosterItem = this.posterItems.first();
  11. this.lastPosterItem = this.posterItems.last();
  12. this.prevBtn = this.caroursel.find(".poster-prev-btn");
  13. this.nextBtn = this.caroursel.find(".poster-next-btn");
  14. //默认参数
  15. this.setting = {
  16. "width":"1000",
  17. "height":"270",
  18. "posterWidth":"640",
  19. "posterHeight":"270",
  20. "scale":"0.8",
  21. "speed":"1000",
  22. "isAutoplay":"true",
  23. "dealy":"1000"
  24. }
  25. //自定义参数与默认参数合并
  26. $.extend(this.setting,this.getSetting())
  27. //设置第一帧位置
  28. this.setFirstPosition();
  29. //设置剩余帧的位置
  30. this.setSlicePosition();
  31. //旋转
  32. this.rotateFlag = true;
  33. this.prevBtn.bind("click",function(){
  34. if(self.rotateFlag){
  35. self.rotateFlag = false;
  36. self.rotateAnimate("left")
  37. }
  38. });
  39. this.nextBtn.bind("click",function(){
  40. if(self.rotateFlag){
  41. self.rotateFlag = false;
  42. self.rotateAnimate("right")
  43. }
  44. });
  45. if(this.setting.isAutoplay){
  46. this.autoPlay();
  47. this.caroursel.hover(function(){clearInterval(self.timer)},function(){self.autoPlay()})
  48. }
  49. };
  50. Caroursel.prototype = {
  51. autoPlay:function(){
  52. var that = this;
  53. this.timer = window.setInterval(function(){
  54. that.prevBtn.click();
  55. },that.setting.dealy)
  56. },
  57. rotateAnimate:function(type){
  58. var that = this;
  59. var zIndexArr = [];
  60. if(type == "left"){//向左移动
  61. this.posterItems.each(function(){
  62. var self = $(this),
  63. prev = $(this).next().get(0)?$(this).next():that.firstPosterItem,
  64. width = prev.css("width"),
  65. height = prev.css("height"),
  66. zIndex = prev.css("zIndex"),
  67. opacity = prev.css("opacity"),
  68. left = prev.css("left"),
  69. top = prev.css("top");
  70. zIndexArr.push(zIndex);
  71. self.animate({
  72. "width":width,
  73. "height":height,
  74. "left":left,
  75. "opacity":opacity,
  76. "top":top,
  77. },that.setting.speed,function(){
  78. that.rotateFlag = true;
  79. });
  80. });
  81. this.posterItems.each(function(i){
  82. $(this).css("zIndex",zIndexArr[i]);
  83. });
  84. }
  85. if(type == "right"){//向右移动
  86. this.posterItems.each(function(){
  87. var self = $(this),
  88. next = $(this).prev().get(0)?$(this).prev():that.lastPosterItem,
  89. width = next.css("width"),
  90. height = next.css("height"),
  91. zIndex = next.css("zIndex"),
  92. opacity = next.css("opacity"),
  93. left = next.css("left"),
  94. top = next.css("top");
  95. zIndexArr.push(zIndex);
  96. self.animate({
  97. "width":width,
  98. "height":height,
  99. "left":left,
  100. "opacity":opacity,
  101. "top":top,
  102. },that.setting.speed,function(){
  103. that.rotateFlag = true;
  104. });
  105. });
  106. this.posterItems.each(function(i){
  107. $(this).css("zIndex",zIndexArr[i]);
  108. });
  109. }
  110. },
  111. setFirstPosition:function(){
  112. this.caroursel.css({"width":this.setting.width,"height":this.setting.height});
  113. this.posterList.css({"width":this.setting.width,"height":this.setting.height});
  114. var width = (this.setting.width - this.setting.posterWidth) / 2;
  115. //设置两个按钮的样式
  116. this.prevBtn.css({"width":width , "height":this.setting.height,"zIndex":Math.ceil(this.posterItems.size()/2)});
  117. this.nextBtn.css({"width":width , "height":this.setting.height,"zIndex":Math.ceil(this.posterItems.size()/2)});
  118. this.firstPosterItem.css({
  119. "width":this.setting.posterWidth,
  120. "height":this.setting.posterHeight,
  121. "left":width,
  122. "zIndex":Math.ceil(this.posterItems.size()/2),
  123. "top":this.setVertialType(this.setting.posterHeight)
  124. });
  125. },
  126. setSlicePosition:function(){
  127. var _self = this;
  128. var sliceItems = this.posterItems.slice(1),
  129. level = Math.floor(this.posterItems.length/2),
  130. leftItems = sliceItems.slice(0,level),
  131. rightItems = sliceItems.slice(level),
  132. posterWidth = this.setting.posterWidth,
  133. posterHeight = this.setting.posterHeight,
  134. Btnwidth = (this.setting.width - this.setting.posterWidth) / 2,
  135. gap = Btnwidth/level,
  136. containerWidth = this.setting.width;
  137. //设置左边帧的位置
  138. var i = 1;
  139. var leftWidth = posterWidth;
  140. var leftHeight = posterHeight;
  141. var zLoop1 = level;
  142. leftItems.each(function(index,item){
  143. leftWidth = posterWidth * _self.setting.scale;
  144. leftHeight = posterHeight*_self.setting.scale;
  145. $(this).css({
  146. "width":leftWidth,
  147. "height":leftHeight,
  148. "left": Btnwidth - i*gap,
  149. "zIndex":zLoop1--,
  150. "opacity":1/(i+1),
  151. "top":_self.setVertialType(leftHeight)
  152. });
  153. i++;
  154. });
  155. //设置右面帧的位置
  156. var j = level;
  157. var zLoop2 = 1;
  158. var rightWidth = posterWidth;
  159. var rightHeight = posterHeight;
  160. rightItems.each(function(index,item){
  161. var rightWidth = posterWidth * _self.setting.scale;
  162. var rightHeight = posterHeight*_self.setting.scale;
  163. $(this).css({
  164. "width":rightWidth,
  165. "height":rightHeight,
  166. "left": containerWidth -( Btnwidth - j*gap + rightWidth),
  167. "zIndex":zLoop2++,
  168. "opacity":1/(j+1),
  169. "top":_self.setVertialType(rightHeight)
  170. });
  171. j--;
  172. });
  173. },
  174. getSetting:function(){
  175. var settting = this.caroursel.attr("data-setting");
  176. if(settting.length > 0){
  177. return $.parseJSON(settting);
  178. }else{
  179. return {};
  180. }
  181. },
  182. setVertialType:function(height){
  183. var algin = this.setting.algin;
  184. if(algin == "top") {
  185. return 0
  186. }else if(algin == "middle"){
  187. return (this.setting.posterHeight - height) / 2
  188. }else if(algin == "bottom"){
  189. return this.setting.posterHeight - height
  190. }else {
  191. return (this.setting.posterHeight - height) / 2
  192. }
  193. }
  194. }
  195. Caroursel.init = function (caroursels){
  196. caroursels.each(function(index,item){
  197. new Caroursel($(this));
  198. }) ;
  199. };
  200. window["Caroursel"] = Caroursel;
  201. })(jQuery)