Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

聊天界面如何实现始终保持滚动条在底部 #7

Open
kiner-tang opened this issue Feb 2, 2017 · 11 comments
Open

聊天界面如何实现始终保持滚动条在底部 #7

kiner-tang opened this issue Feb 2, 2017 · 11 comments

Comments

@kiner-tang
Copy link

kiner-tang commented Feb 2, 2017

在实时聊天的时候,我们一般都希望最新的消息在我们可视区域的最底部,那么在小程序中如果需要实现这样的功能该怎么做呢?我运行了一下demo,貌似是没有实现此功能的。
image
上图最后一条消息后面还有一条最新消息被遮挡住了

@hfhimage
Copy link

同问

@Gcaufy
Copy link
Member

Gcaufy commented May 2, 2017

https://mp.weixin.qq.com/debug/wxadoc/dev/component/scroll-view.html

理论上应该可以通过计算 scroll-top 属性实现滚动到底部,但是我没有试验过。

@hfhimage
Copy link

hfhimage commented May 3, 2017

嗯,关键是没找到计算的方法....我给 scroll-top 设置了一个比较大的值,勉强可以,但达不到小程序 “荔枝FM播客” 的效果,不知道他们怎么实现的

@Gcaufy
Copy link
Member

Gcaufy commented May 3, 2017

发消息会计算消息列表高度,动态设计scroll-top

@Gcaufy
Copy link
Member

Gcaufy commented May 3, 2017

这个属性应该也可以。

scroll-into-view	String		值应为某子元素id(id不能以数字开头),则滚动到该元素,元素顶部对齐滚动区域顶部,只支持纵向滚动

@renyufu
Copy link

renyufu commented Aug 17, 2017

从1.4.0开始,小程序提供了下面的api,可以获取到WXML的信息,包括节点高度:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/wxml-nodes-info.html#nodesreffieldsfieldscallback

@hfhimage
Copy link

这个厉害,我试试

@swit1983
Copy link

let query = wx.createSelectorQuery();
    query.select('#chatScrollView');
    query.selectAll('.chat-item').boundingClientRect(function (rects) {
        _this.setData({
          chatTop: _this.data.chatTop+rects.pop().height
        });
    }).exec();

@EasonShen1989
Copy link

怎么实现的啊??

@LIZHIBIN513
Copy link

聊天页面第一次进入页面 好想获取不到高度??只要点击进去一次 之后都可以自定滚动

@a958330481
Copy link

a958330481 commented Jan 26, 2019

利用scroll-view的scroll-into-view属性

动态给每个消息设置一个id,然后让scroll-into-view指向最新的消息id即可。

demo如下:

scroll-into-view属性值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素

<scroll-view scroll-y="true" scroll-into-view="msg{{msgs.length-1}}" scroll-with-animation="{{true}}">
    <view class="chat-msgs">
        <view class="msg-item" id="msg{{index}}" wx:for="{{msgs}}" wx:key="{{index}}">
            <view class="msg-item-con">
                {{item.content}}
            </view>
        </view>
    </view>
</scroll-view>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants