使用uni.createSelectorQuery注意事项

     一、因为作用是获取节点上的信息,所以必须在mounted 后使用,下面是使用方法之一,或者在需要的时候获取一样的

 mounted() {
        // 获取节点信息
        this.$nextTick(() => {
            this.getSelectInfo();
        });
    }

 getSelectInfo() {
    const query = uni.createSelectorQuery().in(this);
   
    query.select('#id  / .className').boundingClientRect(data => {
      
      console.log("节点离页面顶部的距离为" + data.top);
    }).exec();

    }

二、作者使用此API场景是开发APP(下面是实际代码),避坑点。

1、代码的执行的顺序:先执行条件外的,再执行条件里面的

2、节点返回的数据

        datas:{

"height":18,

"dataset":{"v-241be720":""},

"right":383.0909118652344,

"id":"",

"bottom":45.6363639831543,

"left":10,

"width":373.0909118652344,

"top":27.636363983154297}

   getSelectInfo() {
            // #ifdef H5
            const doc = document.getElementsByClassName('select');
            this.offWidth = ((doc[0].offsetWidth+20) / 8 / 60).toFixed(2); //每分钟的距离 px
            this.countOffWidth();
            // #endif

            // #ifdef APP-PLUS
            const query = uni.createSelectorQuery().in(this);
            query
                .select('.select')
                .boundingClientRect((data) => {

                    let datas = JSON.stringify(data);
                    console.log('Gongdb--->datas',datas);
                    this.currentTime();
                    
                    this.offWidth = ((data.width-10) / 8 / 60).toFixed(2); //每分钟的距离 px
                    this.countOffWidth();
                })
                .exec();
            // #endif
            this.currentTime();
        },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值