react 可拖拽進(jìn)度條的實(shí)現(xiàn)
效果

/*
* @Author: hongbin
* @Date: 2022-04-16 13:26:39
* @LastEditors: hongbin
* @LastEditTime: 2022-04-16 21:00:02
* @Description:拖動(dòng)進(jìn)度條組件
*/
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;
}到此這篇關(guān)于react 可拖拽進(jìn)度條的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)react 可拖拽進(jìn)度條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React/Redux應(yīng)用使用Async/Await的方法
本篇文章主要介紹了React/Redux應(yīng)用使用Async/Await的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解
這篇文章主要為大家介紹了阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
React中使用react-player 播放視頻或直播的方法
這篇文章主要介紹了React中使用react-player 播放視頻或直播,本文教大家如何使用react框架及創(chuàng)建實(shí)例的代碼,本文內(nèi)容簡(jiǎn)短給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
React-RouterV6+AntdV4實(shí)現(xiàn)Menu菜單路由跳轉(zhuǎn)的方法
這篇文章主要介紹了React-RouterV6+AntdV4實(shí)現(xiàn)Menu菜單路由跳轉(zhuǎn),主要有兩種跳轉(zhuǎn)方式一種是編程式跳轉(zhuǎn)另一種是NavLink鏈接式跳轉(zhuǎn),每種方式通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
React配置Redux并結(jié)合本地存儲(chǔ)設(shè)置token方式
這篇文章主要介紹了React配置Redux并結(jié)合本地存儲(chǔ)設(shè)置token方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
React項(xiàng)目中使用Redux的?react-redux
這篇文章主要介紹了React項(xiàng)目中使用Redux的?react-redux,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
React實(shí)現(xiàn)浮層組件的思路與方法詳解
React?浮層組件(也稱為彈出組件或彈窗組件)通常是指在用戶界面上浮動(dòng)顯示的組件,本文主要介紹了浮層組件的實(shí)現(xiàn)方法,感興趣的小伙伴可以了解下2024-02-02
基于webpack4搭建的react項(xiàng)目框架的方法
本篇文章主要介紹了基于webpack4搭建的react項(xiàng)目框架的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

