单击事件-长按时间:手搓播放器

news/2025/3/4 17:10:23/文章来源:https://www.cnblogs.com/Simoon/p/18750888

事件分析

事件区分核心:使用onMouseDownonMouseUp和定时器来区分单击事件与长按事件

按下时设置长按事件定时器(并加入长按回调),并记录当前时间戳,

松开事件中如果当前时间戳差值小于定时器则执行单击回调,并清除定时器,如果大于定时器事件,则按下事件中定时器的长按回调已经执行,事件末清除以上定时器

 const mouseDown = (callBack) => {mouseTime.current = Date.now();clearInterval(timer.current);timer.current = null;setPlaying(false);funcTimer.current.timeOutTimer = setTimeout(() => {funcTimer.current.intervalTimer = setInterval(() => {callBack();}, 100);}, 480);};const mouseUp = (callBack) => {const funcType = Date.now() - mouseTime.current >= 500;if (funcType) {clearInterval(funcTimer.current.intervalTimer);} else {clearTimeout(funcTimer.current.timeOutTimer);callBack();}funcTimer.current = {};mouseTime.current = null;};

手搓播放器代码

import React, { useEffect, useRef, useState } from 'react';
import { Slider, Button } from 'antd';
import { PlayCircleOutlined, PauseCircleOutlined, BackwardOutlined, ForwardOutlined, XFilled } from '@ant-design/icons';const data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
];
function Index() {const [pointIndex, setPointIndex] = useState(0);const [playing, setPlaying] = useState(false);const timer = useRef(null);const mouseTime = useRef(null);const funcTimer = useRef({});function formatter(value) {return `${value}%`;}const play = () => {if (playing) {setPlaying(false);clearInterval(timer.current);timer.current = null;} else {setPlaying(true);timer.current = setInterval(() => {setPointIndex((c) => {if (c < data?.length - 1) {return c + 1;} else {setPlaying(false);clearInterval(timer.current);timer.current = null;return 0;}});}, 200);}};const mouseDown = (callBack) => {mouseTime.current = Date.now();clearInterval(timer.current);timer.current = null;setPlaying(false);funcTimer.current.timeOutTimer = setTimeout(() => {funcTimer.current.intervalTimer = setInterval(() => {callBack();}, 100);}, 480);};const mouseUp = (callBack) => {const funcType = Date.now() - mouseTime.current >= 500;if (funcType) {clearInterval(funcTimer.current.intervalTimer);} else {clearTimeout(funcTimer.current.timeOutTimer);callBack();}funcTimer.current = {};mouseTime.current = null;};const forwardFunc = () => {pointIndex !== data?.length && setPointIndex((c) => c + 1);};const backFunc = () => {pointIndex !== 0 && setPointIndex((c) => c - 1);};const reset = () => {setPointIndex(0);setPlaying(false);clearInterval(timer.current);timer.current = null;};return (<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh', width: '100%' }}><div style={{ width: '700px', display: 'flex' }}><Buttonicon={<BackwardOutlined />}onMouseDown={() => mouseDown(backFunc)}onMouseUp={() => mouseUp(backFunc)}/><Button icon={playing ? <PauseCircleOutlined /> : <PlayCircleOutlined />} onClick={play} /><Buttonicon={<ForwardOutlined />}onMouseDown={() => mouseDown(forwardFunc)}onMouseUp={() => mouseUp(forwardFunc)}/><Button icon={<XFilled style={{ fontSize: 12 }} />} onClick={reset} /><Slider// tipFormatter={formatter}style={{ width: '600px' }}value={pointIndex}max={data?.length - 1}onChange={(e) => {setPlaying(false);clearInterval(timer.current);timer.current = null;setPointIndex(e);}}/></div></div>);
}export default Index;

 

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux用户登录超时设置

1、引言在Linux系统中,用户登录超时设置通常用于增强系统的安全性,防止未经授权的长时间访问。合理的超时设置不仅可以提高系统的安全性,还可以优化用户体验。本文将探讨如何设置Linux终端登录超时,并提出一些优化策略,以确保系统资源得到有效利用,同时降低潜在的安全风险…

构建城市租房时空感知与智能决策平台

在城市化进程不断加速的今天,租房市场作为城市生活的重要组成部分,其健康发展对城市的稳定与繁荣至关重要。“城市租房时空感知与智能决策平台”的建设将利用测绘地理信息技术,为租房市场提供一个全新的视角和决策支持系统。 一、平台建设背景随着城市人口的增长和流动性的增…

从文件到块: 提高 Hugging Face 存储效率

Hugging Face 在 Git LFS 仓库 中存储了超过 30 PB 的模型、数据集和 Spaces。由于 Git 在文件级别进行存储和版本控制,任何文件的修改都需要重新上传整个文件。这在 Hub 上会产生高昂的成本,因为平均每个 Parquet 和 CSV 文件大小在 200-300 MB 之间,Safetensor 文件约 1 G…

Web3 操作系统大揭秘:通用功能打造高效 Web3 生态

在区块链技术飞速发展的今天,RuleOS 作为一款先进的 Web3 操作系统,正逐渐崭露头角,成为推在区块链技术飞速发展的今天,RuleOS 作为一款先进的 Web3 操作系统,正逐渐崭露头角,成为推动行业迈向新高度的关键力量。它以其独特的模块化、紧耦合设计,实现了 Web3 通用应用的…

遥感影像人机协同智能解译系统

在当今信息化时代,遥感技术作为获取地理空间信息的重要手段,其应用范围已经覆盖了农业、林业、城市规划、环境监测等多个领域。随着遥感数据量的激增和人工智能技术的发展,传统的人工解译方式已经难以满足高效率、高精度的需求。因此,遥感影像人机协同智能解译系统的建设显…

Web3世界的乐高工厂:RuleOS如何用插件化生态拼出无限可能?

在数字世界日新月异的今天,Web3作为互联网的新范式正在重塑我们与数字空间的互动方式。在这个去中心化在数字世界日新月异的今天,Web3作为互联网的新范式正在重塑我们与数字空间的互动方式。在这个去中心化的新领域中,一个名为RuleOS的创新平台正如同"乐高工厂"一…

从 0 到 1 搭建 Web3 应用,RuleOS 做对了什么?

在科技飞速发展的当下,区块链技术的兴起为我们带来了全新的变革。其中,Web3 作为区块链技术的重要应用方向,正逐渐走进人们的视野。而在 Web3 的发展进程中,RuleOS 这款创新的 Web3 操作系统脱颖而出,为从 0 到 1 搭建 Web3 应用提供了强大的支持。那么,RuleOS 究竟做对了…

redis基础数据结构——dict

关于哈希: 哈希表是一种提高效率的数据结构,每当我们插入元素时,会根据key的hash值来决定他进入表的哪一槽位。为了保证均匀分布数据,有了下述公式index = hash(key) & (size - 1)在一般情况下,size要求为2的幂。因为原本的公式是hash(key)%size。而使用&的优势是…

No.31 JavaScript--数组

一、定义数组(array)是按次序排列的一组值。每个值的位置都有编号(从0开始),整个数组用方括号表示。 两端的方括号是数组的标志。 除了在定义时赋值,数组也可以先定义后赋值。<script>var name = [];name[0] = "m"</script>任何类型的数据,都可以放入…

c# 托管堆和垃圾回收

前言 我们已经理解了clr可以寄宿,然后宿主可以控制了,也就是说外部问题我们已经解决了,那么还有一件重要的事情。 那就是clr 是如何控制托管地的垃圾回收的,为什么有clr就是为了自动垃圾回收嘛,不然为啥不用c++这种是吧。 正文 首先我们要知道内存的分配呢? 在c语音中,内…

flash 钓鱼

访问真实的flash 网站页面,点击查看网页源代码并将所有代码复制下来保存到index.html2.检查并修改源代码链接。搜索href和src,将链接修改为可以访问的形式..新建 flash.js 文件,内容为(其中的 window.location.href 的值修改为 index.html的url地址):window.alert = functio…

Pandas 常用操作 (缺失值处理/排序/字符串处理/Index/Merge/合并)

处理示例:清洗成 -> Code:import pandas as pd# 读取Excel,跳过前面两行空行 studf = pd.read_excel(rstudent_excel.xlsx, skiprows=2) print(studf) # 删除掉全部是空值的列 studf.dropna(axis=columns, how=all, inplace=True) # 删除掉全部是空值的行 studf.dropn…