1.使用 scroll-view 标签的属性:scroll-top(距离值 num) 或 scroll-into-view(子元素的id,不能以数字开头 string)
<scroll-view
:scroll-top="scrollTop"
:scroll-into-view="toviews"
scroll-y
scroll-with-animation
:style="{ height: screenHeight + 'px' }"
>
2.scroll-into-view的值为子元素的id,不能以数字开头
子元素:
<view
class="item-essay"
:id="`s${options.id}`"
v-for="options in prenatalList"
:key="options.id"
>
3.遇到的问题
-
数据是否准确:确保
scroll-into-view
中的值确实存在于scroll-view
内,以及其格式是否正确。我使用了 `s${options.id}`
,这个值是根据options.id
动态生成的。确保这个值是唯一的,并且与scroll-view
内的某个子元素的id
匹配。 -
生命周期问题:
scroll-view
的scroll-into-view
属性需要在scroll-view
组件已经渲染完毕后才会生效。确保在页面或组件的生命周期中设置scroll-into-view
属性时,scroll-view
已经被渲染出来了。所以就需要等scroll-view中的数据prenatalList 加载完成再设置 toviews的值 -
异步加载问题:如果
scroll-view
或子元素是在异步加载数据后添加到页面的,确保在数据加载完成后设置scroll-into-view
属性。可以使用异步settimeout或者nexttick