弹出框移动、放大、缩小、最大化处理

本文介绍了一种利用JavaScript实现网页元素拖拽及缩放的方法,包括获取视口尺寸、实现弹窗移动和缩放功能等。适用于需要用户交互调整页面元素位置和大小的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function getViewportSize() {
    //可视区域的宽度和高度
    return {
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight
    };
}

class LayerMove {
    /**
     * 弹层移动
     * @param $setCanMoveAreaName 鼠标点击哪个区域可以移动容器
     * @param $moveContainerName 要移动的容器
     */
    static execute($setCanMoveAreaName, $moveContainerName, $iframe) {
        let viewport = getViewportSize();
        $setCanMoveAreaName.mousedown(function (e) {
            e.stopPropagation();
            e.preventDefault();
            let x = e.clientX - $moveContainerName.offset().left;
            let y = e.clientY - $moveContainerName.offset().top;
            $(`#${$iframe}`).find('iframe')[0].contentWindow.init(x, y);
            $(document).mousemove(function (e) {
                let left = e.clientX - x;
                let top = e.clientY - y;
                let currentLeft = viewport.width - $moveContainerName.width();
                let currentTop = viewport.height - $moveContainerName.height();
                left = left < 0 ? 0 : left;
                left = left > currentLeft ? currentLeft : left;
                top = top < 0 ? 0 : top;
                top = top > currentTop ? currentTop : top;
                $moveContainerName.css({left: left + "px", top: top + "px"});
            });
            $(document).mouseup(function () {
                $(this).unbind("mousemove");
                $(`#${$iframe}`).find('iframe')[0].contentWindow.removeMouseMove();
                $(document).unbind("mouseup");
            });
        });
    }
}

/**
 *
 */
class LayerZoom {

    static execute($zoomContainerName, isEnlarge) {
        if (isEnlarge) {
            $zoomContainerName.css({position: "absolute", left: 0, top: 0, width: "100%", height: "100%"});
        } else {
            $zoomContainerName.css({position: "absolute", left: "15%", top: "15%", width: "70%", height: "70%"});
        }
    }

}

class LayerHandZoom {
    static execute($setCanZoomAreaName, $moveContainerName) {
        let viewport = getViewportSize();
        $setCanZoomAreaName.mousedown(function (e) {
            let x = e.clientX - $moveContainerName.width();
            let y = e.clientY - $moveContainerName.height();
            $(document).mousemove(function (e) {
                let width = e.clientX - x + "px";
                let height = e.clientY - y + "px";
                width = width < 0 ? 0 : width;
                width = width > viewport.width ? viewport.width : width;
                height = height < 0 ? 0 : height;
                height = height > viewport.height ? viewport.height : height;
                $moveContainerName.css({width: width, height: height});
            });
            $(document).mouseup(function () {
                $(document).unbind("mousemove");
            });
        });
    }
}

https://www.nowcoder.com

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愤怒的小青春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值