I'm trying to achieve a functionality where on scrolling a video gets paused if it was playing and vice versa. Currently I can do it with an onClick function that sets the state true/false. But the problem is that the video keeps on playing after scrolling down till I don't click it to pause again. I want to keep the onClick feature as it is and also want to add the onScroll feature. Tried doing it with onScroll
but not working.
const [playing, setPlaying] = useState(false);
const videoRef = useRef(null);
const handleVideoPress = () => {
if (playing) {
videoRef.current.pause();
setPlaying(false);
} else {
videoRef.current.play();
setPlaying(true);
}
};
return (
<div className="video">
<video
onClick={handleVideoPress}
className="video__player"
loop
ref={videoRef}
src={url}
></video>
);
}