function zoom(mask, bigimg, smallimg) { this.bigimg = bigimg; this.smallimg = smallimg; this.mask = mask } zoom.prototype = { init: function() { var that = this; this.smallimgclick(); this.maskclick(); this.mousewheel() }, smallimgclick: function() { var that = this; $("." + that.smallimg).click(function() { $("." + that.bigimg).css({ width: $("." + that.smallimg).width() * 2 }); $("." + that.mask).fadein(); $("." + that.bigimg).attr("src", $(this).attr("src")).fadein() }) }, maskclick: function() { var that = this; $("." + that.mask).click(function() { $("." + that.bigimg).fadeout(); $("." + that.mask).fadeout() }) }, mousewheel: function() { function mousewheel(obj, upfun, downfun) { if (document.attachevent) { obj.attachevent("onmousewheel", scrollfn) } else { if (document.addeventlistener) { obj.addeventlistener("mousewheel", scrollfn, false); obj.addeventlistener("dommousescroll", scrollfn, false) } } function scrollfn(e) { var ev = e || window.event; var dir = ev.wheeldelta || ev.detail; if (ev.preventdefault) { ev.preventdefault() } else { ev.returnvalue = false } if (dir == -3 || dir == 120) { upfun() } else { downfun() } } } var that = this; mousewheel($("." + that.bigimg)[0], function() { if ($("." + that.bigimg).innerwidth() > $("body").width() - 20) { alert("不能再放大了"); return } if ($("." + that.bigimg).innerheight() > $("body").height() - 50) { alert("不能再放大"); return } var zoomheight = $("." + that.bigimg).innerheight() * 1.03; var zoomwidth = $("." + that.bigimg).innerwidth() * 1.03; $("." + that.bigimg).css({ height: zoomheight + "px", width: zoomwidth + "px" }) }, function() { if ($("." + that.bigimg).innerwidth() < 100) { alert("不能再缩小了哦!"); return } if ($("." + that.bigimg).innerheight() < 100) { alert("不能再缩小了哦!"); return } var zoomheight = $("." + that.bigimg).innerheight() / 1.03; var zoomwidth = $("." + that.bigimg).innerwidth() / 1.03; $("." + that.bigimg).css({ height: zoomheight + "px", width: zoomwidth + "px" }) }) } };