uni-app scroll-view 子节点view 在可视区域显示1/3时 点击该view节点无法触发scroll-into-view效果

uni-app scroll-view 子节点view 在可视区域显示1/3时 点击该view节点无法触发scroll-into-view效果

测试过的手机:

Android 模拟器

操作步骤:

拖动scroll-view 让某个view在scroll-view可视区域显示1/3出来 在点击该view 无法触发scroll-into-view 效果

预期结果:

跳转到点击的view

实际结果:

没有任何的效果

bug描述:

scroll-view 中的scroll-into-view 无效 , 通过滚动scroll-view卡住某个点会导致 scroll-into-view 失效 无法跳转到指定的锚点

信息类别 详细信息
产品分类 uniapp/App
PC开发环境 Windows
PC操作系统版本 windows11
HBuilderX类型 正式
HBuilderX版本 3.3.11
手机系统 全部
手机系统版本 Android 12
手机厂商 华为
页面类型 nvue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

QQ录屏20220403001552.rar


更多关于uni-app scroll-view 子节点view 在可视区域显示1/3时 点击该view节点无法触发scroll-into-view效果的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

上传可以复现的demo,方便排查

更多关于uni-app scroll-view 子节点view 在可视区域显示1/3时 点击该view节点无法触发scroll-into-view效果的实战教程也可以访问 https://www.itying.com/category-93-b0.html


蹲,请问解决了吗

在使用 uni-appscroll-view 组件时,如果你希望在子节点 view 在可视区域显示 1/3 时点击该 view 节点能够触发 scroll-into-view 效果,但发现无法触发,可能是由于以下几个原因:

1. scroll-into-view 的使用方式

scroll-into-viewscroll-view 组件的一个属性,用于指定滚动到某个子元素的 ID。你需要确保以下几点:

  • 每个子元素都有一个唯一的 id 属性。
  • scroll-into-view 属性的值应该是一个字符串,表示你想要滚动到的子元素的 id
<scroll-view scroll-into-view="{{scrollIntoId}}" scroll-y="true" style="height: 300px;">
  <view id="item1" style="height: 100px;">Item 1</view>
  <view id="item2" style="height: 100px;">Item 2</view>
  <view id="item3" style="height: 100px;">Item 3</view>
</scroll-view>
export default {
  data() {
    return {
      scrollIntoId: ''
    }
  },
  methods: {
    scrollToItem(id) {
      this.scrollIntoId = id;
    }
  }
}

2. 点击事件的处理

你需要在点击 view 时,设置 scroll-into-view 的值为该 viewid

<scroll-view scroll-into-view="{{scrollIntoId}}" scroll-y="true" style="height: 300px;">
  <view id="item1" style="height: 100px;" @tap="scrollToItem('item1')">Item 1</view>
  <view id="item2" style="height: 100px;" @tap="scrollToItem('item2')">Item 2</view>
  <view id="item3" style="height: 100px;" @tap="scrollToItem('item3')">Item 3</view>
</scroll-view>

3. 可视区域显示 1/3 的判断

如果你希望在 view 显示 1/3 时触发滚动,可能需要通过计算 viewscroll-view 中的位置来实现。你可以使用 uni.createSelectorQuery() 来获取 view 的位置信息,然后判断是否显示 1/3。

methods: {
  scrollToItem(id) {
    const query = uni.createSelectorQuery().in(this);
    query.select(`#${id}`).boundingClientRect(data => {
      if (data && data.top < this.scrollViewHeight / 3) {
        this.scrollIntoId = id;
      }
    }).exec();
  }
}

4. scroll-view 的高度

确保 scroll-view 的高度是固定的,否则可能会导致滚动行为不符合预期。

<scroll-view scroll-into-view="{{scrollIntoId}}" scroll-y="true" style="height: 300px;">
  <!-- 子元素 -->
</scroll-view>

5. 其他可能的问题

  • 确保 scroll-viewscroll-yscroll-x 属性已经启用。
  • 确保 scroll-into-view 的值是动态绑定的,并且在点击事件中正确更新。

示例代码

以下是一个完整的示例代码:

<template>
  <view>
    <scroll-view scroll-into-view="{{scrollIntoId}}" scroll-y="true" style="height: 300px;">
      <view id="item1" style="height: 100px;" @tap="scrollToItem('item1')">Item 1</view>
      <view id="item2" style="height: 100px;" @tap="scrollToItem('item2')">Item 2</view>
      <view id="item3" style="height: 100px;" @tap="scrollToItem('item3')">Item 3</view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      scrollIntoId: '',
      scrollViewHeight: 300
    }
  },
  methods: {
    scrollToItem(id) {
      const query = uni.createSelectorQuery().in(this);
      query.select(`#${id}`).boundingClientRect(data => {
        if (data && data.top < this.scrollViewHeight / 3) {
          this.scrollIntoId = id;
        }
      }).exec();
    }
  }
}
</script>
回到顶部