jquery.jqzoom.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. };
  21. if(options) {
  22. $.extend(settings, options);
  23. }
  24. var noalt='';
  25. $(this).hover(function(){
  26. var imageLeft = this.offsetLeft;
  27. var imageRight = this.offsetRight;
  28. var imageTop = $(this).get(0).offsetTop;
  29. var imageWidth = $(this).children('img').get(0).offsetWidth;
  30. var imageHeight = $(this).children('img').get(0).offsetHeight;
  31. noalt= $(this).children("img").attr("alt");
  32. var bigimage = $(this).children("img").attr("jqimg");
  33. $(this).children("img").attr("alt",'');
  34. if($("div.zoomdiv").get().length == 0){
  35. $(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
  36. $(this).append("<div class='jqZoomPup'>&nbsp;</div>");
  37. }
  38. if(settings.position == "right"){
  39. if(imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width){
  40. leftpos = imageLeft - settings.offset - settings.xzoom;
  41. }else{
  42. leftpos = imageLeft + imageWidth + settings.offset;
  43. }
  44. }else{
  45. leftpos = imageLeft - settings.xzoom - settings.offset;
  46. if(leftpos < 0){
  47. leftpos = imageLeft + imageWidth + settings.offset;
  48. }
  49. }
  50. $("div.zoomdiv").css({ top: imageTop,left: leftpos });
  51. $("div.zoomdiv").width(settings.xzoom);
  52. $("div.zoomdiv").height(settings.yzoom);
  53. $("div.zoomdiv").show();
  54. if(!settings.lens){
  55. $(this).css('cursor','crosshair');
  56. }
  57. $(document.body).mousemove(function(e){
  58. mouse = new MouseEvent(e);
  59. /*$("div.jqZoomPup").hide();*/
  60. var bigwidth = $(".bigimg").get(0).offsetWidth;
  61. var bigheight = $(".bigimg").get(0).offsetHeight;
  62. var scaley ='x';
  63. var scalex= 'y';
  64. if(isNaN(scalex)|isNaN(scaley)){
  65. var scalex = (bigwidth/imageWidth);
  66. var scaley = (bigheight/imageHeight);
  67. $("div.jqZoomPup").width((settings.xzoom)/scalex );
  68. $("div.jqZoomPup").height((settings.yzoom)/scaley);
  69. if(settings.lens){
  70. $("div.jqZoomPup").css('visibility','visible');
  71. }
  72. }
  73. xpos = mouse.x - $("div.jqZoomPup").width()/2 - imageLeft;
  74. ypos = mouse.y - $("div.jqZoomPup").height()/2 - imageTop ;
  75. if(settings.lens){
  76. xpos = (mouse.x - $("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ? (imageWidth -$("div.jqZoomPup").width() -2) : xpos;
  77. ypos = (mouse.y - $("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2 > imageHeight + imageTop ) ? (imageHeight - $("div.jqZoomPup").height() -2 ) : ypos;
  78. }
  79. if(settings.lens){
  80. $("div.jqZoomPup").css({ top: ypos,left: xpos });
  81. }
  82. scrolly = ypos;
  83. $("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
  84. scrollx = xpos;
  85. $("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex ;
  86. });
  87. },function(){
  88. $(this).children("img").attr("alt",noalt);
  89. $(document.body).unbind("mousemove");
  90. if(settings.lens){
  91. $("div.jqZoomPup").remove();
  92. }
  93. $("div.zoomdiv").remove();
  94. });
  95. count = 0;
  96. if(settings.preload){
  97. $('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");
  98. $(this).each(function(){
  99. var imagetopreload= $(this).children("img").attr("jqimg");
  100. var content = jQuery('div.jqPreload'+count+'').html();
  101. jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');
  102. });
  103. }
  104. }
  105. })(jQuery);
  106. function MouseEvent(e) {
  107. this.x = e.pageX
  108. this.y = e.pageY
  109. }