好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > HTML5實現直播間評論滾動效果的代碼

HTML5實現直播間評論滾動效果的代碼

熱門標簽:如何獲取地圖標注客戶 拓展地圖標注 只辦理400電話 電話機器人黑斑馬免費 電話機器人電銷系統掙話費 平涼地圖標注位置怎么弄 高德地圖標注地點糾錯 機器人外呼系統存在哪些能力 南昌仁和怎么申請開通400電話

直播間評論滾動效果,下劃查看歷史消息并停止滾動,如有新消息會出現新消息提醒,點擊滾動到底部。

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直播間評論滾動內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持腳本之家!

標簽:西藏 永州 青島 遼源 漯河 棗莊 池州 新疆

巨人網絡通訊聲明:本文標題《HTML5實現直播間評論滾動效果的代碼》,本文關鍵詞  HTML5,實現,直播間,評論,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《HTML5實現直播間評論滾動效果的代碼》相關的同類信息!
  • 本頁收集關于HTML5實現直播間評論滾動效果的代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 成年性香蕉漫画在线观看| 国产高潮国产高潮久久久m3u8 | 电影《強姦》田中裕子| 天天躁日日躁狠狠躁中文字幕| 肉嫁高柳家在线播放| 中国xxxx视频播放免费| free欧美性杂交hd| 吃奶摸下激烈 免费视频大全| 男女一起差差差很痛| 女人18毛片水最多免费观看| 揉我胸?啊?嗯~出水奶| 无套挺进少妇私下处内射| 午夜写真福利在线播放| 99国产精品成人AV片免费看| 欧美午夜色视频国产精品| 台湾一级淫片完整版视频播放 | 尤物.com| 免费激情网址| 蜜桃精产品一区一区三区| 欧美疯狂做受xxxx| 老师洗澡时让我进去摸她的胸 | 无码在线播放| 春色乱小说伦校园短篇| 草莓丝瓜榴莲秋葵污无限看特色 | 毛片免费播放| ass新版亚洲热妇毛耸耸的pics| 亚洲老熟女毛茸茸XXX| 亚洲依依成人综合网站| 少妇老师婬荡呻吟A片迅雷种子| 成人免费看黄| 国产精品尹人在线观看免费| 白嫩的18sex少妇HD| 男女被?到爽??流水电影| 欧美男男在线| 国产女人下面好多水| 成人黄色免费观看| 国产精品美女网站| 97琪琪电影院| 国产乱肥老妇精品视频| 欧美亚洲国产精品久久第一页| 男人天堂影院|