我的应用程序中有一个HTML5视频面板。我设法做了一个进度条,它工作得很好。但我也想通过点击进度栏上的任何位置来改变视频的currentTime。
如何使用React作为比率来获得当前位置?我在谷歌上搜索了进度栏组件,但似乎没有人给value点击的位置,而只是显示。
或者,简单地说,当用户单击进度条的不同位置时,我希望将进度条的值更改/获取到该位置。

更新:
我已经更新了演示,它似乎是疯狂的当点击多次,它似乎不接近点击的位置。

Demo:https://stackblitz.com/edit/react-ts-zudlbe?file=index.tsx
发布于 2020-12-16 06:33:15
这是我测试过的全部组件。似乎很管用
const ProgressBar = ({ duration, progress }: ProgressProps) => {
return (
<div
onClick={(event: React.MouseEvent<HTMLDivElement>) => {
// duration is the video duration. For example 152.
// How can i catch the % (ratio) of clicked position of this progrssbar?
// then I will send this % (lets say 5) to the seek function like:
const x = (event.clientX * duration) / event.currentTarget.offsetWidth; // how to get sec?
callback(x);
}}
style={{
backgroundColor: "#ddd",
borderRadius: 3,
flex: 1,
height: 6,
overflow: "hidden",
position: "relative"
}}
>
<Progress progress={progress} />
</div>
);
};https://stackoverflow.com/questions/65317958
复制相似问题