当前位置: 首页 > news >正文

德州哪里有学做网站的/佛山网站定制

德州哪里有学做网站的,佛山网站定制,杭州做网站公司,山东省建设厅教育网站这篇文章主要介绍了HTML5实现直播间评论滚动效果的代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下直播间评论滚动效果,下划查看历史消息并停止滚动,如有新消息会出现…

这篇文章主要介绍了HTML5实现直播间评论滚动效果的代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

219514419_1_20210407121620816.gif

直播间评论滚动效果,下划查看历史消息并停止滚动,如有新消息会出现新消息提醒,点击滚动到底部。

2.具体代码

  • {{item.name}}:

    {{item.content}}

{{restComment}}条新消息

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"

});

}

}

};

*{

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;

}

用的的两个工具函数

/**

* @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实现直播间评论滚动效果的代码的文章就介绍到这了

http://www.lbrq.cn/news/932707.html

相关文章:

  • wordpress更改链接后网站打不开/2023北京封控了
  • 商务网站建设流程/深圳网络推广服务公司
  • 网站怎么做自然优化/前端seo是什么意思
  • 云南做网站哪家便宜/微信广告平台推广
  • zencart外贸网站建设/网络营销创意案例
  • 网站建设不要摸板/关键词查询爱站网
  • 广州网站建设app开发/百度站长工具seo
  • 网站建设 投标书/怎么提交百度收录
  • 网上网站怎么做/seo测试工具
  • 新疆乌鲁木齐新闻头条/关键词排名关键词优化
  • 免费软件下载app/中国seo排行榜
  • 怎么做狼视听网站/快速排名优化推广价格
  • 网站建设与管理实践实践报告/手机推广软文
  • wordpress卡蜜/谷歌seo快速排名优化方法
  • 银行外包公司面试骗局/青岛网站制作seo
  • 网站开发工程师专业好不好/广东广州疫情最新情况
  • 公司网站建设济南兴田德润厉害吗/百度广告点击软件
  • 怎么做自己的导航网站/徐州网站设计
  • 订餐网站开发流程/湖南靠谱seo优化
  • 专业做网站的公司保定/企业培训课程名称大全
  • 网站认证主体/网络公司网页设计
  • p2p网站建设多少钱/网络营销的真实案例分析
  • 怎么做门户网站设计方案/网站营销推广有哪些
  • 山东省住房城乡建设部网站/怎么建立企业网站
  • 百度收录网站之后又怎么做/潍坊关键词优化平台
  • 网站开发的热门博客/友情链接检索数据分析
  • 用wordpress建公司网站步骤/广告招商
  • 百度给做网站收费多少/今日新闻国际最新消息
  • 上海网站建设微信开发/重庆seo招聘
  • 做网站交易平台挣钱吗/东莞seo托管
  • Linux中Docker Swarm介绍和使用
  • 国产开源大模型崛起:使用Kimi K2/Qwen2/GLM-4.5搭建编程助手
  • 24黑马SpringCloud安装MybatisPlus插件相关问题解决
  • SpringBoot与Rust实战指南
  • 软件性能优化:善用80-20法则,精准突破瓶颈
  • 前端图片懒加载的深度指南:从理论到实战