前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue实现pc/H5弹窗拖拽

Vue实现pc/H5弹窗拖拽

作者头像
明知山
发布2020-09-02 16:34:19
1.3K0
发布2020-09-02 16:34:19
举报
文章被收录于专栏:前端开发随笔前端开发随笔

PC

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>pc拖拽</title>
    <style>
        .move {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: steelblue;
            border-radius: 50%;
            cursor: move;
        }
    </style>
</head>

<body>
    <div id="vue">
        <div class="move" @mousedown="mousedown" @mouseup="mouseup"></div>
    </div>
</body>
<script src="https://cdn.suoluomei.com/common/js2.0/vue/v2.5.16/vue.js"></script>
<script>
    new Vue({
        el: '#vue',
        data: {},
        methods: {
            mousedown(e) {
                var target = e.target
                var x = e.clientX
                var y = e.clientY
                var left = x - target.offsetLeft
                var top = y - target.offsetTop
                document.onmousemove = (e) => {
                    var nx = e.clientX - left
                    var ny = e.clientY - top
                    target.style.left = nx + 'px'
                    target.style.top = ny + 'px'
                }
            },
            mouseup(e) {
                document.onmousemove = null
            }
        }
    })
</script>

</html>

H5

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.suoluomei.com/common/js2.0/npm/vant@2.2/lib/index.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>H5拖拽</title>
    <style>
        .move {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: slategray;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div id="Vue">
        <div class="move" :style="{top:top + 'px',left:left + 'px'}" @touchstart="touchstart"
            @touchmove.prevent="touchmove" @touchend="touchend"></div>
    </div>
</body>
<script src="https://cdn.suoluomei.com/common/js2.0/vue/v2.5.16/vue.js"></script>
<script>
    new Vue({
        el: "#Vue",
        data: {
            x: 0,
            y: 0,
            l: 0,
            t: 0,
            left: "",
            top: "",
            isDown: false
        },
        methods: {
            touchstart(e) {
                this.x = e.changedTouches[0].pageX
                this.y = e.changedTouches[0].pageY
                this.l = this.left
                this.t = this.top
                this.isDown = true
            },
            touchmove(e) {
                if (this.isDown) {
                    var nx = e.changedTouches[0].pageX
                    var ny = e.changedTouches[0].pageY
                    var nl = nx - (this.x - this.l)
                    var nt = ny - (this.y - this.t)
                    this.left = nl
                    this.top = nt
                }
            },
            touchend(e) {
                this.isDown = false
            }
        }
    })
</script>

</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档