网页上图片点击放大js代码

//图片弹出事件
function showPict(path) {
    src = path;
    var mask =
            "<div style = 'position: absolute;width: 100%;z-index: 5555;height: 100%;top: 0;left: 0;background: rgba(0,0,0,0.5);display:none;' id='imgZoomMask'></div>";
    $("html").append(mask);
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    $(window).resize(function () {
        windowWidth = $(window).width();
        windowHeight = $(window).height();
    });
    $("#imgZoomMask").show();

    $(window).resize(function () {
        windowWidth = $(window).width();
        windowHeight = $(window).height();
    });
    var img = new Image();
    img.src = path;
    img.onload = function () {
        var dom = "";
        var displayWidth = 0;
        var displayHeight = 0;
        var style = "";
        if (img.width > img.height) {
            displayWidth = windowWidth / 2;
            displayHeight = img.height * displayWidth / img.width;
            style = "z-index:6666;position:absolute;top:" +
                windowHeight / 2 +
                "px;margin-top:-" +
                displayHeight / 2 +
                "px;left:" +
                windowWidth / 2 +
                "px;margin-left:-" +
                displayWidth / 2 +
                "px;cursor:pointer;";
            dom = "<img draggable='true' src = '" +
                src +
                "' width = '50%' style='" +
                style +
                "' id='imgZoomImg'>";
        } else {
            displayHeight = windowHeight / 2;
            displayWidth = displayHeight * img.width / img.height;
            style = "z-index:6666;position:absolute;top:" +
                windowHeight / 2 +
                "px;margin-top:-" +
                displayHeight / 2 +
                "px;left:" +
                windowWidth / 2 +
                "px;margin-left:-" +
                displayWidth / 2 +
                "px;cursor:pointer;";
            dom = "<img draggable='true' src = '" +
                src +
                "' height = '50%' style=' " +
                style +
                "' id='imgZoomImg'>";
        }
        $("body").append(dom);
        $("#imgZoomImg").dragging({
            move: "both", //拖动方向,x y both
            randomPosition: false //初始位置是否随机
        });

    }
    $(document).on("click", "#imgZoomMask", function () {
        $("#imgZoomMask").hide();
        $("#imgZoomImg").fadeOut().remove();
    });
    $(document).on("mousewheel", function (e, d) {
        //d 1 上 -1 下
        if (d === 1) {
            var width = $("#imgZoomImg").width();
            var height = $("#imgZoomImg").height();
            $("#imgZoomImg").css({ "width": width * 1.2, "height": height * 1.2 });
        }
        if (d === -1) {
            var width = $("#imgZoomImg").width();
            var height = $("#imgZoomImg").height();
            $("#imgZoomImg").css({ "width": width / 1.2, "height": height / 1.2 });
        }
    });
}

 

转载于:https://www.cnblogs.com/llljpf/p/7281600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值