jquery.zoom.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ;(function($){
  2. $.fn.zoom = function(options){
  3. // 默认配置
  4. var _option = {
  5. align: "left", // 当前展示图片的位置,则放大的图片在其相对的位置
  6. thumb_image_width: 300, // 当前展示图片的宽
  7. thumb_image_height: 400, // 当前展示图片的高
  8. source_image_width: 900, // 放大图片的宽
  9. source_image_height: 1200, // 放大图片的高
  10. zoom_area_width: 600, // 放大图片的展示区域的宽
  11. zoom_area_height: "justify",// 放大图片的展示区域的高
  12. zoom_area_distance: 10, //
  13. zoom_easing: true, // 是否淡入淡出
  14. click_to_zoom: false,
  15. zoom_element: "auto",
  16. show_descriptions: true,
  17. description_location: "bottom",
  18. description_opacity: 0.7,
  19. small_thumbs: 3, // 小图片展示的数量
  20. smallthumb_inactive_opacity: 0.4, // 小图片处于非激活状态时的遮罩透明度
  21. smallthumb_hide_single: true, //
  22. smallthumb_select_on_hover: false,
  23. smallthumbs_position: "bottom", // 小图片的位置
  24. show_icon: true,
  25. hide_cursor: false, // 鼠标放到图片时,是否隐藏指针
  26. speed: 600, //
  27. autoplay: true, // 是否自动播放
  28. autoplay_interval: 6000, // 自动播放时每张图片的停留时间
  29. keyboard: true,
  30. right_to_left: false,
  31. }
  32. if(options){
  33. $.extend(_option, options);
  34. }
  35. var $ul = $(this);
  36. if($ul.is("ul") && $ul.children("li").length && $ul.find(".bzoom_big_image").length){
  37. $ul.addClass('bzoom clearfix').show();
  38. var $li = $ul.children("li").addClass("bzoom_thumb"),
  39. li_len = $li.length,
  40. autoplay = _option.autoplay;
  41. $li.first().addClass("bzoom_thumb_active").show();
  42. if(li_len<2){
  43. autoplay = false;
  44. }
  45. $ul.find(".bzoom_thumb_image").css({width:_option.thumb_image_width, height:_option.thumb_image_height}).show();
  46. var scalex = _option.thumb_image_width / _option.source_image_width,
  47. scaley = _option.thumb_image_height / _option.source_image_height,
  48. scxy = _option.thumb_image_width / _option.thumb_image_height;
  49. var $bzoom_magnifier, $bzoom_magnifier_img, $bzoom_zoom_area, $bzoom_zoom_img;
  50. // 遮罩显示的区域
  51. if(!$(".bzoom_magnifier").length){
  52. $bzoom_magnifier = $('<li class="bzoom_magnifier"><div class=""><img src="" /></div></li>');
  53. $bzoom_magnifier_img = $bzoom_magnifier.find('img');
  54. $ul.append($bzoom_magnifier);
  55. $bzoom_magnifier.css({top:top, left:left});
  56. $bzoom_magnifier_img.attr('src', $ul.find('.bzoom_thumb_active .bzoom_thumb_image').attr('src')).css({width: _option.thumb_image_width, height: _option.thumb_image_height});
  57. $bzoom_magnifier.find('div').css({width:_option.thumb_image_width*scalex, height:_option.thumb_image_height*scaley});
  58. }
  59. // 大图
  60. if(!$('.bzoom_zoom_area').length){
  61. $bzoom_zoom_area = $('<li class="bzoom_zoom_area"><div><img class="bzoom_zoom_img" /></div></li>');
  62. $bzoom_zoom_img = $bzoom_zoom_area.find('.bzoom_zoom_img');
  63. var top = 0,
  64. left = 0;
  65. $ul.append($bzoom_zoom_area);
  66. if(_option.align=="left"){
  67. top = 0;
  68. left = 0 + _option.thumb_image_width + _option.zoom_area_distance;
  69. }
  70. $bzoom_zoom_area.css({top:top, left:left});
  71. $bzoom_zoom_img.css({width: _option.source_image_width, height: _option.source_image_height});
  72. }
  73. var autoPlay = {
  74. autotime : null,
  75. isplay : autoplay,
  76. start : function(){
  77. if(this.isplay && !this.autotime){
  78. this.autotime = setInterval(function(){
  79. var index = $ul.find('.bzoom_thumb_active').index();
  80. changeLi((index+1)%_option.small_thumbs);
  81. }, _option.autoplay_interval);
  82. }
  83. },
  84. stop : function(){
  85. clearInterval(this.autotime);
  86. this.autotime = null;
  87. },
  88. restart : function(){
  89. this.stop();
  90. this.start();
  91. }
  92. }
  93. // 循环小图
  94. var $small = '';
  95. if(!$(".bzoom_small_thumbs").length){
  96. var top = _option.thumb_image_height+10,
  97. width = _option.thumb_image_width,
  98. smwidth = (_option.thumb_image_width / _option.small_thumbs) - 10,
  99. smheight = smwidth / scxy,
  100. ulwidth =
  101. smurl = '',
  102. html = '';
  103. for(var i=0; i<_option.small_thumbs; i++){
  104. smurl = $li.eq(i).find('.bzoom_thumb_image').attr("src");
  105. if(i==0){
  106. html += '<li class="bzoom_smallthumb_active"><img src="'+smurl+'" alt="small" style="width:'+smwidth+'px; height:'+smheight+'px;" /></li>';
  107. }else{
  108. html += '<li style="opacity:0.4;"><img src="'+smurl+'" alt="small" style="width:'+smwidth+'px; height:'+smheight+'px;" /></li>';
  109. }
  110. }
  111. $small = $('<li class="bzoom_small_thumbs" style="top:'+top+'px; width:'+width+'px;"><ul class="clearfix" style="width: 485px;">'+html+'</ul></li>');
  112. $ul.append($small);
  113. $small.delegate("li", "click", function(event){
  114. changeLi($(this).index());
  115. autoPlay.restart();
  116. });
  117. autoPlay.start();
  118. }
  119. function changeLi(index){
  120. $ul.find('.bzoom_thumb_active').removeClass('bzoom_thumb_active').stop().animate({opacity: 0}, _option.speed, function() {
  121. $(this).hide();
  122. });
  123. $small.find('.bzoom_smallthumb_active').removeClass('bzoom_smallthumb_active').stop().animate({opacity: _option.smallthumb_inactive_opacity}, _option.speed);
  124. $li.eq(index).addClass('bzoom_thumb_active').show().stop().css({opacity: 0}).animate({opacity: 1}, _option.speed);
  125. $small.find('li:eq('+index+')').addClass('bzoom_smallthumb_active').show().stop().css({opacity: _option.smallthumb_inactive_opacity}).animate({opacity: 1}, _option.speed);
  126. $bzoom_magnifier_img.attr("src", $li.eq(index).find('.bzoom_thumb_image').attr("src"));
  127. }
  128. _option.zoom_area_height = _option.zoom_area_width / scxy;
  129. $bzoom_zoom_area.find('div').css({width:_option.zoom_area_width, height:_option.zoom_area_height});
  130. $li.add($bzoom_magnifier).mousemove(function(event){
  131. var xpos = event.pageX - $ul.offset().left,
  132. ypos = event.pageY - $ul.offset().top,
  133. magwidth = _option.thumb_image_width*scalex,
  134. magheight = _option.thumb_image_height*scalex,
  135. magx = 0,
  136. magy = 0,
  137. bigposx = 0,
  138. bigposy = 0;
  139. if(xpos < _option.thumb_image_width/2){
  140. magx = xpos > magwidth/2 ? xpos-magwidth/2 : 0;
  141. }else{
  142. magx = xpos+magwidth/2 > _option.thumb_image_width ? _option.thumb_image_width-magwidth : xpos-magwidth/2;
  143. }
  144. if(ypos < _option.thumb_image_height/2){
  145. magy = ypos > magheight/2 ? ypos-magheight/2 : 0;
  146. }else{
  147. magy = ypos+magheight/2 > _option.thumb_image_height ? _option.thumb_image_height-magheight : ypos-magheight/2;
  148. }
  149. bigposx = magx / scalex;
  150. bigposy = magy / scaley;
  151. $bzoom_magnifier.css({'left':magx, 'top':magy});
  152. $bzoom_magnifier_img.css({'left':-magx, 'top': -magy});
  153. $bzoom_zoom_img.css({'left': -bigposx, 'top': -bigposy});
  154. }).mouseenter(function(event){
  155. autoPlay.stop();
  156. $bzoom_zoom_img.attr("src", $(this).find('.bzoom_big_image').attr('src'));
  157. $bzoom_zoom_area.css({"background-image":"none"}).stop().fadeIn(400);
  158. $ul.find('.bzoom_thumb_active').stop().animate({'opacity':0.5}, _option.speed*0.7);
  159. $bzoom_magnifier.stop().animate({'opacity':1}, _option.speed*0.7).show();
  160. }).mouseleave(function(event){
  161. $bzoom_zoom_area.stop().fadeOut(400);
  162. $ul.find('.bzoom_thumb_active').stop().animate({'opacity':1}, _option.speed*0.7);
  163. $bzoom_magnifier.stop().animate({'opacity':0}, _option.speed*0.7, function(){
  164. $(this).hide();
  165. });
  166. autoPlay.start();
  167. })
  168. }
  169. }
  170. })(jQuery);