美文网首页
vue拖拽效果电脑手机

vue拖拽效果电脑手机

作者: 衬fzy | 来源:发表于2022-03-25 10:57 被阅读0次
image.png
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>vue拖拽效果电脑手机</title>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width" />
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.6/dist/vue.min.js"></script>
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    <style>
        body {
            font-size: 30px;
        }

        .xuanfu {
            height: 4.5rem;
            width: 4.5rem;
            /* 如果碰到滑动问题,1.3 请检查 z-index。z-index需比web大一级*/
            z-index: 999;
            position: fixed;
            top: 6.2rem;
            left: 50%;
            border-radius: 0.8rem;
            background-color: rgba(0, 0, 0, 0.55);
        }

        .yuanqiu {
            height: 2.7rem;
            width: 2.7rem;
            border: 0.3rem solid rgba(140, 136, 136, 0.5);
            margin: 0.65rem auto;
            color: #000000;
            font-size: 1.6rem;
            line-height: 2.7rem;
            text-align: center;
            border-radius: 100%;
            background-color: #ffffff;
        }
    </style>
    <body>
        <div id="app">
            <div>你的web页面</div>
            <!-- 如果碰到滑动问题,1.1 请检查这里是否属于同一点。 -->
            <!-- 悬浮的HTML -->
            <div class="xuanfu" id="moveDiv" @mousemove="move" @touchmove.prevent="move" @mouseup="end" @touchend="end">
                <div class="yuanqiu" @mousedown="down" @touchstart="down"></div>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            data() {
                return {
                    flags: false,
                    position: {
                        x: 0,
                        y: 0
                    },
                    nx: '',
                    ny: '',
                    dx: '',
                    dy: '',
                    xPum: '',
                    yPum: '',
                }
            },
            methods: {
                // 实现移动端拖拽
                down() {
                    console.log(1)
                    this.flags = true;
                    var touch;
                    if (event.touches) {
                        touch = event.touches[0];
                    } else {
                        touch = event;
                    }
                    this.position.x = touch.clientX;
                    this.position.y = touch.clientY;
                    this.dx = moveDiv.offsetLeft;
                    this.dy = moveDiv.offsetTop;
                },
                move() {
                    if (this.flags) {
                        console.log(2)
                        var touch;
                        if (event.touches) {
                            touch = event.touches[0];
                        } else {
                            touch = event;
                        }
                        this.nx = touch.clientX - this.position.x;
                        this.ny = touch.clientY - this.position.y;
                        this.xPum = this.dx + this.nx;
                        this.yPum = this.dy + this.ny;
                        moveDiv.style.left = this.xPum + "px";
                        moveDiv.style.top = this.yPum + "px";
                        //阻止页面的滑动默认事件;如果碰到滑动问题,1.2 请注意是否获取到 touchmove
                        document.addEventListener("touchmove", function() {
                            event.preventDefault();
                        }, false);
                    }
                },
                //鼠标释放时候的函数
                end() {
                    console.log(3)
                    this.flags = false;
                },
            }
        })
    </script>
</html>

感谢大家点赞!

相关文章

  • vue拖拽效果电脑手机

    感谢大家点赞!

  • vue拖拽效果

    在实现前有必须说一下鼠标事件的几个重要属性 这里有两种方式实现,一个是基本的vue组件,另外一个是vue的指令的方...

  • 封装移动端 vue 拖拽指令

    封装移动端 vue 拖拽指令 通过vue自定义指令,将拖拽行为封装为指令 使用transform做移动效果,需要注...

  • Vue 实现拖拽效果

    Vue 怎么实现拖拽,本来想用 Vue + 原生的 JS + HTML5 自己造一个轮子,这段时间公司要推全栈化,...

  • 关于怎么在手机端实现一个拖拽的操作

    手机端,肯定是监听touchstart,touchmove,touchend事件先来看看效果 当拖拽时,拖拽到哪个...

  • 基于Vue实现拖拽效果

    效果图 分清clientY pageY screenY layerY offsetY的区别 在我们想要做出拖拽这个...

  • 基于Vue实现拖拽效果

    效果图 分清clientY pageY screenY layerY offsetY的区别 在我们想要做出拖拽这个...

  • vue实现图片拖拽效果

    1.安装依赖包 2.main.js文件(全局)引入 3.在使用图片拖拽的页面对应的循环的标签加上以下代码: 4.在...

  • vue网格拖拽排序

    上图是网格拖拽排序的效果图,如果在你的项目中也有这样的需求,那么不妨来看看如何实现。 技术框架:vue.js 拖拽...

  • 拖拽API

    实现拖拽效果 目前实现拖拽效果 HTML5拖拽 源元素事件例子 目标元素事件 从本地拖放图片到页面中 实现拖拽

网友评论

      本文标题:vue拖拽效果电脑手机

      本文链接:https://www.haomeiwen.com/subject/rmwrjrtx.html