imgZoom.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function zoom(mask, bigimg, smallimg) {
  2. this.bigimg = bigimg;
  3. this.smallimg = smallimg;
  4. this.mask = mask
  5. }
  6. zoom.prototype = {
  7. init: function() {
  8. var that = this;
  9. this.smallimgClick();
  10. this.maskClick();
  11. this.mouseWheel()
  12. },
  13. smallimgClick: function() {
  14. var that = this;
  15. $("." + that.smallimg).click(function() {
  16. $("." + that.bigimg).css({
  17. height: $("." + that.smallimg).height() * 1.5,
  18. width: $("." + that.smallimg).width() * 1.5
  19. });
  20. $("." + that.mask).fadeIn();
  21. $("." + that.bigimg).attr("src", $(this).attr("src")).fadeIn()
  22. })
  23. },
  24. maskClick: function() {
  25. var that = this;
  26. $("." + that.mask).click(function() {
  27. $("." + that.bigimg).fadeOut();
  28. $("." + that.mask).fadeOut()
  29. })
  30. },
  31. mouseWheel: function() {
  32. function mousewheel(obj, upfun, downfun) {
  33. if (document.attachEvent) {
  34. obj.attachEvent("onmousewheel", scrollFn)
  35. } else {
  36. if (document.addEventListener) {
  37. obj.addEventListener("mousewheel", scrollFn, false);
  38. obj.addEventListener("DOMMouseScroll", scrollFn, false)
  39. }
  40. }
  41. function scrollFn(e) {
  42. var ev = e || window.event;
  43. var dir = ev.wheelDelta || ev.detail;
  44. if (ev.preventDefault) {
  45. ev.preventDefault()
  46. } else {
  47. ev.returnValue = false
  48. }
  49. if (dir == -3 || dir == 120) {
  50. upfun()
  51. } else {
  52. downfun()
  53. }
  54. }
  55. }
  56. var that = this;
  57. mousewheel($("." + that.bigimg)[0], function() {
  58. if ($("." + that.bigimg).innerWidth() > $("body").width() - 20) {
  59. alert("不能再放大了");
  60. return
  61. }
  62. if ($("." + that.bigimg).innerHeight() > $("body").height() - 50) {
  63. alert("不能再放大");
  64. return
  65. }
  66. var zoomHeight = $("." + that.bigimg).innerHeight() * 1.03;
  67. var zoomWidth = $("." + that.bigimg).innerWidth() * 1.03;
  68. $("." + that.bigimg).css({
  69. height: zoomHeight + "px",
  70. width: zoomWidth + "px"
  71. })
  72. }, function() {
  73. if ($("." + that.bigimg).innerWidth() < 100) {
  74. alert("不能再缩小了哦!");
  75. return
  76. }
  77. if ($("." + that.bigimg).innerHeight() < 100) {
  78. alert("不能再缩小了哦!");
  79. return
  80. }
  81. var zoomHeight = $("." + that.bigimg).innerHeight() / 1.03;
  82. var zoomWidth = $("." + that.bigimg).innerWidth() / 1.03;
  83. $("." + that.bigimg).css({
  84. height: zoomHeight + "px",
  85. width: zoomWidth + "px"
  86. })
  87. })
  88. }
  89. };