溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在html5中如何設(shè)置直播間評(píng)論滾動(dòng)

發(fā)布時(shí)間:2022-02-22 14:18:54 來(lái)源:億速云 閱讀:127 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹在html5中如何設(shè)置直播間評(píng)論滾動(dòng),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

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

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(); // 移動(dòng)端scrollTo behavior: "smooth"動(dòng)畫失效的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);
    	   });
       },	// 隊(duì)列添加消息
	async queue(data) {    for (let i = 0; i < data.length; i++) {    		const opt = {
    			name: i + "-用戶名",
    			content: i + "-評(píng)論內(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;
            }
        },	// 添加評(píng)論 如果超過(guò)150條就將前50條刪除addComment(data) {if (this.list.length >= 150) {this.list.splice(0, 50);
            }this.list.push(data);this.$nextTick(() => {		this.renderComment();
	    });
	},	// 渲染評(píng)論renderComment() {const listHight = this.listDom.offsetHeight;const diff = listHight - this.wrapperHeight; // 列表高度與容器高度差值const top = this.wrapperDom.scrollTop; // 列表滾動(dòng)高度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;
    	},	// 滾動(dòng)到底部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>

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

/**
 * @desc 函數(shù)防抖
 * @param {需要防抖的函數(shù)} func
 * @param {延遲時(shí)間} wait
 */export function debounce(func, wait = 500) {// 緩存一個(gè)定時(shí)器idlet timer = 0;// 這里返回的函數(shù)是每次用戶實(shí)際調(diào)用的防抖函數(shù)// 如果已經(jīng)設(shè)定過(guò)定時(shí)器了就清空上一次的定時(shí)器// 開始一個(gè)新的定時(shí)器,延遲執(zhí)行用戶傳入的方法return function (...args) {if (timer) clearTimeout(timer)
    	timer = setTimeout(() => {
    		func.apply(this, args)
    	}, wait)
    }
}/**
 * @desc 是否滾到到容器底部
 * @param {滾動(dòng)容器} ele 
 * @param {容器高度} wrapHeight 
 */export function isScrollBottom(ele, wrapHeight, threshold = 30) {const h2 = ele.scrollHeight - ele.scrollTop;const h3 = wrapHeight + threshold;const isBottom = h2 <= h3;return isBottom;
}

以上是“在html5中如何設(shè)置直播間評(píng)論滾動(dòng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI