vue 实现 浮动图标 仿iphone悬浮球

同级目录引用组件 ./
上一级目录,两个点 …/

原文应用案例

<!-- 给定一个初始位置position,插槽中填写想滑动的部分 -->
<xuanfuqiu :position="position">
	<d-add-button @click="addPigFarm" add-item="猪场"></d-add-button>
</xuanfuqiu>


我的引用
一定记得注册组件
不要重复写components


import xuanfuqiu from './xuanfuqiu'


export default {
   name: '',


  components: {
    xuanfuqiu
  },

悬浮球代码

<template>
    <div>
        <div class="xuanfu" id="moveDiv" :style="position"
            @mousedown="down" @touchstart="down"
            @mousemove="move" @touchmove="move"
            @mouseup="end" @touchend="end">
            <slot></slot>
        </div>
    </div>
</template>

<script>
export default {
    name: "",
    components: {},
    props: {
        // 通过position来设置初始定位
        position: {
            type: Object,
            default: function() {
                return {
                    top: "32.25rem",
                    left: "18.34375rem"
                }
            }
        },
        // 通过fixed来禁用自由移动
        fixed: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            flags: false,
            positionTemp: { x: 0, y: 0 },   // 记录手指点击的位置
            nx: '', ny: '', dx: '', dy: '', xPum: '', yPum: '',
        }
    },
    watch: {},
    computed: {},
    methods: {
        // 实现移动端拖拽
        down(){
            if (this.fixed) {
                return
            }

            this.flags = true;
            var touch;
            // 该if判断是用touch还是mouse来移动
            if (event.touches) {
                touch = event.touches[0];
            } else {
                touch = event;
            }
            this.positionTemp.x = touch.clientX;   // 手指点击后的位置
            this.positionTemp.y = touch.clientY;
            
            this.dx = moveDiv.offsetLeft;    // 移动的div元素的位置
            this.dy = moveDiv.offsetTop;
            
            // console.log("moveDiv.offsetLeft", moveDiv.offsetLeft)
            // console.log("touch.clientX", touch.clientX)
        },
        move(){
            if(this.flags) {
                var touch ;
                if(event.touches){
                    touch = event.touches[0];
                }else {
                    touch = event;
                }
                this.nx = touch.clientX - this.positionTemp.x;   // 手指移动的变化量
                this.ny = touch.clientY - this.positionTemp.y;
                
                this.xPum = this.dx + this.nx;   // 移动后,div元素的位置
                this.yPum = this.dy + this.ny;
                
                let windowWidth = document.documentElement.clientWidth
                let windowHeight = document.documentElement.clientHeight
                // console.log("window.clientWidth", windowWidth)
                // console.log(this.xPum)
                // console.log(" moveDiv.clientWidth",  moveDiv.clientWidth)
                
                if (this.xPum > 0 && (this.xPum + moveDiv.clientWidth < windowWidth)) {
                	// movediv的左右边,未出界
                    moveDiv.style.left = this.xPum + "px";
                } else if (this.xPum <= 0) {
                    // 左边出界,则左边缘贴边
                    moveDiv.style.left = 0 + "px";
                } else if (this.xPum + moveDiv.clientWidth >= windowWidth) {
                    // 右边缘出界
                    moveDiv.style.left = (windowWidth - moveDiv.clientWidth) + "px";
                    // console.log("dx", windowWidth - moveDiv.clientWidth)
                }
                // 上下未出界
                if (this.yPum > 0 && (this.yPum + moveDiv.clientHeight < windowHeight)) {
                    moveDiv.style.top = this.yPum +"px";
                } else if (this.yPum <= 0) {
                    // 上边缘出界
                    moveDiv.style.top = 0 + "px"
                } else if (this.yPum + moveDiv.clientHeight >= windowHeight) {
                    // 下边缘
                    // console.log("windowHeight:", windowHeight)
                    // console.log("moveDiv.clientHeight:", moveDiv.clientHeight)
                    // console.log(this.yPum + moveDiv.clientHeight)
                    moveDiv.style.top = windowHeight - moveDiv.clientHeight + "px"
                }

                // 阻止页面的滑动默认事件,为了只让悬浮球滑动,其他部分不滑动;如果碰到滑动问题,1.2 请注意是否获取到 touchmove, 系统默认passive: true,无法使用preventDefault
                // document.addEventListener("touchmove", function(){
                //    event.preventDefault();
                // }, { passive: false });
                // document.addEventListener("mousemove", function(){
                //     event.preventDefault();
                // }, { passive: false });
                document.addEventListener("touchmove", this.preventDefault, { passive: false })
                document.addEventListener("mousemove", this.preventDefault, { passive: false })
            }
        },
        //鼠标释放时候的函数,鼠标释放,移除之前添加的侦听事件,将passive设置为true,不然背景会滑动不了
        end(){
            this.flags = false
            // 注意事项,在添加和删除监听事件时,其function必须是同名的函数,不能为匿名函数。
            document.removeEventListener('touchmove',this.preventDefault, false)
            document.removeEventListener('mousemove',this.preventDefault, false)
            // 下面两句是保证在移除监听事件后,除了悬浮球的部分还能够滑动,如果不添加,则无法滑动
            document.addEventListener("touchmove", function(e) {
                window.event.returnValue = true
            })
            document.addEventListener("mousemove", function(e) {
                window.event.returnValue = true
            })
        },
        preventDefault(e) {
            e.preventDefault()
        }
    },
    created() {},
    mounted() {}
}
</script>

