網(wǎng)站首頁 編程語言 正文
效果
/*
* @Author: hongbin
* @Date: 2022-04-16 13:26:39
* @LastEditors: hongbin
* @LastEditTime: 2022-04-16 21:00:02
* @Description:拖動進度條組件
*/
import { FC, ReactElement, useRef } from "react";
import styled from "styled-components";
import { flexCenter } from "../../styled";
interface IProps {
/**
* 0-1
*/
value: number;
/**
* callback 0-1
*/
onChange: (percent: number) => void;
}
const ProgressBar: FC<IProps> = ({ value, onChange }): ReactElement => {
const totalRef = useRef<HTMLDivElement>(null);
return (
<Container>
<div ref={totalRef}>
<div style={{ width: value * 100 + "%" }} />
</div>
<div
onMouseDown={(e) => {
const { offsetWidth } = totalRef.current!;
const stop = e.currentTarget;
const { offsetLeft } = stop;
stop.style["left"] = offsetLeft + "px";
const { pageX: start } = e;
const move = (e: MouseEvent) => {
let val = offsetLeft + e.pageX - start;
if (val <= 0) val = 0;
if (val >= offsetWidth) val = offsetWidth;
// stop.style["left"] = val + "px";
onChange(val / offsetWidth);
};
const clear = () => {
document.removeEventListener("mousemove", move);
document.removeEventListener("mouseup", clear);
document.removeEventListener("mouseleave", clear);
};
document.addEventListener("mousemove", move);
document.addEventListener("mouseup", clear);
document.addEventListener("mouseleave", clear);
}}
style={{ left: value * 100 + "%" }}
></div>
</Container>
);
};
export default ProgressBar;
const Container = styled.div`
position: relative;
width: 10vw;
height: 1vw;
${flexCenter};
& > :first-child {
width: inherit;
height: 0.5vw;
background-color: var(--tint-color);
border-radius: 10vw;
overflow: hidden;
display: flex;
align-items: center;
padding: 0.05vw;
div {
width: 5vw;
height: 0.4vw;
background-color: var(--deep-color);
border-radius: 0.4vw;
}
}
& > :last-child {
width: 1vw;
height: 1vw;
background-color: var(--deep-color);
border-radius: 1vw;
position: absolute;
cursor: pointer;
transform: translateX(-0.5vw);
svg {
width: 0.9vw;
}
}
`;
export const flexCenter = css`
display: flex;
justify-content: center;
align-items: center;
`;
:root {
--deep-color: #978961;
--tint-color: #efe5d4;
}
原文鏈接:https://blog.csdn.net/printf_hello/article/details/124225923
相關推薦
- 2022-08-03 使用Apache?Camel表達REST服務的方法_Linux
- 2022-03-13 C語言實現(xiàn)求最大公約數(shù)的三種方法_C 語言
- 2022-12-26 Python常用標準庫之os模塊功能_python
- 2021-12-07 bitbucket搭建詳細過程記錄_其它綜合
- 2023-12-14 【datetime模塊】將時間加一秒或者減一秒
- 2023-07-02 Python+streamlit實現(xiàn)輕松創(chuàng)建人事系統(tǒng)_python
- 2022-03-03 解決Typescript報錯:Property 'style' does not exist on
- 2022-09-17 ASP.NET?Core中Grpc通信的簡單用法_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支