Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信小程序createSelectorQuery踩坑 #97

Open
lizhongzhen11 opened this issue Jun 24, 2020 · 0 comments
Open

微信小程序createSelectorQuery踩坑 #97

lizhongzhen11 opened this issue Jun 24, 2020 · 0 comments

Comments

@lizhongzhen11
Copy link
Owner

抄colorui的垂直导航时踩坑了,明明完全一样的代码,为何colorui上下滚动时左侧tab会跟着变,而我上下滚动时左侧tab不动???

代码如下:

VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  }

我打印输出 view.fields 的回调参数 data 时发现为 null,这特么让我震惊了!!!看官方论坛发现有人说是id不能为数字的原因,我立马否决这个答案,因为colorui一点问题没有!

后来洗了把澡,清醒了些,意识到我的页面是 Component,而 colorui 对应的是 Page,我怀疑是这里有问题,遂去官方文档上找,果然!!!

wx.createSelectorQuery() 说明,开头就是提醒!!!

抄人家的代码不是那么简单的啊,一不小心就踩坑了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant