谁有类似淘宝的放大镜功能

2016-07-07 16:23 来源:www.chinab4c.com 作者:ecshop专家

淘宝的很简单的,谁知道的么?

d.jpg

我在网上找到一个代码段,JS的,谁能帮我看下,是不是这个,如果是的话怎么使用?


  1. <script>
  2. if(typeof(qsoft) == "undefined")
  3. qsoft = {};

  4. qsoft.PopBigImage = function (origImageId,dx,dy)
  5. {
  6. this.oim = document.getElementById(origImageId);

  7. this.oim.style.cursor = "crosshair";

  8. this.ow = this.oim.width;
  9. this.oh = this.oim.height;

  10. this.detaX = dx?dx : 30;
  11. this.detaY = dy?dy : 0;

  12. this.getAbsSize = function (obj)
  13. {
  14. return obj.getBoundingClientRect();
  15. };

  16. var rect = this.getAbsSize(this.oim);
  17. this.ol = rect.left + this.detaX + this.ow;
  18. this.ot = rect.top + this.detaY;

  19. this.src = this.oim.src;

  20. this.getImageSize = function (img)
  21. {
  22. var im = new Image();
  23. im.src = img.src;

  24. var size = {};
  25. size.width = im.width;
  26. size.height = im.height;

  27. im = null;
  28. delete im;

  29. return size;
  30. };

  31. var rsize = this.getImageSize(this.oim);
  32. this.w = rsize.width;
  33. this.h = rsize.height;

  34. var qObj = this;

  35. this.createMask = function ()
  36. {
  37. if(typeof(this.mask) == "undefined")
  38. {
  39. this.mask = document.createElement("div");
  40. this.mask.id = this.oim.id + "_mask";

  41. this.mask.style.position= "absolute";
  42. this.mask.style.width = this.ow;
  43. this.mask.style.height = this.oh;
  44. this.mask.style.left = this.ol;
  45. this.mask.style.top = this.ot;
  46. this.mask.style.backgroundImage= "url("+this.src+")";
  47. this.mask.style.backgroundRepeat = "no-repeat";
  48. this.mask.style.display ="none";

  49. document.body.appendChild(this.mask);

  50. //this.mask.style = "position: absolute; ";//left: "+this.ol+"; top: "+this.ot+";width: " + this.ow + "; height: " + this.oh + ";
  51. ///*display: none;*/ background: url(" + this.src + ") left top no-repeat;";
  52. }
  53. };

  54. this.regEvent = function ()
  55. {
  56. this.oim.onmousemove = function ()
  57. {
  58. var x = Math.ceil(event.offsetX * qObj.w/qObj.ow) - qObj.ow/2;
  59. var y = Math.ceil(event.offsetY * qObj.h/qObj.oh) - qObj.oh/2;

  60. if(x<0) x = 0;
  61. if(y<0) y = 0;

  62. var maxx = Math.ceil((qObj.w-qObj.ow));
  63. var maxy = Math.ceil((qObj.h-qObj.oh));

  64. if(x>maxx) x = maxx;
  65. if(y>maxy) y = maxy;

  66. qObj.mask.style.backgroundPositionX = -x;
  67. qObj.mask.style.backgroundPositionY = -y;
  68. };

  69. this.oim.onmouseout = function (e)
  70. {
  71. qObj.mask.style.display = "none";
  72. };

  73. this.oim.onmouseenter = function (e)
  74. {
  75. qObj.mask.style.display = "block";
  76. };

  77. };

  78. this.render = function ()
  79. {
  80. this.createMask();
  81. this.regEvent();
  82. };
  83. };

  84. qsoft.version = 0.1;
  85. window.onload = function (){
  86. var pbi = new qsoft.PopBigImage("orig",24,-2);
  87. pbi.render();
  88. }

  89. </script>
  90. <img src="images/200905/source_img/1226569902610916918.jpg" id="orig" alt="天子星" width="250" height="250" style="margin-top:60px;" />
复制代码

回答:
自己沙发先