js实现div的拖动放大缩小

这篇博客介绍了一个使用JavaScript实现div元素拖动和缩放的示例。通过监听鼠标事件,实现了地图图片及其矩形框的拖动和滚轮缩放效果,代码中详细解释了各个函数的作用,有助于理解JavaScript在交互式网页开发中的应用。

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

js实现div的拖动放大缩小

效果

js实现div的拖动放大缩小

code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #mapwrap {
            width: 1000px;
            height: 900px;
            border: green solid 1px;
            margin: 0 auto;
            position: relative;
            overflow: hidden;
            transform: translate(0);
            cursor: pointer;
            z-index: 5;
            overflow: hidden;
        }


        #mapImg {
            width: 800px;
            height: 511px;
            border: 1px solid green;
            /* background-image: url('img/mayi.jpeg'); */
            /* background-repeat: no-repeat; */
            position: absolute;
            top: 0;
            left: 0;
            z-index: 3;
            zoom: 100%;
        }

        #mapImg img {
            width: 100%;
            height: 100%;
            z-index: 0;
            -webkit-user-drag: none;
        }

        #rect {
            width: 80px;
            height: 80px;
            z-index: 8;
            border: 1px solid red;
            position: absolute;
            top: 219px;
            left: 350px;
        }
    </style>
</head>

<body>
    <div id="mapwrap">
        <div id="mapImg">
            <img src="img/mayi.jpeg" />
        </div>
        <div id="rect">
        </div>
</body>
<script>
    // import MySizeChange from './test'
    var mapwrap = document.getElementById('mapwrap')
    var mapImage = document.getElementById('mapImg')
    var rect = document.getElementById('rect')

    function MySizeChange(obj, w, h, x, y) {

        this.x = x;
        this.y = y;
        this.preWidth = w;
        this.preHeight = h;
        this.obj = obj
        this.down = false
        this.pressX = 0
        this.pressY = 0

        this.onMouseWheel = function (e) {
            let width = parseInt(this.obj.style.width, 10) || w;
            var delta = Math.round(e.wheelDelta);
            width += delta;
            // console.log(width);

            if (width > w) {
                let k = width / this.preWidth     //缩放倍数

                obj.style.width = width + 'px'
                this.preWidth = width  //保存上一次宽度

                let height = Math.round(this.preHeight * k)
                //本次

                this.obj.style.height = height + 'px'
                this.preHeight = height

                let deltaX = Math.round(width * (k - 1) / 2)
                let deltaY = Math.round(height * (k - 1) / 2)

                this.x = this.x - deltaX
                this.y = this.y - deltaY


                this.obj.style.left = this.x + 'px'
                this.obj.style.top = this.y + 'px'
                //减少到800 还原
            } else {
                this.obj.style.width = w + 'px'
                this.obj.style.height = h + 'px'
                this.obj.style.top = y + 'px'
                this.obj.style.left = x + 'px'

                this.x = x, this.y = y

            }
        }
        this.onMouseDown = function (e) {
            // console.log(e);
            this.down = true
            this.pressX = e.layerX
            this.pressY = e.layerY
        }
        this.onMouseMove = function (e) {
            if (this.down) {
                let offsetX = e.layerX - this.pressX
                let offsetY = e.layerY - this.pressY
                this.x = this.x + offsetX
                this.y = this.y + offsetY
                this.obj.style.left = this.x + 'px'
                this.obj.style.top = this.y + 'px'
            }

        }
        this.onMouseUp = function (e) {
            this.down = false
        }
    }



    var obj = new MySizeChange(mapImage, 800, 511, 0, 0)
    var obj1 = new MySizeChange(rect, 350, 219, 0, 0)
    mapImage.onmousedown = function (e) {
        obj.onMouseDown(e)
        obj1.onMouseDown(e)
    }
    mapImage.onmousemove = function (e) {
        obj.onMouseMove(e)
        obj1.onMouseMove(e)
    }
    mapImage.onmouseup = function (e) {
        obj.onMouseUp(e)
        obj1.onMouseUp(e)
    }
    mapImage.onmousewheel = function (e) {
        obj.onMouseWheel(e)
    }

</script>

</html>

用到的图片:
11

在Vue2中使用Hammer.js实现移动端div的拖拽、放大缩小和旋转可以按照以下步骤进行。 首先,在Vue项目中安装Hammer.js。 ``` npm install hammerjs ``` 然后,在需要使用拖拽、放大缩小和旋转功能的组件中引入Hammer.js,并初始化一个Hammer实例,将其绑定到要拖拽、放大缩小和旋转的div元素上。 ``` import Hammer from 'hammerjs' export default { mounted() { const element = this.$refs.element // 获取div元素的引用 const mc = new Hammer.Manager(element) // 初始化Hammer实例并将其绑定到div元素上 // 添加拖拽、放大缩小和旋转的手势识别 mc.add(new Hammer.Pan({ threshold: 0, pointers: 0 })) mc.add(new Hammer.Pinch({ threshold: 0 })).recognizeWith(mc.get('pan')) mc.add(new Hammer.Rotate({ threshold: 0 })).recognizeWith(mc.get('pan')) // 初始化div元素的拖拽、放大缩小和旋转的初始状态值 let posX = 0 let posY = 0 let scale = 1 let lastScale = 1 let rotation = 0 // 监听拖拽事件 mc.on('pan', (e) => { // 实现拖拽 posX = e.deltaX posY = e.deltaY element.style.transform = `translate(${posX}px, ${posY}px) scale(${scale}) rotate(${rotation}deg)` }) // 监听放大缩小事件 mc.on('pinch', (e) => { // 实现放大缩小 scale = lastScale * e.scale element.style.transform = `translate(${posX}px, ${posY}px) scale(${scale}) rotate(${rotation}deg)` }) // 监听旋转事件 mc.on('rotate', (e) => { // 实现旋转 rotation = e.rotation element.style.transform = `translate(${posX}px, ${posY}px) scale(${scale}) rotate(${rotation}deg)` }) } } ``` 最后,在该组件的模板中添加一个div元素,并给其添加初始样式。 ``` <template> <div ref="element" style="width: 200px; height: 200px; background-color: red;"></div> </template> ``` 这样,当你在移动端上访问这个页面时,就可以拖拽、放大缩小和旋转这个div元素了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值