audio_video_img图片音视频异步可视化加载

news/2024/4/30 3:29:40

最近在做即时消息,消息类型除了文字还有音频、视频、图片展示,如果消息很多,在切换聊天框时,会有明显卡顿,后续做了懒加载,方案是只加载用户能看到的资源,看不到的先不加载;

LazyAudio.tsx
import {useRef, useEffect} from 'react';interface PropsType {url: string,className?: string,
}export default function LazyAudio({url, className= ''}: PropsType) {const currentRef: any = useRef(null)useEffect(() => {if (!currentRef.current) {return;}const observer = new IntersectionObserver(([{ isIntersecting }]) => {// 表示进入了可视范围if (isIntersecting) {if (!currentRef.current.src) {currentRef.current.src = url;observer.unobserve(currentRef.current);}}});observer.observe(currentRef.current);return () => {if (currentRef.current) {observer.unobserve(currentRef.current);}}}, [])return (<><audioref={currentRef}controls preload="metadata" className={`${className} `}></audio></>)
}
LazyImg.tsx
import {useEffect, useRef} from 'react';interface PropsType {url: string,className?: string,onClick: () => void,
}
export default function LazyImg({url, className = '', onClick}: PropsType) {const currentRef: any = useRef(null)// 图片加载完成后const imageLoaded = () => {// 图片加载完成后重置了高度currentRef.current.style.height = 'auto';}useEffect(() => {if (!currentRef.current) {return;}const observer = new IntersectionObserver(([{ isIntersecting }]) => {// 表示进入了可视范围if (isIntersecting) {if (!currentRef.current.src) {currentRef.current.src = url;observer.unobserve(currentRef.current);}}});observer.observe(currentRef.current);return () => {if (currentRef.current) {observer.unobserve(currentRef.current);}}}, [])return <><img alt="无法加载图片"width={320}height={320}ref={currentRef}onClick={onClick}onLoad={imageLoaded}className={`${className} `}/></>
}
LazyVideo.tsx
import {useEffect, useRef} from 'react';interface PropsType {url: string,className?: string,width?: number,height?: number,
}
export default function LazyVideo({url, className = '', width = 320, height = 240}: PropsType) {const currentRef: any = useRef(null)useEffect(() => {if (!currentRef.current) {return;}const observer = new IntersectionObserver(([{ isIntersecting }]) => {// 表示进入了可视范围if (isIntersecting) {if (!currentRef.current.src) {currentRef.current.src = url;observer.unobserve(currentRef.current);}}});observer.observe(currentRef.current);return () => {if (currentRef.current) {observer.unobserve(currentRef.current);}}}, [])return (<><video ref={currentRef}width={width}height={height}controls preload="metadata"className={`${className} `}></video></>)
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/11778.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

【stm32】I2C通信协议

【stm32】I2C通信协议 概念及原理 如果我们想要读写寄存器来控制硬件电路&#xff0c;就至少需要定义两个字节数据 一个字节是我们要读写哪个寄存器&#xff0c;也就是指定寄存器的地址 另一个字节就是这个地址下存储寄存器的内容 写入内容就是控制电路&#xff0c;读出内容就…

ChatGPT 初学者指南

原文&#xff1a;ChatGPT for Beginners 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 介绍 如果您一直关注新闻和趋势&#xff0c;您可能已经在某个地方读到或听到过&#xff0c;Sam Altman 的生成式人工智能平台 ChatGPT 已经将人工智能推向了一个新的高度 - 许多…

Topaz Video AI for Mac v5.0.0激活版 视频画质增强软件

Topaz Video AI for Mac是一款功能强大的视频处理软件&#xff0c;专为Mac用户设计&#xff0c;旨在通过人工智能技术为视频编辑和增强提供卓越的功能。这款软件利用先进的算法和深度学习技术&#xff0c;能够自动识别和分析视频中的各个元素&#xff0c;并进行智能修复和增强&…

基于springboot实现影城管理系统项目【项目源码+论文说明】

基于springboot实现影城管理系统演示 摘要 随着现在网络的快速发展&#xff0c;网上管理系统也逐渐快速发展起来&#xff0c;网上管理模式很快融入到了许多生活之中&#xff0c;随之就产生了“小徐影城管理系统”&#xff0c;这样就让小徐影城管理系统更加方便简单。 对于本小…

FreeRTOS学习 -- 再识

工作中一直使用FreeRTOS进行着开发&#xff0c;但是没有进行过系统的总结过。现在将快速使用几天时间将FreeRTOS相关知识点加以总结。 官网&#xff1a; https://www.freertos.org/zh-cn-cmn-s/ 参看资料&#xff1a; 正点原子 STM32F1 FreeRTOS开发手册_V1.2.pdf The FreeRTOS…

linuxday05

1、makedile原理&#xff08;增量编译生成代码&#xff09; # &#xff08;注释符&#xff09; 目标------依赖 目标不存在//目标比依赖旧才会执行命令&#xff1b; makefile的实现 1、命名要求&#xff08;Makefile/makefile&#xff09; 2、规则的集合 目标文件&#…