Vue2 悬浮球

<template>
  <div>
    <FloatBall />
  </div>
</template>
 
<script>
import FloatBall from "@/components/FloatBall.vue"
export default {
     
  name: "HomeView",
  components: {
      FloatBall },
  data() {
     
    return {
     
    
    };
  },
  mounted() {
     
   
  }
};
</script>
 
<style lang="less" scoped>
</style>
 

<template>
    <transition>
        <div ref="floatBall" class="floatBall" @touchstart.stop="handleStart"
            @touchmove.prevent.stop="handleMove($event)" @touchend.stop="handleEnd"
            :style="{ left: left + 'px', top: top + 'px', width: itemWidth + 'px', height: itemHeight + 'px' }"
            v-if="isShow">
            {
  { text }}
        </div>
    </transition>
</template>

<script>
export default {
     
    props: {
     
        text: {
     
            type: String,
            default: '悬浮'
        },
        itemWidth: {
     
            type: Number,
            default: 80
        },
        itemHeight: {
     
            type: Number,
            default: 80
        }
    },
    data() {
     
        return {
     
            left: 0,
            top: 0,
            startToMove: false,
            isShow: true,
            timer: null,
            currentTop: null,
            clientW: document.documentElement.clientWidth, // 视口宽
            clientH: document.documentElement.clientHeight, // 视口高
        }
    },
    created() {
     
        this.left = (this.clientW - this.itemWidth - 30)
        this.top = (this.clientH / 2 - this.itemHeight / 2)
    },
    mounted() {
     
        this.bindScrollEvent()
    },
    beforeDestroy() {
     
        this.removeScrollEvent()
    },
    methods: {
     
        handleStart() {
     
            this.startToMove = true
            this.$refs.floatBall.style.transition = "none"
        },
        handleMove(e) {
     
            const clientX = e.targetTouches[0].clientX;
            const clientY = e.targetTouches[0].clientY;
            const isInScreen = clientX <= this.clientW && clientX >= 0 && clientY <= this.clientH && clientY >= 0
            if (this.startToMove && e.targetTouches.length === 1) {
     
                if (isInScreen) {
     
                    this.left = clientX - this.itemWidth / 2
                    this.top = clientY - this.itemHeight / 2
                }
            }
        },
        handleEnd() {
     
            if (this.left < (this.clientW / 2)) {
     
                this.left = 30;//不让贴边 所以设置30没设置0
                this.handleIconY()
            } else {
     
                this.left = this.clientW - this.itemWidth - 30;//不让贴边 所以减30
                this.handleIconY()
            }
            this.$refs.floatBall.style.transition = "all .3s"
        },
        handleIconY() {
     
            if (this.top < 0) {
     
                this.top = 30;
            } else if (this.top + this.itemHeight > this.clientH) {
     
                this.top = this.clientH - this.itemHeight - 30;
            }
        },
        bindScrollEvent() {
     
            window.addEventListener('scroll', this.handleScrollStart)
        },
        removeScrollEvent() {
     
            window.removeEventListener('scroll', this.handleScrollStart)
        },
        handleScrollStart() {
     
            this.isShow = false
            this.timer && clearTimeout(this.timer)
            this.timer = setTimeout(() => {
     
                this.handleScrollEnd()
            }, 300)
            this.currentTop = document.documentElement.scrollTop || document.body.scrollTop
        },
        handleScrollEnd() {
     
            this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 判断是否停止滚动的条件
            if (this.scrollTop == this.currentTop) {
     
                this.isShow = true
            }
        }
    },
}
</script>

<style scoped>
.floatBall {
     
    position: fixed;
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background-color: #f0f;
    line-height: 80px;
    text-align: center;
    color: #fff;
}

.v-enter {
     
    opacity: 1;
}

.v-leave-to {
     
    opacity: 0;
}

.v-enter-active,
.v-leave-active {
     
    transition: opacity 0.3s;
}
</style>

相关推荐

  1. Vue2 悬浮

    2024-01-31 00:16:03       37 阅读
  2. css实现拖拽悬浮组件

    2024-01-31 00:16:03       17 阅读
  3. vue实现小掉落

    2024-01-31 00:16:03       41 阅读
  4. vue实现悬浮窗拖动的自定义指令

    2024-01-31 00:16:03       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-31 00:16:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-31 00:16:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-31 00:16:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-31 00:16:03       18 阅读

热门阅读

  1. Springboot整合mqtt采用注解进行监听(第二篇)

    2024-01-31 00:16:03       38 阅读
  2. 2.1写一个梅林dynv6插件(上)

    2024-01-31 00:16:03       49 阅读
  3. 为什么Vue3双向绑定使用Proxy

    2024-01-31 00:16:03       45 阅读
  4. 飞往前端的第二天

    2024-01-31 00:16:03       33 阅读
  5. SpringMVC初始化源码学习

    2024-01-31 00:16:03       34 阅读
  6. Chinese and English names of 45 common character symbols

    2024-01-31 00:16:03       28 阅读
  7. Map和Set

    Map和Set

    2024-01-31 00:16:03      34 阅读
  8. 如何编写.gitignore文件

    2024-01-31 00:16:03       28 阅读