vue 移动端可拖拽式icon图标

需求描述:在移动端页面悬浮一个可随便拖拽的图标,类似于苹果手机的辅助触控小圆点,并且可随意拖动。

预览:

 组件代码如下:

<template>
    <div class="ys-float-btn" :style="{'width':itemWidth+'px','height':itemHeight+'px','left':left+'px','top':top+'px'}"
         ref="div"
         @click ="onBtnClicked">
        <slot name="icon"></slot>
        <SvgIcon :iconClass="'changjianwentijieda'"
                 :style="{'width':itemWidth+'px','height':itemHeight+'px'}"/>
    </div>
</template>


<script>
    export default {
        name: "DragIcon",
        props:{
            itemWidth:{
                type:Number,
                default:40
            },
            itemHeight:{
                type:Number,
                default:40
            },
            gapWidth:{
                type:Number,
                default:10
            },

            coefficientHeight:{
                type:Number,
                default:0.8
            }
        },
        created(){
            this.clientWidth = document.documentElement.clientWidth;
            this.clientHeight = document.documentElement.clientHeight;
            this.left = this.clientWidth - this.itemWidth - this.gapWidth;
            this.top = this.clientHeight*this.coefficientHeight;
        },
        mounted(){
            this.$nextTick(()=>{
                const div = this.$refs.div;
                div.addEventListener("touchstart",(e)=>{
                    e.stopPropagation();
                    div.style.transition = 'none';
                });
                div.addEventListener("touchmove",(e)=>{
                        e.stopPropagation();
                        if (e.targetTouches.length === 1) {
                            let touch = event.targetTouches[0];
                            this.left = touch.clientX - this.itemWidth/2;
                            this.top = touch.clientY - this.itemHeight/2;
                        }
                    },
                    false
                );
                div.addEventListener("touchend",(e)=>{
                    e.stopPropagation();
                    div.style.transition = 'all 0.3s';
                    if(this.left>this.clientWidth/2){
                        this.left = this.clientWidth - this.itemWidth - this.gapWidth;
                    }else{
                        this.left = this.gapWidth;
                    }
                    if(this.top<=36)
                    {
                        this.top=36+this.gapWidth
                    }
                    else{
                        let bottom=this.clientHeight-50-this.itemHeight-this.gapWidth
                        console.log(bottom,this.top)
                        if(this.top>=bottom)
                        {
                            this.top=bottom
                        }

                    }
                });
            });
        },

        methods:{
            onBtnClicked(){
                this.$emit("onFloatBtnClicked");
            },

        },
        data(){
            return{
                timer:null,
                currentTop:0,
                clientWidth:0,
                clientHeight:0,
                left:0,
                top:0,
            }
        }
    }
</script>



<style lang="scss" scoped>
    .ys-float-btn{
        background:rgba(56,181,77,1);
        box-shadow:0 2px 10px 0 rgba(0,0,0,0.1);
        border-radius:50%;
        color: #666666;
        z-index: 20;
        transition: all 0.3s;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        position: fixed;
        bottom: 20vw;

        img{
            width: 50%;
            height: 50%;
            object-fit: contain;
            margin-bottom: 3px;
        }
    }
    .su_img{
        width: 40px;
        height: 40px;
        margin: 8px 0 0 0;
    }

</style>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值