uniapp 拖拽小球编写

编写floatButton组件,主要代码如下

<template>
    <view>
        <view class="float-button"
        :style="{
            left: safeLeft,
            top: safeTop,
            width: width + 'px',
            height: height + 'px',
            'transition': isMove ? 'none' : 'all .2s',
            'display': isShow ? 'block' : 'none'
        }"
        @touchstart.prevent.stop="dragStart" 
        @touchmove.prevent.stop="dragMove"
        @touchend.prevent.stop="dragStop"
        :animation="animation">
            <slot></slot>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            //  距离左边的百分比
            x: {
                type: Number,
                default: 0
            },
            //  距离右边的百分比
            y: {
                type: Number,
                default: 0
            },
            //  宽度
            width: {
                type: Number,
                default: 50
            },
            //  高度
            height: {
                type: Number,
                default: 50
            },
            //  离页面边上的距离
            padding: {
                type: Number,
                default: 10
            }
        },
        data () {
            return {
                isShow: false,
                isMove: false,
                start: [0, 0],
                moveY: null,
                moveX: null,
                windowWidth: null,
                windowHeight: null,
                animation: {},
                timeout: null
            }
        },
        computed: {
            safeLeft () {
                let maxX = this.windowWidth - this.width - this.padding
                let minX = this.padding
                if (this.moveX == null) {
                    let safeX = (this.x / 100) * this.windowWidth
                    return safeX > maxX ? maxX + 'px' : (safeX < minX) ? minX + 'px' : safeX + 'px'
                }
                return this.moveX > maxX ? maxX + 'px' : (this.moveX < minX) ? minX + 'px' : this.moveX + 'px'
            },
            safeTop () {
                let maxY = this.windowHeight - this.height - this.padding
                let minY = this.padding
                if (this.moveY == null) {
                    let safeY = (this.y / 100) * this.windowHeight
                    return safeY > maxY ? maxY + 'px' : (safeY < minY) ? minY + 'px' : safeY + 'px'
                }
                return this.moveY > maxY ? maxY + 'px' : (this.moveY < minY) ? minY + 'px' : this.moveY + 'px'
            }
        },
        mounted () {
            const {
                windowWidth,
                windowHeight
            } = uni.getSystemInfoSync()
            this.windowWidth = windowWidth
            this.windowHeight = windowHeight
            this.$nextTick(() => {
                this.isShow = true
            })
        },
        methods: {
            dragStart(event) {
                this.start[0] = event.touches[0].clientX - event.target.offsetLeft;
                this.start[1] = event.touches[0].clientY - event.target.offsetTop;
            },
            //判断防止悬浮球被拖出页面
            dragMove(event) {
                this.isMove = true
                const touch = event.touches[0] || event.changedTouches[0]
                this.moveX = touch.clientX - this.start[0]
                this.moveY = touch.clientY - this.start[1]
            },
            dragStop (event) {
                // 判断在左侧还是右侧
                if (this.isMove) {
                    this.isMove = false
                    if (this.moveX > this.windowWidth / 2) {
                        this.moveX = this.windowWidth - this.width - this.padding
                    } else {
                        this.moveX = 0
                    }
                } else {
                    this.$emit('click')
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .float-button{
        position: fixed;
        z-index: 999;
        transition: all .2s;
    }
</style>

使用

<floatButton :width="40" :height="40" :x="90" :y="90" @click="clickFun">
  // 这里放置需要的内容
</floatButton>

效果如图所示

image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 商城首页App的编写,首先我们要确定我们需要确定一下大框架: 如图所示,我们可以看到,首先我们需要一个内容根布局,...
    仲夏之雪梦旅人阅读 4,332评论 0 1
  • 协程属于Kotlin中非常有特色的一项技术,因为大部分编程语言中是没有协程这个概念的。 那么什么是...
    隨風cvil阅读 4,790评论 0 1
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,515评论 2 59
  • 通过第 12 章的学习,大家已经知道后台的请求处理方法可以包含多种参数类型。 在实际的项目开发中,多数情况下客户端...
    辽A丶孙悟空阅读 3,480评论 1 12
  • 第一章 初识Flink 大数据开发总体架构 数据传输层:常用的数据传输工具有Flume、Sqoop、Kafka。F...
    日落_3d9f阅读 13,320评论 0 9

友情链接更多精彩内容