需求:用户进入会话界面后,屏幕刷新到最新一条消息处
wxml文件:
<scroll-view scroll-y="true" scroll-into-view='{{toView}}' style='height: {{scrollHeight_}};'>
<view wx:for ="{{session}}">
<view id="send{{index}}"></view>
</view>
</scroll-view>
js文件:
var windowHeight = wx.getSystemInfoSync().windowHeight - 1;
var scrollHeight_ = windowHeight +"px";
.........
onLoad: function (e) {
//获取历史信息
wx.request({
success:function(res){
.........
that.setData({
session = res.data
})
that.setData({
toView = res.data.length
})
}
})
}
踩坑点:
1、id: id直接使用{{index}},导致无效; //index指for循环中数据的下标,从1开始;
2、onLoad中进行toview赋值值和数据赋值需分开setData,且数据赋值须在toView之前;
3、style:height需赋定值,单位为px。和scroll-top不同,scroll-top无需单位(如:scroll-top="100";),且scroll-to-view的优先级高于oll-top