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>
相关推荐
前端Hardy9 小时前
HTML&CSS:惊艳!科技感爆棚的登录页面代码解析
前端·javascript·html
Dxy123931021610 小时前
Python Requests-HTML库详解:从入门到实战
开发语言·python·html
Darling02zjh15 小时前
HTML5
前端·html·html5
CodeCraft Studio1 天前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
冰菓Neko1 天前
HTML 常用标签速查表
前端·html
qq_三哥啊2 天前
【HTML】<script>元素中的 defer 和 async 属性详解
html·js
Dxy12393102162 天前
Python HTML模块详解:从基础到实战
开发语言·python·html
Amodoro2 天前
nuxt更改页面渲染的html,去除自定义属性、
前端·html·nuxt3·nuxt2·nuxtjs
飛_2 天前
【Word Press基础】创建一个动态的自定义区块
运维·nginx·html·word press
oioihoii2 天前
恋爱时间倒计时网页设计与实现方案
html