VUE PC端可拖动悬浮按钮的实现代码

 更新时间:2024年02月02日 11:25:43   作者:Best_Liu~  
这篇文章主要介绍了VUE PC端可拖动悬浮按钮的实现代码,通过实例代码介绍了父页面引用的方法,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下

一、实现效果:

二、FloatButton.vue

<template>
    <div>
        <div class="sssss">
            <div class="callback float" @mousedown="down" @touchstart="down" @mousemove="move" @touchmove="move" @mouseover="over" @mouseout="out"
                @mouseup="end" @touchend="end" ref="fu" style="color: #1a1919;">
                <el-button circle @click="screen()">
                    {{name}}
                </el-button>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        name: "suspensionBall",
        props: {
            name: {
                type: String,
                default: '打开全屏'
            },
        },
        data() {
            return {
                left: 0,
                top: 40,
                bg: 1,
                menu: false,
                isLoading: false,
                flags: false, //控制使用
                position: {
                    x: 0,
                    y: 0,
                },
                nx: "",
                ny: "",
                dx: "",
                dy: "",
                xPum: "",
                yPum: "",
                movb: 1,
                num: 1,
            };
        },
        created() { },
        mounted() {
            this.left = this.$refs.fu.offsetLeft - 750;
        },
        methods: {
            out2() {
                this.menu = false;
            },
            over2() { },
            out() {
                this.bg = 2;
            },
            over() {
                this.menu = true;
                this.num = 2;
                this.bg = 1;
            },
            callback() {
                this.$router.go(-1);
            },
            onRefresh() {
                // window.location.reload();
                setTimeout((res) => {
                    console.log(res);
                    this.isLoading = false;
                }, 1000);
            },
            down() {
                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 = this.$refs.fu.offsetLeft;
                this.dy = this.$refs.fu.offsetTop;
            },
            move() {
                if (this.flags) {
                    this.movb = 2;
                    this.menu = false;
                    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;
                    let width = window.innerWidth - this.$refs.fu.offsetWidth; //屏幕宽度减去自身控件宽度
                    let height = window.innerHeight - this.$refs.fu.offsetHeight; //屏幕高度减去自身控件高度
                    this.xPum < 0 && (this.xPum = 0);
                    this.yPum < 0 && (this.yPum = 0);
                    this.xPum > width && (this.xPum = width);
                    this.yPum > height && (this.yPum = height);
                    // if (this.xPum >= 0 && this.yPum >= 0 && this.xPum<= width &&this.yPum<= height) {
                    this.$refs.fu.style.left = this.xPum + "px";
                    this.$refs.fu.style.top = this.yPum + "px";
                    this.left = this.xPum - 750;
                    this.top = this.yPum;
                    // }
                    //阻止页面的滑动默认事件
                    document.addEventListener(
                        "touchmove",
                        function () {
                            event.preventDefault();
                        },
                        false
                    );
                }
            },
            //鼠标释放时候的函数
            end() {
                this.flags = false;
            },
            screen() {
                this.$emit("changeClick");
            },
        },
    };
</script>
<style scoped>
    .callback {
        position: fixed;
        width: 200px;
        height: 20px;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        top: 200px;
        left: 90%;
        z-index: 99999;
    }
    .float {
        position: fixed;
        touch-action: none;
        text-align: center;
        border-radius: 24px;
        line-height: 48px;
        color: white;
    }
    .menuclass {
        text-align: left;
        position: absolute;
        color: #000;
        width: 764px;
        background: #ffffff;
        box-shadow: 0px 6px 26px 1px rgba(51, 51, 51, 0.16);
        padding: 20px;
    }
    .sssss {
        position: relative;
        background-color: #000;
        right: 0;
        z-index: 99999;
    }
    .titlea {
        font-size: 18px;
        font-family: Microsoft YaHei-Bold, Microsoft YaHei;
        font-weight: bold;
        color: #333333;
    }
    .boxa {
        display: flex;
        flex-wrap: wrap;
        margin-top: 20px;
        z-index: 999999;
    }
    .item {
        width: 168px;
        height: 75px;
        border-radius: 4px 4px 4px 4px;
        font-size: 16px;
        font-family: Microsoft YaHei-Bold, Microsoft YaHei;
        font-weight: bold;
        color: #ffffff;
        text-align: center;
        margin-left: 7px;
        line-height: 75px;
    }
</style>

三、父页面引用

<template>
    <div>
        <float-button ref="floatButton" :name="buttonName" @changeClick="screen" />
    </div>
</template>
<script>
import FloatButton from './chart/FloatButton'
export default {
        name: 'Index',
        components: {
            FloatButton
        },
}
</script>

到此这篇关于VUE PC端可拖动悬浮按钮的文章就介绍到这了,更多相关vue拖动悬浮按钮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue 动态设置浏览器标题的方法详解

    vue 动态设置浏览器标题的方法详解

    这篇文章主要为大家介绍了vue动态设置浏览器标题的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • VUE+Canvas实现财神爷接元宝小游戏

    VUE+Canvas实现财神爷接元宝小游戏

    这篇文章主要介绍了VUE+Canvas实现财神爷接元宝小游戏,需要的朋友可以参考下本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2021-04-04
  • Vue中的过滤器(filter)详解

    Vue中的过滤器(filter)详解

    vue filter 过滤器处理数据的作用,使用位置:mustache插值和v-bind表达式,过滤器用于文本转换,复杂的数据处理则用computed,这篇文章主要介绍了Vue中的过滤器(filter),需要的朋友可以参考下
    2022-11-11
  • ElementUI级联选择器实现同一父级下最多只能选中一个子级

    ElementUI级联选择器实现同一父级下最多只能选中一个子级

    本文主要介绍了ElementUI级联选择器实现同一父级下最多只能选中一个子级,同一父级下的子节点单选,又可以选择多个不同父级下的节点,具有一定参考价值,感兴趣的可以了解一下
    2023-10-10
  • vue-cli3配置多项目并按项目分别实现打包

    vue-cli3配置多项目并按项目分别实现打包

    这篇文章主要介绍了vue-cli3配置多项目并按项目分别实现打包方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • vue车牌搜索组件使用方法详解

    vue车牌搜索组件使用方法详解

    这篇文章主要为大家详细介绍了vue车牌搜索组件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • vue中Promise的使用方法详情

    vue中Promise的使用方法详情

    这篇文章主要介绍了vue中Promise的使用方法详情,Promise可以说是异步编程的一种解决方法,主要是为了解决代码乱的情景而出现的,下文介绍其具体用法,需要的小伙伴可以参考一下
    2022-03-03
  • 在Vue当中同时配置多个路由文件的方法案例代码

    在Vue当中同时配置多个路由文件的方法案例代码

    这篇文章主要介绍了在Vue当中同时配置多个路由文件的方法,包含具体代码,本文分步骤结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • vue判断input输入内容全是空格的方法

    vue判断input输入内容全是空格的方法

    下面小编就为大家分享一篇vue判断input输入内容全是空格的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • vue踩坑之在input中使用filters局部过滤器问题

    vue踩坑之在input中使用filters局部过滤器问题

    这篇文章主要介绍了vue踩坑之在input中使用filters局部过滤器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11

最新评论