Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Play on hover #2854

Unanswered
illiachumak asked this question in Q&A
Discussion options

Hi, how to make video play on hover?
currently its not working, only on click

const VideoPlayer: React.FC<VideoPlayerProps> = ({ sourceItem }) => {
 const videoMedia = sourceItem.media[0] as Video;
 const playerRef = useRef<any>(null);
 const [isPlaying, setIsPlaying] = useState(false);
 const [isHover, setIsHover] = useState(false);
 const playerSource: SourceInfo = {
 type: "video" as const,
 title: sourceItem.title || "",
 sources: videoMedia.version_list.map((v: VideoVersion) => ({
 src: v.url,
 type: v.url.endsWith(".webm") ? "video/webm" : "video/mp4",
 size: v.height,
 })),
 poster: "",
 previewThumbnails: undefined,
 tracks: [],
 };
 const plyrOptions: Options = {
 controls: [],
 clickToPlay: true,
 autopause: true,
 };
 useEffect(() => {
 const plyrInstance = playerRef.current?.plyr;
 if (!plyrInstance) return;
 if (isHover) {
 plyrInstance.play();
 setIsPlaying(true);
 } else {
 plyrInstance.pause();
 setIsPlaying(false);
 }
 }, [isHover]);
 const handleVideoClick = () => {
 const plyrInstance = playerRef.current?.plyr;
 if (!plyrInstance) return;
 if (isPlaying) {
 plyrInstance.pause();
 setIsPlaying(false);
 } else {
 plyrInstance.play();
 setIsPlaying(true);
 }
 };
 const containerStyle: React.CSSProperties = {
 position: "relative",
 width: "100%",
 height: "auto",
 borderRadius: "8px",
 overflow: "hidden",
 cursor: "pointer",
 };
 return (
 <div
 style={containerStyle}
 // onMouseEnter={() => setIsHover(true)}
 // onMouseLeave={() => setIsHover(false)}
 onClick={handleVideoClick}
 >
 <Plyr ref={playerRef} source={playerSource} options={plyrOptions} />
 </div>
 );
};
export default VideoPlayer;
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /