图片放大镜的实现

news/2024/7/19 14:42:52 标签: 放大镜, 图片, js

图片放大镜的实现(支持旋转)

功能介绍:

显示效果:
发大镜
旋转放大

场景:

图片查看一般引入了插件(可旋转/翻转等),blowup.js文件需要获取旋转参数值,所以需要修改我们原本有的旋转插件js

// blowup.js
$(function ($) {

    $.fn.blowup = function (attributes) {

        var $element = this;

        // If the target element is not an image
        if (!$element.is("img")) {
            console.log("%c Blowup.js Error: " + "%cTarget element is not an image.",
                "background: #FCEBB6; color: #F07818; font-size: 17px; font-weight: bold;",
                "background: #FCEBB6; color: #F07818; font-size: 17px;");
            return;
        }

        // Constants
        var $IMAGE_URL    = $element.attr("src");
        var $IMAGE_WIDTH  = $element.width();
        var $IMAGE_HEIGHT = $element.height();
        var NATIVE_IMG    = new Image();
        NATIVE_IMG.src    = $element.attr("src");

        // Default attributes
        var defaults = {
            round      : true,
            width      : 200,
            height     : 200,
            background : "#FFF",
            shadow     : "0 8px 17px 0 rgba(0, 0, 0, 0.2)",
            border     : "6px solid #FFF",
            cursor     : true,
            zIndex     : 999999
        }

        // Update defaults with custom attributes
        var $options = $.extend(defaults, attributes);

        // Modify target image
        $element.on('dragstart', function (e) { e.preventDefault(); });
        $element.css("cursor", $options.cursor ? "crosshair" : "none");

        // Create magnification lens element
        var lens = document.createElement("div");
        lens.id = "BlowupLens";

        // Attack the element to the body
        $("body").append(lens);

        // Updates styles
        $blowupLens = $("#BlowupLens");
        $blowupLens.css("cssText",(
            "position:absolute;" +
            "visibility:hidden;" +
            "pointer-events:none;"+
            "width:"+$options.width+"px;" +
            "height:"+$options.height+"px;" +
            "border:"+$options.border +
            ";background:"+$options.background +
            ";border-radius:50%!important;" +
            "box-shadow:"+$options.shadow+
            ";background-repeat:no-repeat;"
        ))

        // Show magnification lens
        $element.mouseenter(function () {
            $blowupLens.css("visibility", "visible");
        })

        // Mouse motion on image
        $element.mousemove(function (e) {
            var isOriginal = Math.abs($element.width() - NATIVE_IMG.width) < 10 ? true : false;
            var zoomeTimes = isOriginal ? 1.5 : 1;

            // Lens position coordinates
            var lensX = e.pageX - $options.width / 2;
            var lensY = e.pageY - $options.height / 2;

            // Relative coordinates of image
            // var relX = e.pageX - this.offsetLeft;
            // var relY = e.pageY - this.offsetTop;
            var relX = e.offsetX;//修改的
            var relY = e.offsetY;//修改的

            // Zoomed image coordinates
            var zoomX = -Math.floor(relX / $element.width() * NATIVE_IMG.width * zoomeTimes - $options.width / 2);
            var zoomY = -Math.floor(relY / $element.height() * NATIVE_IMG.height * zoomeTimes - $options.height / 2);

            // 图片旋转参数值
            var rotateScale = $("#rotateScale").val();
            var bgSize =  NATIVE_IMG.width * zoomeTimes;
            console.log(rotateScale, zoomeTimes, $element.width(), NATIVE_IMG.width)

            // Apply styles to lens
            $blowupLens.css({
                left                  : lensX,
                top                   : lensY,
                "background-image"    : "url(" + $IMAGE_URL + ")",
                "background-position" : zoomX + "px" + " " + zoomY + "px",
                "transform" : rotateScale,
                "background-size"      : bgSize + "px",
                "zIndex" : $options.zIndex
            });
            // $blowupLens.css("cssText", ";border-radius:50%!important;");

        })

        // Hide magnification lens
        $element.mouseleave(function () {
            $blowupLens.css("visibility", "hidden");
        })

    }
})

修改:(以下为示例代码)

  • blowup初始化,即标记哪些图片需要放大效果

    // 图片class添加imgZoom属性
    toggleClass("imgZoom", true);
    // 放大镜初始化
    $(".imgZoom").blowup({
    	background : "#FCEBB6"
    })
    
  • 基于旋转插件,点击图片,弹出旋转插件,图片加载时,记录旋转参数并初始化

    this.$canvas.append('<input id="rotateScale" type="hidden" value="rotate(0deg) scale(1, 1);">')
    
  • 图片旋转时,同步参数

    $("#rotateScale").val(transforms.join(' '));
    
  • 旋转插件关闭时,清楚放大镜div

    $("#BlowupLens").remove();
    

http://www.niftyadmin.cn/n/1307143.html

相关文章

理解static的加载顺序

理解static的加载顺序 测试类&#xff1a; public class StaticDemo {/*** 静态变量/静态代码块 -> 非静态变量/非静态代码块 -> 构造方法*/public static int k 0;public static StaticDemo t1 new StaticDemo("t1");public static StaticDemo t2 new St…

Linux命令学习-less/more/tail/head

Linux命令学习-less/more/tail/head less从后向前查看日志 less logs/catalina.out q 退出less命令 g 移动到第一行 G 移动到最后一行 ctrl B 查看向上一屏; ctrl U 半屏 ctrl F 查看向下一屏; ctrl D 半屏 j 查看向下一行 k 查看向上一行 more more 20 file 第…

设计模式-多重if的应用

设计模式-多重if的应用 文章目录设计模式-多重if的应用多重if代码示例改进代码参考资料策略模式改进多重if结构 多重if代码示例 /*** 支付方式** author admin*/ enum PayEnum {/*** 支付宝*/ALI_PAY("ali", "支付宝支付"),/*** 微信*/WECHAT_PAY("w…

Flex样式-HRule篇

HRule {strokeColor: #00ff66;shadowColor: #ff0000;strokeWidth: 20; }

Flex样式-VRule篇

VRule {strokeColor: #00ffff;shadowColor: #ffff00;strokeWidth: 20; }

dojo之日期DateTextBox篇

1、设计思路 &#xff08;1&#xff09;利用dojo的DateTextBox设计出四组日期格式以及选择日期类型&#xff1b; &#xff08;2&#xff09;控制日期的格式&#xff0c;分别显示年月日、年月和年份三种&#xff0c;另外一种是利用RadioButton选择日期格式 2、设计步骤 第一步…

Flex样式-HScrollBar篇

HScrollBar {cornerRadius: 10;highlightAlphas: 0.44, 0.44;fillAlphas: 0.45, 0.73, 0.63, 0.65;fillColors: #cc3300, #6666cc, #ccff00, #ffff00;trackColors: #00cc66, #cc00cc;themeColor: #cc9900;borderColor: #00ff00;iconColor: #330000;thumbOffset: 1; }

Flex样式-VScrollBar篇

VScrollBar {cornerRadius: 7;highlightAlphas: 0.4, 0.44;fillAlphas: 0.75, 0.49, 0.75, 0.65;fillColors: #999900, #cc0000, #6600ff, #33cc66;trackColors: #00cc66, #660099;themeColor: #3399ff;borderColor: #cc0000;iconColor: #330000;thumbOffset: 1; }