麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > HTML5 > 正文

HTML5實現(xiàn)直播間評論滾動效果的代碼

2024-09-05 07:23:31
字體:
供稿:網(wǎng)友

直播間評論滾動效果,下劃查看歷史消息并停止?jié)L動,如有新消息會出現(xiàn)新消息提醒,點擊滾動到底部。

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"動畫失效的polyfillexport 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 + "-評論內(nè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>

用的的兩個工具函數(shù)

/** * @desc 函數(shù)防抖 * @param {需要防抖的函數(shù)} func * @param {延遲時間} wait */export function debounce(func, wait = 500) {    // 緩存一個定時器id    let timer = 0;    // 這里返回的函數(shù)是每次用戶實際調(diào)用的防抖函數(shù)    // 如果已經(jīng)設(shè)定過定時器了就清空上一次的定時器    // 開始一個新的定時器,延遲執(zhí)行用戶傳入的方法    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;}

總結(jié)

到此這篇關(guān)于HTML5實現(xiàn)直播間評論滾動效果的代碼的文章就介紹到這了,更多相關(guān)H5直播間評論滾動內(nèi)容請搜索武林網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持武林網(wǎng)!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 高清视频91 | 国产成人高潮免费观看精品 | 蜜桃视频观看麻豆 | 看免费的毛片 | 天天色狠狠干 | 亚洲白嫩在线观看 | 久草在线综合 | 羞羞视频免费观看网站 | 黄色一级毛片免费看 | 欧美激情在线播放 | 精品国产91久久久久久久妲己 | 国产精品国产成人国产三级 | 日韩欧美高清一区 | 欧美另类视频在线 | 色欲香天天天综合网站 | 免费一级片观看 | 视频一区二区三区在线观看 | 国产午夜精品一区二区三区免费 | 欧美精品电影一区二区 | 草草久久久 | 欧美成人免费在线视频 | 中文字幕在线观看亚洲 | 国产精品成人免费一区久久羞羞 | 国产精品久久久久久久久久iiiii | 国产精品一区在线观看 | 国产亚洲精品久久久久婷婷瑜伽 | 成人国产免费观看 | 欧美性生交大片 | 亚洲一区二区三区视频 | 羞羞网站在线观看入口免费 | 国产99久久精品一区二区300 | 日韩视 | 久国产 | 久久亚洲精选 | 欧美成在人线a免费 | 中文字幕 亚洲一区 | 欧洲成人一区二区 | www日韩在线 | 91小视频在线观看免费版高清 | 免费看毛片的网站 | 免费看污视频在线观看 |