vue项目实现按钮可随意移动

 更新时间:2022年03月30日 08:51:15   作者:帅_帅  
这篇文章主要为大家详细介绍了vue项目实现按钮可随意移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

vue项目中实现按钮可随意移动,供大家参考,具体内容如下

组件代码如下:

在项目中引入该组件即可

<template>
  <div v-show="hide" class="move-button" ref="moveBtn"
       @mousedown="btnDown"
       @touchstart="btnDown"
       @mousemove="btnMove"
       @touchmove="btnMove"
       @mouseup="btnEnd"
       @touchend="btnEnd"
       @touchcancel="btnEnd">
    <div class="button-mainbg">
    </div>
    </div>
</template>

<script>
export default {
    name: 'MoveButton',
    data() {
        return {
            hide: true,
            img: require('@/assets/img/moveButton.png'),
            flags: false,
            position: {
                x: 0,
                y: 0
            },
            nx: '',
            ny: '',
            dx: '',
            dy: '',
            xPum: '',
            yPum: '',
            isShow: false,
            moveBtn: {},
            timer: null,
            currentTop:0
        }
    },
    mounted() {
        this.moveBtn = this.$refs.moveBtn;
        window.addEventListener('scroll', this.hideButton);
    },
    beforeDestroy() {
        window.addEventListener('scroll', this.hideButton);
    },
    methods: {
        hideButton() {
            this.timer&&clearTimeout(this.timer);
            this.timer = setTimeout(()=>{
             this.handleScrollEnd();
            },300);
            this.currentTop = document.documentElement.scrollTop || document.body.scrollTop;
            this.hide = false;
        },
        handleScrollEnd(){
            let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            if(scrollTop === this.currentTop){
            this.hide = true;
            clearTimeout(this.timer);
            }
        },

        // 实现移动端拖拽
      btnDown() {
            this.flags = true;
            let touch;
            if (event.touches) {
                touch = event.touches[0];
            } else {
                touch = event;
            }
            this.position.x = touch.clientX;
            this.position.y = touch.clientY;
            this.dx = this.moveBtn.offsetLeft;
            this.dy = this.moveBtn.offsetTop;
        },
      btnMove() {
            if (this.flags) {
                let 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 clientWidth = document.documentElement.clientWidth;
              let clientHeight = document.documentElement.clientHeight;
                if (this.xPum > 0 && this.xPum < (clientWidth - this.moveBtn.offsetWidth)) {
                    this.moveBtn.style.left = this.xPum + "px";
                }
                if (this.yPum > 0 && this.yPum < (clientHeight - this.moveBtn.offsetHeight)) {
                    this.moveBtn.style.top = this.yPum + "px";
                }

                //阻止页面的滑动默认事件
                document.addEventListener("touchmove", this.handler, {
                    passive: false
                });
            }
        },
        //鼠标释放时候的函数
        btnEnd() {
            this.flags = false;
            document.addEventListener('touchmove', this.handler, {
                passive: false
            });
        },
        handler(e) {
            if(this.flags){
                event.preventDefault();
            }else{
                return true
            }
        }
    }
}
</script>

<style lang="stylus" scoped>
.move-button {

    border-radius:50%;
    width: 50px;
    height: 50px;
    position: fixed;
    z-index: 10;

  color: #FFF;

  .button-mainbg{
    position: relative;
    width:50px;
    height:50px;
    border-radius:50%;
    background: url("../../assets/img/moveButton.png") no-repeat;
    background-size: 50px 50px;
  }


}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Vue基于vue-quill-editor富文本编辑器使用心得

    详解Vue基于vue-quill-editor富文本编辑器使用心得

    这篇文章主要介绍了Vue基于vue-quill-editor富文本编辑器使用心得,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • vue实现拖拽滑动分割面板

    vue实现拖拽滑动分割面板

    这篇文章主要为大家详细介绍了vue实现拖拽滑动分割面板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 关于Element table组件滚动条不显示的踩坑记录

    关于Element table组件滚动条不显示的踩坑记录

    这篇文章主要介绍了关于Element table组件滚动条不显示的踩坑记录,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Vue+Element UI实现概要小弹窗的全过程

    Vue+Element UI实现概要小弹窗的全过程

    弹窗效果是我们日常开发中经常遇到的一个功能,下面这篇文章主要给大家介绍了关于Vue+Element UI实现概要小弹窗的相关资料,需要的朋友可以参考下
    2021-05-05
  • Vue.js中的computed功能设计

    Vue.js中的computed功能设计

    computed作为计算属性其作用是描述响应式数据的复杂逻辑计算,当所依赖的响应式数据发生改变时计算属性会重新计算,更新逻辑计算的结果,这篇文章主要介绍了Vue.js中的computed的功能设计,需要的朋友可以参考下
    2023-06-06
  • 一文带你了解什么是Vue Vapor

    一文带你了解什么是Vue Vapor

    随着Svelte和SolidJS的流行,无虚拟DOM模式逐渐开始火了起来,Vue Vapor就是一个无虚拟DOM模式版本的vue,下面就跟随小编一起来深入了解一下Vue Vapor吧
    2024-11-11
  • VUE开发分布式医疗挂号系统后台管理页面步骤

    VUE开发分布式医疗挂号系统后台管理页面步骤

    本文从整体上介绍Vue框架的开发流程,结合具体的案例,使用Vue框架调用具体的后端接口,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-04-04
  • Vue+Axios实现文件上传自定义进度条

    Vue+Axios实现文件上传自定义进度条

    这篇文章主要为大家详细介绍了Vue+Axios实现文件上传自定义进度条,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • vue实现同时设置多个倒计时

    vue实现同时设置多个倒计时

    这篇文章主要为大家详细介绍了vue实现同时设置多个倒计时,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • Vue项目中如何引入本地第三方JS库

    Vue项目中如何引入本地第三方JS库

    vue中常遇到第三方js,这篇文章主要给大家介绍了关于Vue项目中如何引入本地第三方JS库的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2023-06-06

最新评论