问题
scroll-view中scroll-into-view的值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素。
但这只能保证元素显示出来,不保证元素完全显示出来,每次滚到到元素内部的位置高度是随机的。
解决方法
为了使元素的顶部显示出来,可以在元素外层包一个view,然后顶部给一个空的view当定位
如图:
部分代码:
<scroll-view :scroll-into-view="scrollName" :scroll-y="true" :show-scrollbar="false" ref="scrollView" class="flex-1">
<view v-for="(item,index) in foodList" :key="item.id" style="position: relative;"> //外层view
<view :id="`food${item.id}`" style="position: absolute;top: -80rpx;"></view> //用来定位的空view
<view></view> //元素
</view>
</scroll-view>
通过scrollName定位空view的id,就可以实现使元素的顶部显示出来,即完整显示这个元素。