zoukankan      html  css  js  c++  java
  • uni-app拖拽悬浮球优化

    使用uni-app拖拽悬浮球,插件不错 ,插件地址 ext.dcloud.net.cn/plugin?id=5…

    插件挺不错的,有几点需求我改了下
    1、背景图片保持纵横比缩放图片,使用aspectFit好点
    2、初始化球位置时使用%比较符合实际,如果放到右底部使用px还要适配
    3、拖拽超出边框没有做限制。



    <template>
        <view class="holdon" >
            <image  class="ball" :style="'left:'+(moveX == 0 & x>0? x+'%':moveX+'px')+';top:'+(moveY == 0 & y>0? y+'%':moveY+'px')"     
                    @touchstart="drag_start" @touchmove.prevent="drag_hmove" :src="image"  mode="aspectFit">
            </image>
        </view>
    </template>
    <script>
        export default {
            props: {
                x: {
                    type: Number,
                    default:0
                },
                y: {
                    type: Number,
                    default:0
                },
                image:{
                    type:String,
                    default: ''
                }
            },
            data() {
                return {
                    start:[0,0],
                    moveY:0,
                    moveX:0,
                    windowWidth:'',
                    windowHeight:'',
                }
            },
            mounted() {
                const { windowWidth, windowHeight } = uni.getSystemInfoSync();    
                this.windowWidth = windowWidth
                this.windowHeight = windowHeight
            },
            methods: {
                drag_start(event){
                    this.start[0]= event.touches[0].clientX-event.target.offsetLeft;
                    this.start[1]= event.touches[0].clientY-event.target.offsetTop;
                },
                drag_hmove(event){
                        let     tag      = event.touches;
                        if(tag[0].clientX < 0 ){
                            tag[0].clientX = 0
                        }
                        if(tag[0].clientY < 0 ){
                            tag[0].clientY = 0
                        }
                        if(tag[0].clientX > this.windowWidth ){
                            tag[0].clientX = this.windowWidth
                        }
                        if(tag[0].clientY > this.windowHeight ){
                            tag[0].clientY = this.windowHeight
                        }
                        this.moveX     = tag[0].clientX-this.start[0];
                        this.moveY     = tag[0].clientY-this.start[1];
                }
            }}
    </script>
     
    <style lang="less">
        .holdon{
             100%;
            height: 100%;
        }
        .ball{
             70upx;height: 70upx;
            background:linear-gradient(to bottom, #F8F8FF,#87CEFA);
            border-radius: 50%;
            box-shadow: 0 0 15upx #87CEFA;
            color: #fff;
            font-size: 30upx;
            display: flex;justify-content: center;align-items: center;
            position: fixed !important;
            z-index: 1000000;
        }
    </style>

  • 相关阅读:
    [Bada开发]基于bada1.0的5种控件介绍[待续]
    [Bada开发]API官方学习2-风格
    [Bada开发]HelloWorld篇
    [Bada开发]初步入口函数介绍
    [Bada开发]使用共享库
    [Bada开发]使用静态库
    [Bada开发]OpenGL ES 2.0程序 创建简单3D图形
    [Bada开发]播放实时rtsp流
    剑指offer-删除链表中重复的结点
    剑指offer-构建乘积数组
  • 原文地址:https://www.cnblogs.com/lpz1096/p/12876426.html
Copyright © 2011-2022 走看看