基于HTML5如何写直播时评论滚动的效果
Admin 2022-07-11 群英技术资讯 901 次浏览
这篇文章主要介绍“基于HTML5如何写直播时评论滚动的效果”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“基于HTML5如何写直播时评论滚动的效果”文章能帮助大家解决问题。
直播间评论滚动效果,下划查看历史消息并停止滚动,如有新消息会出现新消息提醒,点击滚动到底部。
2.具体代码
<template>
<div class="comment">
<div class="comment-wrap" ref="wrapper">
<ul class="list" ref="list">
<li v-for="item in list" :key="item.id">
<span class="name">{{item.name}}:</span>
<span class="content">{{item.content}}</span>
</li>
</ul>
</div>
<div class="rest-nums" v-show="restComment" @click="scrollBottom">{{restComment}}条新消息</div>
</div>
</template>
<script>
import smoothscroll from 'smoothscroll-polyfill';
import { debounce, isScrollBottom } from '../utils/utils';
smoothscroll.polyfill(); // 移动端scrollTo behavior: "smooth"动画失效的polyfill
export default {
data() {
return {
list: [],
restComment: 0,
restNums: 0,
wrapperDom: null,
listDom: null,
wrapperHeight: 0
};
},
mounted() {
this.initDom();
// ajax...
const data = new Array(20).fill('');
this.queue(data);
setTimeout(() => {
const list = new Array(10).fill('');
this.queue(list);
}, 30000);
},
methods: {
initDom() {
this.wrapperDom = this.$refs.wrapper;
this.listDom = this.$refs.list;
this.wrapperHeight = this.wrapperDom.offsetHeight;
},
addTimeOut(opt) {
return new Promise((resolve, reject) => {
setTimeout(() => {
this.addComment(opt);
resolve()
}, 500);
});
},
// 队列添加消息
async queue(data) {
for (let i = 0; i < data.length; i++) {
const opt = {
name: i + "-用户名",
content: i + "-评论内容",
id: Date.now()
}
await this.addTimeOut(opt);
}
},
addScroll() {
debounce(this.listScroll, 200);
this.isBindScrolled = true;
},
listScroll() {
const ele = this.wrapperDom;
const isBottom = isScrollBottom(ele, ele.clientHeight);
if (isBottom) {
this.restNums = 0;
this.restComment = 0;
}
},
// 添加评论 如果超过150条就将前50条删除
addComment(data) {
if (this.list.length >= 150) {
this.list.splice(0, 50);
}
this.list.push(data);
this.$nextTick(() => {
this.renderComment();
});
},
// 渲染评论
renderComment() {
const listHight = this.listDom.offsetHeight;
const diff = listHight - this.wrapperHeight; // 列表高度与容器高度差值
const top = this.wrapperDom.scrollTop; // 列表滚动高度
if (diff - top < 50) {
if (diff > 0) {
if (this.isBindScrolled) {
this.isBindScrolled = false;
this.wrapperDom.removeEventListener("scroll", this.addScroll);
}
this.wrapperDom.scrollTo({
top: diff + 10,
left: 0,
behavior: "smooth"
});
this.restNums = 0;
}
} else {
++this.restNums;
if (!this.isBindScrolled) {
this.isBindScrolled = true;
this.wrapperDom.addEventListener("scroll", this.addScroll);
}
}
this.restComment = this.restNums >= 99 ? "99+" : this.restNums;
},
// 滚动到底部
scrollBottom() {
this.restNums = 0; // 清除剩余消息
this.restComment = this.restNums;
this.wrapperDom.scrollTo({
top: this.listDom.offsetHeight,
left: 0,
behavior: "smooth"
});
}
}
};
</script>
<style scoped>
*{
padding: 0;
margin: 0;
}
.comment{
width: 70%;
height: 350px;
position: relative;
margin: 100px 0 0 20px;
}
.comment-wrap{
height: 350px;
overflow-y: scroll;
-webkit-overflow-scrolling:touch;
}
.comment-wrap li{
text-align: left;
line-height: 30px;
padding-left: 10px;
background: rgba(0, 0, 0, 0.3);
margin-top: 5px;
border-radius: 15px;
color: #fff;
}
.rest-nums{
position: absolute;
height: 24px;
line-height: 24px;
color: #f00;
border-radius: 15px;
padding: 0 15px;
bottom: 10px;
background: #fff;
font-size: 14px;
left: 10px;
}
</style>
用的的两个工具函数
/**
* @desc 函数防抖
* @param {需要防抖的函数} func
* @param {延迟时间} wait
*/
export function debounce(func, wait = 500) {
// 缓存一个定时器id
let timer = 0;
// 这里返回的函数是每次用户实际调用的防抖函数
// 如果已经设定过定时器了就清空上一次的定时器
// 开始一个新的定时器,延迟执行用户传入的方法
return function (...args) {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, wait)
}
}
/**
* @desc 是否滚到到容器底部
* @param {滚动容器} ele
* @param {容器高度} wrapHeight
*/
export function isScrollBottom(ele, wrapHeight, threshold = 30) {
const h1 = ele.scrollHeight - ele.scrollTop;
const h2 = wrapHeight + threshold;
const isBottom = h1 <= h2;
return isBottom;
}
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了CSS 标签显示模式的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
CSS 中的 2D 转换允许我们在二维空间中执行一些基本的变换操作,例如移动、旋转、缩放或扭曲等,变换后的元素与绝对定位的元素类似,不会影响周围的元素,但可以和周围的元素重叠,不
如何利用CSS构建粒子动画,具体怎样做?下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
本文将结合实例代码,介绍html实现弹窗的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
vue打包后js和css、图片不显示,引用的字体找不到问题;图片一般都是背景图片。一、vue打包出现js和css不显示问题:1、不使用mode:‘history’2、使用mode:‘history’手动添加:dist包不是服务器跟目录,在index.htm里手动给js和css添加dist目录即可/dist/;3、或者使用mode:‘history’修改配置:config里面的index
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008