HTML5实现直播间评论滚动效果的代码


Posted in HTML / CSS onMay 27, 2020

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;
}

总结

到此这篇关于HTML5实现直播间评论滚动效果的代码的文章就介绍到这了,更多相关H5直播间评论滚动内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
利用CSS3 动画 绘画 圆形动态时钟
Mar 20 HTML / CSS
css3 实现滚动条美化效果的实例代码
Jan 06 HTML / CSS
html5/css3响应式页面开发总结
Oct 16 HTML / CSS
HTML5 解析规则分析
Aug 14 HTML / CSS
html5中地理位置定位api接口开发应用小结
Jan 04 HTML / CSS
Html5实现如何在两个div元素之间拖放图像
Mar 29 HTML / CSS
html5的画布canvas——画出简单的矩形、三角形实例代码
Jun 09 HTML / CSS
html5 视频播放解决方案
Nov 06 HTML / CSS
html5 datalist 选中option选项后的触发事件
Mar 05 HTML / CSS
amaze ui 的使用详细教程
Aug 19 HTML / CSS
h5封装下拉刷新
Aug 25 HTML / CSS
CSS元素定位之通过元素的标签或者元素的id、class属性定位详解
Sep 23 HTML / CSS
html5拖拽应用记录及注意点
May 27 #HTML / CSS
recorder.js 基于Html5录音功能的实现
May 26 #HTML / CSS
HTML5+CSS设置浮动却没有动反而在中间且错行的问题
May 26 #HTML / CSS
H5 video poster属性设置视频封面的方法
May 25 #HTML / CSS
html5中嵌入视频自动播放的问题解决
May 25 #HTML / CSS
HTML5 FileReader对象的具体使用方法
May 22 #HTML / CSS
HTML5 Blob对象的具体使用
May 22 #HTML / CSS
You might like
PHP 多进程 解决难题
2009/06/22 PHP
PHP中通过语义URL防止网站被攻击的方法分享
2011/09/08 PHP
apache配置虚拟主机的方法详解
2013/06/17 PHP
浅析php中如何在有限的内存中读取大文件
2013/07/02 PHP
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
2014/01/19 PHP
php中有关合并某一字段键值相同的数组合并的改进
2015/03/10 PHP
WordPress中用于创建以及获取侧边栏的PHP函数讲解
2015/12/29 PHP
PHP编程一定要改掉的5个不良习惯
2020/09/18 PHP
地震发生中逃生十大法则
2008/05/12 Javascript
asp 取文本框名称代码
2008/12/02 Javascript
JavaScript 继承的实现
2009/07/09 Javascript
ExtJS4 表格的嵌套 rowExpander应用
2014/05/02 Javascript
js函数内变量的作用域分析
2015/01/12 Javascript
深入分析JSON编码格式提交表单数据
2015/06/25 Javascript
详解JavaScript的回调函数
2015/11/20 Javascript
angularjs数组判断是否含有某个元素的实例
2018/02/27 Javascript
详解webpack 热更新优化
2018/09/13 Javascript
vue实现移动端省市区选择
2019/09/27 Javascript
微信小程序吸底区域适配iPhoneX的实现
2020/04/09 Javascript
[04:40]DOTA2-DPC中国联赛1月26日Recap集锦
2021/03/11 DOTA
Android应用开发中Action bar编写的入门教程
2016/02/26 Python
Python中的if、else、elif语句用法简明讲解
2016/03/11 Python
python 日期排序的实例代码
2019/07/11 Python
django 中的聚合函数,分组函数,F 查询,Q查询
2019/07/25 Python
Python中sorted()排序与字母大小写的问题
2020/01/14 Python
Python实现树莓派摄像头持续录像并传送到主机的步骤
2020/11/30 Python
matplotlib交互式数据光标实现(mplcursors)
2021/01/13 Python
暑期培训随笔感言
2014/03/10 职场文书
开业典礼主持词
2014/03/21 职场文书
竞选体育委员演讲稿
2014/04/26 职场文书
五一活动标语
2014/06/30 职场文书
护士旷工检讨书
2015/08/15 职场文书
党章学习心得体会2016
2016/01/14 职场文书
教你解决往mysql数据库中存入汉字报错的方法
2021/05/06 MySQL
浅谈MySQL之浅入深出页原理
2021/06/23 MySQL
mysql全面解析json/数组
2022/07/07 MySQL