uni-app nvue list组件使用dom.scrollToElement效果错误
uni-app nvue list组件使用dom.scrollToElement效果错误
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Mac | 10.15.3 (19D76) | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Mac
PC开发环境操作系统版本号:10.15.3 (19D76)
HBuilderX类型:正式
HBuilderX版本号:3.1.22
手机系统:Android
手机系统版本号:Android 11
手机厂商:小米
手机机型:Redmi Note9 Pro
页面类型:nvue
打包方式:云端
### 示例代码:
```vue
<template>
<view class="wrapper">
<list v-if="isList" class="scroller">
<cell v-for="(name, index) in rows" :key="index" :ref="'item' + index">
<view class="row">
<text class="text" :ref="'text' + index">{{ name }}</text>
</view>
</cell>
</list>
<scroller v-else class="scroller">
<template v-for="(name, index) in rows">
<view class="row" :key="index" :ref="'item' + index">
<text class="text" :ref="'text' + index">{{ name }}</text>
</view>
</template>
</scroller>
<view class="group">
<text>是否List组件</text>
<switch :checked="isList" @change="changeList" />
</view>
<view class="group">
<text @click="goto10" class="button">Go to 10</text>
<text @click="goto20" class="button">Go to 20</text>
</view>
</view>
</template>
<script>
const dom = uni.requireNativePlugin('dom');
export default {
data() {
return {
isList: false,
rows: []
};
},
created() {
for (let i = 0; i < 30; i++) {
this.rows.push('row ' + i);
}
},
methods: {
changeList() {
this.isList = !this.isList;
},
goto10(count) {
const el = this.$refs.item10[0];
dom.scrollToElement(el, {});
},
goto20(count) {
const el = this.$refs.item20[0];
dom.scrollToElement(el, { offset: 0 });
}
}
};
</script>
<style scoped>
.scroller {
width: 700rpx;
height: 700rpx;
border-width: 3rpx;
border-style: solid;
border-color: rgb(162, 217, 192);
margin-left: 25rpx;
}
.row {
height: 100rpx;
flex-direction: column;
justify-content: center;
padding-left: 30rpx;
border-bottom-width: 2rpx;
border-bottom-style: solid;
border-bottom-color: #dddddd;
}
.text {
font-size: 45rpx;
color: #666666;
}
.group {
flex-direction: row;
justify-content: center;
margin-top: 60rpx;
}
.button {
width: 200rpx;
padding-top: 20rpx;
padding-bottom: 20rpx;
font-size: 40rpx;
margin-left: 30rpx;
margin-right: 30rpx;
text-align: center;
color: #41b883;
border-width: 2rpx;
border-style: solid;
border-color: rgb(162, 217, 192);
background-color: rgba(162, 217, 192, 0.2);
}
</style>
操作步骤:
- 运行示例代码
- 点击【Go to 10】、【Go to 20】
- 切换【是否List组件】,继续步骤2
预期结果:
list或scroller使用dom.scrollToElement能滚动到指定位置
实际结果:
切换到list组件,测试dom.scrollToElement,发现list组件并未滚动到指定位置
bug描述:
nvue中使用dom.scrollToElement,可以作用list和scroller组件,测试发现2个组件效果不一致
scroller 正常
list 不正常
更多关于uni-app nvue list组件使用dom.scrollToElement效果错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html
请问 问题解决了吗 同样的问题。
更多关于uni-app nvue list组件使用dom.scrollToElement效果错误的实战教程也可以访问 https://www.itying.com/category-93-b0.html
没呀,官方还没看
官方有说明 ref 不能用index, 你加个ID什么的试一下呢
是呀,我用的是 ‘item’ + index
回复 码农朱哲: 我现在是this.$refs.xxx无法获得,提示undefined
在nvue中,list组件和scroller组件虽然都支持滚动,但它们的实现机制不同,这导致了dom.scrollToElement行为不一致。
list组件是复用型列表,采用cell回收机制,并非所有元素都在DOM中实时渲染。而dom.scrollToElement需要操作实际渲染的DOM节点,当目标cell被回收时,该方法可能无法正确定位。
对于list组件,推荐使用其内置的滚动方法:
// 获取list组件实例
const list = this.$refs.listRef
// 滚动到指定位置
list.scrollToElement(this.$refs.item10[0])

