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拖动悬浮按钮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • axios取消请求的实践记录分享

    axios取消请求的实践记录分享

    今天小编就为大家带来一篇axios取消请求的实践记录分享,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Vue3项目中使用自适应Rem示例

    Vue3项目中使用自适应Rem示例

    这篇文章主要为大家介绍了Vue3项目中使用自适应Rem示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • 使用Vue后如何针对搜索引擎做SEO优化

    使用Vue后如何针对搜索引擎做SEO优化

    本文介绍了Vue.js在SEO优化方面的挑战,并提供了一些方法来解决这些问题,包括使用服务器端渲染、预渲染和使用VueRouter的History模式来生成静态HTML页面,以及添加meta信息和内部链接来提高搜索引擎的索引和排名
    2025-02-02
  • Vue quill-editor 编辑器使用及自定义toobar示例详解

    Vue quill-editor 编辑器使用及自定义toobar示例详解

    这篇文章主要介绍了Vue quill-editor编辑器使用及自定义toobar示例详解,这里讲解编辑器quil-editor的知识结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • Vue起步(无cli)的啊教程详解

    Vue起步(无cli)的啊教程详解

    本文通过实例代码给大家介绍了Vue起步(无cli)的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
    2019-04-04
  • mpvue写一个CPASS小程序的示例

    mpvue写一个CPASS小程序的示例

    这篇文章主要介绍了mpvue写一个CPASS小程序的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • Vue实现自定义组件改变组件背景色(示例代码)

    Vue实现自定义组件改变组件背景色(示例代码)

    要实现 Vue 自定义组件改变组件背景色,你可以通过 props 将背景色作为组件的一个属性传递给组件,在组件内部监听这个属性的变化,并将其应用到组件的样式中,下面通过示例代码介绍Vue如何实现自定义组件改变组件背景色,感兴趣的朋友一起看看吧
    2024-03-03
  • Vue实现省市区三级联动el-select组件的示例代码

    Vue实现省市区三级联动el-select组件的示例代码

    这篇文章主要为大家详细介绍了Vue实现省市区三级联动el-select组件的方法,文中的示例代码讲解详细,具有一定的借鉴价值,需要的的可以参考一下
    2023-02-02
  • 使用vite搭建ssr活动页架构的实现

    使用vite搭建ssr活动页架构的实现

    本文主要介绍了使用vite搭建ssr活动页架构,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Vue vxe-table使用问题收录小结

    Vue vxe-table使用问题收录小结

    这篇文章主要为大家介绍了Vue vxe-table使用问题收录小结,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09

最新评论