jquery.jqzoom.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //**************************************************************
  2. // jQZoom allows you to realize a small magnifier window,close
  3. // to the image or images on your web page easily.
  4. //
  5. // jqZoom version 2.1
  6. // Author Doc. Ing. Renzi Marco(www.mind-projects.it)
  7. // First Release on Dec 05 2007
  8. // i'm searching for a job,pick me up!!!
  9. // mail: renzi.mrc@gmail.com
  10. //**************************************************************
  11. (function($){
  12. $.fn.jqueryzoom = function(options){
  13. var settings = {
  14. xzoom: 200, //zoomed width default width
  15. yzoom: 200, //zoomed div default width
  16. offset: 10, //zoomed div default offset
  17. position: "right" ,//zoomed div default position,offset position is to the right of the image
  18. lens:1, //zooming lens over the image,by default is 1;
  19. preload: 1,
  20. defaultwidth:600,
  21. defaultheight:600
  22. };
  23. if(options) {
  24. $.extend(settings, options);
  25. }
  26. var noalt='';
  27. $(this).hover(function(){
  28. // remove parents obj within 'relative'
  29. var parents = $(this).parents();
  30. parents.each(function(){
  31. if($(this).css('position') == 'relative'){
  32. $(this).css('position', 'static');
  33. $(this).attr('nc_type', 'jqzoom_relative');
  34. }
  35. });
  36. var imageLeft = this.offsetLeft;
  37. var imageRight = this.offsetRight;
  38. var imageTop = $(this).get(0).offsetTop;
  39. var imageWidth = $(this).children('img').get(0).offsetWidth;
  40. var imageHeight = $(this).children('img').get(0).offsetHeight;
  41. noalt= $(this).children("img").attr("alt");
  42. var bigimage = $(this).children("img").attr("jqimg");
  43. $(this).children("img").attr("alt",'');
  44. if(settings.position == "right"){
  45. if(imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width){
  46. leftpos = imageLeft - settings.offset - settings.xzoom;
  47. }else{
  48. leftpos = imageLeft + imageWidth + settings.offset;
  49. }
  50. }else{
  51. leftpos = imageLeft - settings.xzoom - settings.offset;
  52. if(leftpos < 0){
  53. leftpos = imageLeft + imageWidth + settings.offset;
  54. }
  55. }
  56. $("div.zoomdiv").show();
  57. if(!settings.lens){
  58. $(this).css('cursor','crosshair');
  59. }
  60. $(document.body).mousemove(function(e){
  61. $("div.zoomdiv").css({ top: imageTop,left: leftpos });
  62. $("div.zoomdiv").width(settings.xzoom);
  63. $("div.zoomdiv").height(settings.yzoom);
  64. mouse = new MouseEvent(e);
  65. /*$("div.jqZoomPup").hide();*/
  66. var bigwidth = $(".bigimg").get(0).offsetWidth;
  67. var bigheight = $(".bigimg").get(0).offsetHeight;
  68. if( bigwidth < settings.defaultwidth || bigheight < settings.defaultheight ){
  69. $("div.jqZoomPup").hide();
  70. $("div.zoomdiv").hide();
  71. }else{
  72. $("div.jqZoomPup").show();
  73. $("div.zoomdiv").show();
  74. }
  75. var scaley ='x';
  76. var scalex= 'y';
  77. if(isNaN(scalex)|isNaN(scaley)){
  78. var scalex = (bigwidth/imageWidth);
  79. var scaley = (bigheight/imageHeight);
  80. var pupx = scalex <= 1 ? imageWidth : (settings.xzoom)/scalex;
  81. var pupy = scaley <= 1 ? imageHeight : (settings.yzoom)/scaley;
  82. $("div.jqZoomPup").width(pupx);
  83. $("div.jqZoomPup").height(pupy);
  84. if(settings.lens){
  85. $("div.jqZoomPup").css('visibility','visible');
  86. }
  87. }
  88. xpos = mouse.x - $("div.jqZoomPup").width()/2 - imageLeft;
  89. ypos = mouse.y - $("div.jqZoomPup").height()/2 - imageTop ;
  90. if(settings.lens){
  91. xpos = (mouse.x - $("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ? (imageWidth -$("div.jqZoomPup").width() -2) : xpos;
  92. ypos = (mouse.y - $("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2 > imageHeight + imageTop ) ? (imageHeight - $("div.jqZoomPup").height() -2 ) : ypos;
  93. }
  94. if(settings.lens){
  95. $("div.jqZoomPup").css({ top: ypos,left: xpos });
  96. }
  97. scrolly = ypos;
  98. $("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
  99. scrollx = xpos;
  100. $("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex ;
  101. });
  102. },function(){
  103. // assign for parents obj within 'relative'
  104. var parents = $(this).parents();
  105. parents.each(function(){
  106. if($(this).attr('nc_type') == 'jqzoom_relative'){
  107. $(this).css('position', 'relative');
  108. }
  109. });
  110. $(this).children("img").attr("alt",noalt);
  111. $(document.body).unbind("mousemove");
  112. if(settings.lens){
  113. $("div.jqZoomPup").hide();
  114. }
  115. $("div.zoomdiv").hide().css({top:'-9999em'});
  116. });
  117. count = 0;
  118. if(settings.preload){
  119. $('#content').append("<div style='display:none;' class='jqPreload"+count+"'></div>");
  120. $(this).each(function(){
  121. var imagetopreload= $(this).children("img").attr("jqimg");
  122. var content = jQuery('div.jqPreload'+count+'').html();
  123. jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');
  124. });
  125. }
  126. }
  127. })(jQuery);
  128. function MouseEvent(e) {
  129. this.x = e.pageX
  130. this.y = e.pageY
  131. }