html中,实现通过拖拽调整图像尺寸

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>html中拖拽修改图像尺寸</title>
<style>
  #imageContainer {
    position: relative;
    display: inline-block;
  }
  #resizeHandle {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 20px;
    height: 20px;
    cursor: nw-resize;
    background-color: #333;
    color: white;
    text-align: center;
    line-height: 20px;
  }
  img {
    display: block;
    max-width: 100%;
  }


    .zoom {
        position: absolute;
        width: 6px;
        height: 6px;
        background: #fff;
        border: 1px solid #2298ff;
        border-radius: 50%;
        display: none;
    }

        .zoom.up-left {
            margin-top: -5px;
            margin-left: -5px;
            cursor: nw-resize;
            top: 0;
        }

        .zoom.up {
            left: 50%;
            margin-left: -5px;
            top: -5px;
            cursor: n-resize;
        }

        .zoom.up-right {
            top: -5px;
            right: -5px;
            cursor: ne-resize;
        }

        .zoom.right {
            top: 50%;
            margin-top: -5px;
            right: -5px;
            cursor: e-resize;
        }

        .zoom.right-down {
            right: -5px;
            bottom: -5px;
            cursor: se-resize;
        }

        .zoom.down {
            bottom: -5px;
            left: 50%;
            margin-left: -5px;
            cursor: s-resize;
        }

        .zoom.left-down {
            left: -5px;
            bottom: -5px;
            cursor: sw-resize;
        }

        .zoom.left {
            left: -5px;
            top: 50%;
            margin-top: -5px;
            cursor: w-resize;
        }

    .active {
        border: 2px solid #f00;
    }

</style>
</head>
<body>
 
    <div id="imageContainer" class="active">
        <img src="http://localhost:51190//upload/exam/paper/20241210/b211681f38514f56ab933ea6152ad808.png?_t=20241210165544?t=10000" alt="Resizable Image">
        <!--<div id="resizeHandle">◢</div>-->

        <div class="zoom right-down" style="display: block;"></div>

        <!--<div class="zoom up-left" style="display: block;"></div>
        <div class="zoom up" style="display: block;"></div>
        <div class="zoom up-right" style="display: block;"></div>
        <div class="zoom right" style="display: block;"></div>
        <div class="zoom down" style="display: block;"></div>
        <div class="zoom left-down" style="display: block;"></div>
        <div class="zoom left" style="display: block;"></div>-->
    </div>
 
<script>
    // JavaScript to make the resize handle draggable and resize the image
    //const handle = document.getElementById('resizeHandle');

    const image = document.querySelector('#imageContainer img');
    let isResizing = false;
    let startX;
    let startY;
    let startWidth;
    let startHeight;

    //handle.addEventListener('mousedown', function(e) {
    //  isResizing = true;
    //  startX = e.clientX;
    //  startY = e.clientY;
    //  startWidth = parseInt(getComputedStyle(image).width, 10);
    //  startHeight = parseInt(getComputedStyle(image).height, 10);
    //});

    document.querySelector('.zoom').addEventListener("mousedown", function (e) {//菜单点击时,防止误拖拽
        isResizing = true;
        startX = e.clientX;
        startY = e.clientY;
        startWidth = parseInt(getComputedStyle(image).width, 10);
        startHeight = parseInt(getComputedStyle(image).height, 10);
    });
 
    document.addEventListener('mousemove', function(e) {
        if (isResizing) {
            const deltaX = e.clientX - startX;
            const deltaY = e.clientY - startY;
            image.style.width = (startWidth + deltaX) + 'px';
            image.style.height = (startHeight + deltaY) + 'px';
        }
    });
 
    document.addEventListener('mouseup', function() {
        isResizing = false;
    });
</script>
 
</body>
</html>
相关推荐
tanxiaomi13 小时前
通过HTML演示JVM的垃圾回收-新生代与老年代
前端·jvm·html
少年阿闯~~19 小时前
HTML——1px问题
前端·html
Never_Satisfied19 小时前
在JavaScript / HTML中,实现`<iframe>` 自适应高度
开发语言·javascript·html
敲代码的嘎仔1 天前
JavaWeb零基础学习Day1——HTML&CSS
java·开发语言·前端·css·学习·html·学习方法
β添砖java1 天前
CSS网格布局
前端·css·html
阿昭L1 天前
html头部
html
杨超越luckly2 天前
HTML应用指南:利用POST请求获取全国中国工商农业银行网点位置信息
大数据·前端·html·数据可视化·银行网点
Never_Satisfied2 天前
在JavaScript / HTML中,浏览器提示 “Refused to execute inline event handler” 错误
开发语言·javascript·html
Never_Satisfied2 天前
在JavaScript / HTML中,事件监听的捕获和冒泡阶段解析
开发语言·javascript·html
qianmo20212 天前
基于pycharm实现html文件的快速实现问题讨论
前端·html