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;
}

记录下

原文地址:https://www.cnblogs.com/xiami2104/p/11826712.html

时间: 2024-07-31 22:07:55

jquey实现放大镜功能的相关文章

javascript 实现图片放大镜功能

淘宝上经常用到的一个功能是利用图片的放大镜功能来查看商品的细节 下面我们来实现这样一个功能吧,原理很简单: 实现一个可以随鼠标移动的虚框 在另外一个块中对应显示虚框中的内容 实现思路: 虚框用css中的透明度实现filter:alpha(opacity:20); opacity:0.2; 鼠标移动到小图上面时:虚框出现,大图对应出现 细节注意的地方: 1:虚框的定位:保持鼠标位于虚框的中心,如何处理鼠标中心距离四边距离小于虚框半径(或者方形的边长的一半)的情况? 2:保持大图中显示的内容是虚框选

用JS做一个简单的电商产品放大镜功能

使用js制作一个简单的产品放大图 购物网站的产品页经常会放有一个产品展示图区.该图区有一个功能就是产品图的放大功能,移动左侧的焦点区域,可以放大细节部分观看,详情如下图.实现该功能的方法也非常简单. 实验:制作产品焦点放大图. 所需技能:1.基本的获取页面元素的方法: 2.几个简单的事件: 3.会使用dom设置元素的属性: 案例原理:1.焦点框的跟随鼠标事件: 2.焦点框的移动区域规定: 3.大盒子内容的显示: 适合对象:js初学者 -------------------------------

图片放大镜功能

HTML: <div class="pr_hLeft"> <div class="imgLeft"> <!--中号图--> <div class="Medium"> <!-- 放大镜 --> <div class="magnifier" id="magnifier"> <img src="http://product.d

放大镜功能

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <style>        * {            margin: 0;            padding: 0;        } .box {            width: 350px;   

电商网站的放大镜功能

首先是样式部分,分别定义一大一小两个盒子,用来盛放大图片和小图片. <style > *{ padding: 0; margin: 0; } #demo { display: block; width: 400px; height: 255px; margin: 50px; position: relative; border: 1px solid #ccc; } #small_box { position: absolute; z-index: 1; } #mark { position:

c# winfrom 放大镜功能

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Threading; 10 11 namespace test 12

B2C电子商务系统研发——产品媒体常见功能点

产品媒体常见功能点 电商研发系列——产品媒体常见功能点 支持图片.视频和文档等媒体类型 产品图片对清晰度要求比极高,但又不能太大,所以图片一般是jpg格式. 视频一般是flv流媒体格式,如果是嵌入产品详细描述的视频,可以是优酷.youTube等视频网站的视频. 文档一般是pdf格式,用途多是帮助文档. 需要支持多张图,其中一张为主图 支持生成多种尺寸的图片 在不同的页面需要不同的尺寸,而这不能通过css直接设置高度宽度来控制,因为这样将导致整个页面太大, 降低页面访问速度,用户体验会很差.所以需

preview放大镜

[preview放大镜] preview有放大镜功能,放`键即可(键盘左上角,1左边的键). preview放大镜,布布扣,bubuko.com

利用JS实现购物网站商品放大镜效果

大家几乎都有在网上购物的经验,有的网站会有一个商品放大镜功能, 就是把鼠标移到图片上的时候,旁边会有另外一张大的图片展示,等同于 放大镜效果,那这样的效果怎样实现的呢,我把代码发给大家,请大家参考. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>放大镜</title> <style> *{m