<style lang="scss" scoped>
.xuanfu {
    /* 如果碰到滑动问题,1.3 请检查 z-index。z-index需比web大一级*/
    z-index: 999;
    position: fixed; // 这里的定位方式有待考量,fixed的话存在未知设置不合理,跑出屏幕不显示的问题
}
</style>

参考
https://blog.csdn.net/qq_41009742/article/details/101516232?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现靠边悬浮的代码,可以通过以下步骤来实现: 1. 在Vue组件中,定义一个data属性来存储悬浮的位置和状态信息,如下所示: ```js data() { return { ball: { left: 0, // 悬浮左侧距离屏幕左侧的距离 top: 0, // 悬浮顶部距离屏幕顶部的距离 isFixed: false, // 悬浮是否固定在边缘 isLeft: true // 悬浮是否在左侧边缘 } } } ``` 2. 在组件的`mounted`钩子函数中,监听窗口的滚动事件,并根据窗口滚动的距离和悬浮的位置信息,来判断悬浮是否需要固定在边缘,以及固定在哪个边缘,如下所示: ```js mounted() { window.addEventListener('scroll', () => { const ball = this.ball const scrollTop = document.documentElement.scrollTop || document.body.scrollTop if (scrollTop > ball.top && !ball.isFixed) { ball.isFixed = true ball.top = 0 if (ball.left < 100) { ball.isLeft = true ball.left = 0 } else { ball.isLeft = false ball.left = document.documentElement.clientWidth - 100 } } else if (scrollTop <= ball.top && ball.isFixed) { ball.isFixed = false ball.left = ball.isLeft ? 0 : document.documentElement.clientWidth - 100 } }) } ``` 3. 在组件的模板中,根据悬浮的位置和状态信息,来动态设置悬浮的样式和位置,如下所示: ```html <template> <div> <div class="ball" :class="{ fixed: ball.isFixed, left: ball.isLeft }" :style="{ left: ball.left + 'px', top: ball.top + 'px' }"></div> </div> </template> <style scoped> .ball { position: absolute; width: 100px; height: 100px; border-radius: 50%; background-color: #f00; cursor: pointer; transition: all 0.3s ease; } .ball.fixed { position: fixed; top: 0; } .ball.left { left: 0; } .ball:not(.left) { right: 0; } </style> ``` 通过以上步骤,就可以实现一个Vue组件中的靠边悬浮了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值