使用jqzoom插件实现图片放大镜效果

一时失言乱红尘 2021-09-12 08:42 809阅读 0赞

jqzoom插件实现图片放大镜效果。

1333548315_2346.png

图1.1jqzoom插件实现图片放大镜效果

1、引入jqurty和jqzoom插件

  1. <script src="/js/common/jquery-1.6.2.js" type="text/javascript"></script>
  2. <script src="/js/common/jquery.jqzoom.js" type="text/javascript"></script>

2、应用官方网站给定的样式

  1. /*jQzoom*/
  2. .jqzoom{
  3. border:1px solid #BBB;
  4. float:left;
  5. position:relative;
  6. padding:0px;
  7. cursor:pointer;
  8. }
  9. div.zoomdiv {
  10. z-index: 999;
  11. position : absolute;
  12. top:0px;
  13. left:0px;
  14. width : 200px;
  15. height : 200px;
  16. background: #ffffff;
  17. border:1px solid #CCCCCC;
  18. display:none;
  19. text-align: center;
  20. overflow: hidden;
  21. }
  22. div.jqZoomPup {
  23. z-index : 999;
  24. visibility : hidden;
  25. position : absolute;
  26. top:0px;
  27. left:0px;
  28. width : 50px;
  29. height : 50px;
  30. border: 1px solid #aaa;
  31. background: #ffffff url(/images/shopping/zoomlens.gif) 50% top no-repeat;
  32. opacity: 0.5;
  33. -moz-opacity: 0.5;
  34. -khtml-opacity: 0.5;
  35. filter: alpha(Opacity=50);
  36. }

3、编写HTML代码

  1. <div class="jqzoom">
  2. <img src="/images/shopping/pro_img/blue_one_small.jpg" style="width:300px; height:300px;" alt="" jqimg="/images/shopping/pro_img/blue_one_big.jpg" id="bigImg"/>
  3. </div>

其中,在HTML代码中添加该插件自定义的jqimg属性,值为大图的文件路径。

4、查看官方网站的API使用说明,可以写出如下JS代码

  1. /*使用jqzoom*/
  2. $(function() {
  3. $(".jqzoom").jqueryzoom({
  4. xzoom: 300, //放大图的宽度(默认是 200)
  5. yzoom: 300, //放大图的高度(默认是 200)
  6. offset: 10, //离原图的距离(默认是 10)
  7. position: "right", //放大图的定位(默认是 "right")
  8. preload: 1
  9. });
  10. });

* 附件

附件1:jquery.jqzoom.js

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

附件2:放大镜图标(zoomlens.gif)

1333549036_8494.gif

发表评论

表情:
评论列表 (有 0 条评论,809人围观)

还没有评论,来说两句吧...

相关阅读