jquey实现放大镜功能
Posted xiami2104
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquey实现放大镜功能相关的知识,希望对你有一定的参考价值。
(function ($) { $.fn.imagezoom = function (options) { var settings = { xzoom: 220, yzoom: 220, offset: 10, position: "BTR", preload: 1, scale:1 }; if (options) { $.extend(settings, options); } var noalt = ‘‘,iTop,iLeft,iWidth,iHeight; var self = this; return self.each(function () { self.imagezoom = true; var _this = this; var explorer = navigator.userAgent; var mouseAdd = "mousewheel"; if (explorer.indexOf("Firefox") >= 0) { mouseAdd = "DOMMouseScroll"; } $("body").off(mouseAdd).on(mouseAdd, "div.zoomMask", function (ev) { var $this = $(this); var imgWid = $("div.zoomDiv>img").width(); var zoom = ev.originalEvent.deltaY || (ev.originalEvent.detail > 0 ? ev.originalEvent.detail + 97 : ev.originalEvent.detail-97); if ($this.width() > iWidth && zoom > 0) { return; } $("div.zoomDiv>img").css({ width: imgWid - zoom, height: "auto" }); var mouseWheel = { x: ev.originalEvent.clientX, y: ev.originalEvent.clientY }; var bigwidth = $(".bigimg").get(0).offsetWidth, bigheight = $(".bigimg").get(0).offsetHeight, scalex = (bigwidth / iWidth), scaley = (bigheight / iHeight), xpos = mouse.x - $this.width() / 2, ypos = mouse.y - $this.height() / 2; $this.width((settings.xzoom) / scalex); $this.height((settings.yzoom) / scaley); xpos = (mouseWheel.x - $this.width() / 2 < iLeft) ? iLeft : (mouseWheel.x + $this.width() / 2 > iWidth + iLeft) ? (iWidth + iLeft - $this.width()) : xpos; ypos = (mouseWheel.y - $this.height() / 2 < iTop) ? iTop : (mouseWheel.y + $this.height() / 2 > iHeight + iTop) ? (iHeight + iTop - $this.height()) : ypos; $this.css({ top: ypos + $(document).scrollTop(), left: xpos }); var xposs = mouseWheel.x - $this.width() / 2 - iLeft, yposs = mouseWheel.y - $this.height() / 2 - iTop; $("div.zoomDiv").scrollLeft(xposs * scalex); $("div.zoomDiv").scrollTop(yposs * scaley); //$("div.zoomDiv>img").css({ left: "-" + xposs * scalex + "px", top: "-" + yposs * scaley + "px" }); ev.preventDefault(); }); $(this).on("mouseover", function (ev) { var imageLeft = $(this).offset().left; var imageTop = $(this).offset().top - $(document).scrollTop(); var imageWidth = $(this).get(0).offsetWidth; var imageHeight = $(this).get(0).offsetHeight; iTop = imageTop, iLeft = imageLeft, iWidth = imageWidth, iHeight = imageHeight; var boxLeft = $(this).parent().offset().left; var boxTop = $(this).parent().offset().top; var boxWidth = $(this).parent().width(); var boxHeight = $(this).parent().height(); noalt = $(this).attr("alt"); var bigimage = $(this).attr("rel"); $(this).attr("alt", ‘‘); var Img = new Image(); Img.src = $(this).attr("src"); Img.onload = function () { $("div.zoomDiv>img").css({ width: this.width * settings.scale, height: this.height * settings.scale }); }; if ($("div.zoomDiv").length == 0) { $(document.body).append("<div class=‘zoomDiv‘ style=‘border-radius:25px;‘><img style=‘position:absolute;top:0;left:0;‘ class=‘bigimg‘ src=‘" + bigimage + "‘ onerror=‘citms.nofind();‘ /></div><div class=‘zoomMask‘> </div>");//放大镜框及遮罩 } else { $("div.zoomDiv>img").attr("src", $(this).attr("src")); } $("div.zoomDiv").width(settings.xzoom); $("div.zoomDiv").height(settings.yzoom); $("div.zoomDiv").show(); $("div.zoomMask").show(); $(this).css(‘cursor‘, ‘crosshair‘); $(document.body).off("mousemove").on("mousemove", function (e) { if ($("div.zoomDiv").length == 0) { $(document.body).append("<div class=‘zoomDiv‘ style=‘border-radius:25px;‘><img style=‘position:absolute;top:0;left:0;‘ class=‘bigimg‘ src=‘" + bigimage + "‘ onerror=‘citms.nofind();‘ /></div><div class=‘zoomMask‘> </div>");//放大镜框及遮罩 } var ePageX = e.pageX + 10, ePageY = e.pageY + 10, ceiTop = 0; if (settings.position == "BTR") { leftpos = $("div.zoomMask").position().left + $("div.zoomMask").width() + 10; ceiTop = $("div.zoomMask").position().top; if ($(document).find(".layout-updown").length == 0) { if (ePageY + settings.yzoom + 10 > $(document.body).height()) { ceiTop = ePageY - settings.yzoom - 10 } } else { if (ePageY + settings.yzoom + 10 > $(".punishEditLeft").height()) { ceiTop = ePageY - settings.yzoom - 10 } } if (ePageX + settings.xzoom + 10 > $(document.body).width() - 300) { leftpos = ePageX - settings.xzoom - 20 - ($("div.zoomMask").width() / 2) } if ((ePageX - settings.xzoom) / 2 < $("div.zoomMask").width() / 2) { leftpos = ePageX + ($("div.zoomMask").width() / 2) ; if (ePageX < $("div.zoomMask").width() / 2) { leftpos = $("div.zoomMask").width() + 10; } } if ($(document.body).width() - ePageX < ($("div.zoomMask").width() / 2)) { leftpos = $(document.body).width() - $("div.zoomMask").width() - $("div.zoomDiv").width() - 20 } } else { leftpos = imageLeft - settings.xzoom - settings.offset; if (leftpos < 0) { leftpos = imageLeft + imageWidth + settings.offset } ceiTop = boxTop } $("div.zoomDiv").css({ top: ceiTop >= 0?ceiTop:0, left: leftpos }); mouse = { x: e.originalEvent.clientX, y: e.originalEvent.clientY }; if (mouse.x < parseInt(imageLeft) || mouse.x > Math.round(imageLeft + imageWidth) || mouse.y < parseInt(imageTop) || mouse.y > Math.round(imageTop + imageHeight)) { mouseOutImage(); return; } var bigwidth = 0; var bigheight = 0; if ($(".bigimg").get(0)) { bigwidth = $(".bigimg").get(0).offsetWidth; bigheight = $(".bigimg").get(0).offsetHeight; } var scaley = ‘x‘; var scalex = ‘y‘; if (isNaN(scalex) | isNaN(scaley)) { var scalex = (bigwidth / imageWidth); var scaley = (bigheight / imageHeight); $("div.zoomMask").width((settings.xzoom) / scalex); $("div.zoomMask").height((settings.yzoom) / scaley); $("div.zoomMask").css(‘visibility‘, ‘visible‘); } xpos = mouse.x - $("div.zoomMask").width() / 2; ypos = mouse.y - $("div.zoomMask").height() / 2; xposs = mouse.x - $("div.zoomMask").width() / 2 - imageLeft; yposs = mouse.y - $("div.zoomMask").height() / 2 - imageTop; xpos = (mouse.x - $("div.zoomMask").width() / 2 < imageLeft) ? imageLeft : (mouse.x + $("div.zoomMask").width() / 2 > imageWidth + imageLeft) ? (imageWidth + imageLeft - $("div.zoomMask").width()) : xpos; ypos = (mouse.y - $("div.zoomMask").height() / 2 < imageTop) ? imageTop : (mouse.y + $("div.zoomMask").height() / 2 > imageHeight + imageTop) ? (imageHeight + imageTop - $("div.zoomMask").height()) : ypos; $("div.zoomMask").css({ top: ypos + $(document).scrollTop(), left: xpos }); $("div.zoomDiv").scrollLeft(xposs * scalex); $("div.zoomDiv").scrollTop(yposs * scaley); //$("div.zoomDiv").get(0).scrollTop = yposs * scaley; //var MaskLeft = -xposs * scalex; //console.log(-xposs * scalex + "&&&" + -yposs * scaley); //if (-xposs * scalex) //$("div.zoomDiv>img").css({ left: -xposs * scalex > 0 ? 0 : -xposs * scalex, top: -yposs * scaley > 0 ? 0 : -yposs * scaley }); }); }); }); var count = 0; function mouseOutImage() { $(self).attr("alt", noalt); $(document.body).unbind("mousemove"); $("div.zoomMask").hide(); $("div.zoomDiv").hide(); mouseImg = null; } if (settings.preload) { $(‘body‘).append("<div style=‘display:none;‘ class=‘jqPreload" + count + "‘></div>"); $(this).each(function () { var imagetopreload = $(this).attr("rel"); var content = jQuery(‘div.jqPreload‘ + count + ‘‘).html(); jQuery(‘div.jqPreload‘ + count + ‘‘).html(content + ‘<img src="‘ + imagetopreload + ‘">‘); }); } } })(jQuery); function MouseEvent(e) { this.x = e.pageX; this.y = e.pageY; }
记录下
以上是关于jquey实现放大镜功能的主要内容,如果未能解决你的问题,请参考以下文章