html5代码之翻页(HTML5实现直播间评论滚动效果的代码)
类别:Web前端 浏览量:2188
时间:2021-10-16 00:11:38 html5代码之翻页
HTML5实现直播间评论滚动效果的代码直播间评论滚动效果,下划查看历史消息并停止滚动,如有新消息会出现新消息提醒,点击滚动到底部。
2.具体代码
<template> <li class="comment"> <li 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> </li> <li class="rest-nums" v-show="restComment" @click="scrollBottom">{{restComment}}条新消息</li> </li> </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直播间评论滚动内容请搜索开心学习网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持开心学习网!
标签:
您可能感兴趣
- 怎么调input搜索框边框颜色(使用placeholder属性设置input文本框的提示信息)
- python从零开始学习人工智能(为何人工智能AI首选Python?读完这篇文章你就知道了推荐)
- python3web编程基础教程(python初学者,用python实现基本的学生管理系统python3代码实例)
- Jquery中parent()和parents()
- pyqt复选框样式(基于PyQt4和PySide实现输入对话框效果)
- C#enum枚举
- 什么是mongodb 固定集合
- nginx怎么设置域名访问网页(Nginx服务器如何设置url链接)
- centos怎样开启tomcat(Centos8.2云服务器环境安装Tomcat8.5的详细教程)
- Mysql官方性能测试工具mysqlslap的使用简介(Mysql官方性能测试工具mysqlslap的使用简介)
- idea配置tomcat启动web项目(如何在IntelliJ IDEA 2018上配置Tomcat并运行第一个JavaWeb项目)
- css如何设置简写代码(10个CSS简写/优化技巧整理)
- pythonlist类型教程(Python List cmp知识点总结)
- Asp.net页面传值乱码
- 云服务器实例和配置(运维必须知道的关于云服务器的十个问题)
- python numpy 安装(python3.6下Numpy库下载与安装图文教程)
- 这竟然是捏出来的 20种橡皮泥玩法让你轻松hold住魔娃(这竟然是捏出来的)
- 自制橡皮泥(自制橡皮泥)
- 还在卖 禁药西布曲明网上论斤卖(还在卖禁药西布曲明网上论斤卖)
- 微商在朋友圈热卖的 DL减肥咖啡 含违禁药物,你还敢买吗(微商在朋友圈热卖的)
- 八一节,说说中国女兵(八一节说说中国女兵)
- 王治郅菜鸟赛季已让八一带入正轨,大郅七大经典语录或是成功秘诀(王治郅菜鸟赛季已让八一带入正轨)
热门推荐
- laravel 快速开发api技巧(Laravel5.4简单实现app接口Api Token认证方法)
- eval解析json字符串
- python矩阵怎么生成(python实现矩阵打印)
- 云服务器如何进入(云服务器配置怎么看?)
- 使用Console命令调试JS
- 让文字居中代码是多少(如何使定义了高度和宽度的< a >里的文字垂直居中实现代码)
- set statistics time on的理解
- 小白vue教学(尤大大新活petite-vue的实现)
- dedecms使用安装(使用dedecms搭建自己的本地网站全程图解)
- mysql常用数据模型(MySQL数据库基于sysbench实现OLTP基准测试)