vue3长列表优化,使用vue-virtual-scroller实现直播间弹幕列表虚拟滚动效果

使用的组件库是:https://github.com/Akryum/vue-virtual-scroller 

官方文档:vue-virtual-scroller

安装依赖

npm install --save vue-virtual-scroller@next


pnpm install --save vue-virtual-scroller@next


yarn add vue-virtual-scroller@next

组件导入

在main.ts中导入组件,这个依赖库支持RecycleScroller,DynamicScroller,DynamicScrollerItem三个组件,可以全量导入,也可以部分导入。这三个组件区别是滚动每一项高度是固定的还是动态的,Recycle就是固定的高度,Dynamic是动态的,动态的话,必须要包含DynamicScrollerItem。

import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
import VirtualScroller from 'vue-virtual-scroller'


app.use(VirtualScroller)

使用试试

使用固定高度的RecycleScroller组件:

<template>
  <RecycleScroller
    class="scroller"
    :items="list"
    :item-size="32"
    key-field="id"
    v-slot="{ item }"
  >
    <div class="user">
      {{ item.name }}
    </div>
  </RecycleScroller>
</template>

<script>
export default {
  props: {
    list: Array,
  },
}
</script>

<style scoped>
.scroller {
  height: 100%;
}

.user {
  height: 32%;
  padding: 0 12px;
  display: flex;
  align-items: center;
}
</style>

使用动态高度的DynamicScroller组件:

注意事项:要结合DynamicScrollerItem一起使用,并且要加上active属性才可以,不然会有警告。

<DynamicScroller
                :items="messageList"
                :min-item-size="32"
                class="liveMeg"
                id="liveMsg"
                ref="liveMsg"
                v-if="messageList.length"
            >
                <template v-slot="{ item, active }">
                    <DynamicScrollerItem
                        :item="item"
                        :active="active"
                        class="msgBox"
                        :size-dependencies="[item.name, item.msg]"
                        :data-index="item.id"
                    >
                        <div class="content">
                            <span class="name">{{ item.name }}:</span>
                            <span class="msg">{{ item.msg }}</span>
                        </div>
                    </DynamicScrollerItem>
                </template>
            </DynamicScroller>


css样式:
.liveMeg {
            flex: 1;
            margin-left: 10px;
            background-color: #252632;
            border-radius: 10px;
            box-shadow: 0 0 10px 2px gray;
            scrollbar-color: #363741 transparent;
            scrollbar-width: thin;
            overflow-y: scroll;

            .msgBox {
                display: flex;
                flex-direction: row;
                padding: 5px;
                white-space: wrap;

                .name {
                    color: #8ce7ff;
                    margin-right: 2px;
                    white-space: nowrap;
                }

                .msg {
                    color: white;
                    white-space: wrap;
                }
            }
        }

没使用之前页面会渲染超级多个div元素,但是使用这个虚拟列表之后,就只会渲染在视窗里面的元素:页面中元素数量一直就是这多,超过了就不会渲染出来

自动滚动到底部

想要让消息内容自动滚动到底部,适用于直播间弹幕消息或者聊天消息等结构,可以尝试使用这种方式。但是需要注意:获取DynamicScroller这个组件的时候,要使用document.getElementById('liveMsg')这种形式,不能使用ref这种,因为ref获取到的并不是一个html元素,没有scrollTop方法,所以无法滚动到底部。

// 滚动盒子到底部
    if (liveMsg.value) {
        const msgDom: HTMLElement | null = document.getElementById('liveMsg')
        console.log('liveMsg.value--', msgDom)
        if (msgDom) {
            msgDom.scrollTop = msgDom.scrollHeight
        }
    }

我这就是在收到message消息的时候就执行这个滚动的方法:

最后实现的抖音直播间的弹幕滚动效果:

开源地址:GitHub - Sjj1024/LiveBox: livebox

  • 30
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024小神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值