百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-hover-video-player

> 前端框架
开源

一个用于在悬停时播放视频的 React 组件,包括对鼠标和触摸事件的支持,以及用于添加缩略图和加载状态的简单 API

100 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个用于在悬停时播放视频的 React 组件,包括对鼠标和触摸事件的支持,以及用于添加缩略图和加载状态的简单 API

React Hover Video Player

What It Is

A React component that makes it simple to set up a video that will play when the user hovers over it. This is particularly useful for setting up a thumbnail that will play a video preview on hover.

Want to play around with a real working example? Check it out on CodeSandbox!

Features

  • Out-of-the-box support for both mouse and touchscreen interactions
  • Easily add custom thumbnails and loading states
  • Lightweight and fast
  • No dependencies
  • Gracefully uses fallback behavior if browser policies block a video from playing with sound on

How It Works

This component will render a video element which will start playing when an onMouseEnter, onTouchStart, or onFocus event is fired on the hover target and will accordingly be paused when an onMouseLeave or onBlur event is fired on the target, or an onTouchStart event is fired outside of the target. This default behavior can be disabled, overridden, and customized as needed.

Everything is written with extra care to cleanly handle the video element's state as it asynchronously loads and plays.

Contributing

Want to help? Contributions are welcome! Check out the contributing guide for details

Upgrading to v10

react-hover-video-player 10.0.0 includes some breaking API changes. See here for a breakdown of the changes and how to migrate.

Get Started

Installation

bash
npm install react-hover-video-player

Basic Usage

javascript
import HoverVideoPlayer from 'react-hover-video-player';

function MyComponent() {
  return (
    
  );
}

Sources

videoSrc

Type: string or a React node | This prop is required

videoSrc accepts a string or a React node containing a set of `` elements for the video source file(s) which should be used for the video player.

If you only have one video source, you can simply provide a single string for the URL path to the video file like so:

javascript

If you have multiple video sources, you can provide all of them as `` elements wrapped in a React fragment:

javascript

      
      
    
  )}
/>

If you have multiple video sources, make sure you order them by ascending file size so that the smallest video file is first; browsers will always simply pick the first source that they support.

videoCaptions

Type: object or array of objects | Default: null

videoCaptions accepts a React node containing one or more `` elements descibing the caption track sources to use in order to apply closed captions to the video for accessibility.

If you have a single caption track, you can apply it like so:

typescript

Note that if you do not set default or have more than one track, it is recommended that you set the controls prop to true so that the user may enable the captions or choose the correct captions for their desired language.

If you have more than one track, you can wrap them in a React fragment like so:

javascript

      
      
    
  )}
  // Enable the video's controls so that the user can select the caption track they want or toggle captions on and off
  controls
/>

crossOrigin

Type: string | Default: null

The crossOrigin prop maps directly to the HTML Video element's crossorigin attribute and allows us to define how the video element should handle CORS requests. For most purposes, you should not need to worry about setting this, but if you are having trouble with CORS a good first step may be to try setting it to "anonymous".

The acceptable values are:

  • "anonymous": The video element will send cross-origin requests with no credentials.
  • "use-credentials": The video element will send cross-origin requests with credentials.
javascript

Overlays

pausedOverlay

Type: node | Default: null

This optional prop accepts any renderable content that you would like to be displayed over the video while it is in a paused or loading state. When the video starts playing, this content will be faded out. A common use case for this would be displaying a thumbnail image over the video while it is paused.

javascript

The overlayTransitionDuration prop allows you to set how long it should take for the overlay to fade out when the video starts playing and fade back in when it stops playing.

loadingOverlay

Type: node | Default: null

loadingOverlay is an optional prop that accepts any renderable content that you would like to be displayed over the video if it takes too long to start after the user attempts to play it. This allows you to provide a better user experience for users with slower internet connections, particularly if you are using larger video assets.

Note that the pausedOverlay will still be rendered while the video is in a loading state, so this overlay will simply be displayed on top of that one.

javascript

This overlay will only be displayed if it takes more than a certain amount of time for the video to start after we attempt to play it. You can set the precise timeout duration with the loadingStateTimeout prop.

overlayTransitionDuration

Type: number | Default: 400

overlayTransitionDuration accepts the number of milliseconds that it should take for the paused overlay and loading overlay to fade in and out as the player's state changes.

After the user stops hovering on the player, the video will continue playing until the overlay has fully faded back in to provide the most seamless user experience possible.

javascript

loadingStateTimeout

Type: number | Default: 200

loadingStateTimeout accepts the number of milliseconds that the player should wait before showing a loading state if the video is not able to play immediately.

javascript

hoverOverlay

Type: node | Default: null

hoverOverlay is an optional prop that accepts any renderable content that you would like to be displayed over the video while the player is active from a hover/touch event or when the focused prop is true. This can be useful if you wish to reveal content to the user when they hover while the video still plays underneath it.

Note that this overlay takes highest ordering priority and will be displayed on top of both the pausedOverlay and loadingOverlay if they are set.

javascript

Hover Event Handling

hoverTarget

Type: Node, a function that returns a Node, or a React.RefObject | Default: null

hoverTarget accepts a DOM node, a function that returns a DOM node, or a React ref to an element. The component will apply default event handling to the received target element so the video will play when a user hovers over it with a mouse or touch interaction. If no hoverTarget is provided, HoverVideoPlayer will use the component's container `


### onHoverStart

**Type**: `function` | **Default**: `null`

`onHoverStart` accepts a callback function which will be fired when the user hovers on the player's [hover target](#hovertarget).

```jsx
 {
    console.log('User just moused over or touched hover target.');
    console.log('The video will now attempt to play.');
  }}
/>

onHoverEnd

Type: function | Default: null

onHoverStart accepts a callback function which will be fired when the user stops hovering on the player's hover target.

javascript
 {
    console.log('User just moused out of or touched outside of hover target.');
    console.log('The video will now stop playing.');
  }}
/>

Video Behavior

restartOnPaused

Type: boolean | Default: false

restartOnPaused accepts a boolean value which will toggle whether the video should be reset to the start every time it is paused or resume from the previous time it was at.

javascript

…

jsx

volume

Type: number | Default: 1

volume accepts a number on a scale from 0-1 for the volume that the video's audio should play at.

Note that this will only work if the muted prop is also set to false.

javascript

loop

Type: boolean | Default: true

loop accepts a boolean value which toggles whether or not the video should loop when it reaches the end.

javascript

videoRef

Type: React.Ref | Default: null

videoRef can be used to expose a ref to the video element rendered by HoverVideoPlayer. This is useful if you need to directly access the video element to extend its behavior in some way, but beware that any changes you make could produce unexpected behavior from the component.

javascript
const hoverVideoRef = useRef();

useEffect(() => {
  const videoElement = hoverVideoRef.current;

  videoElement.playbackRate = 2;
}, []);

return ;

…

jsx

playbackRangeEnd

Type: number | Default: null

playbackRangeEnd accepts a number in seconds for what time video playback should end at. If not set, the video will play all the way to the end of the video file.

javascript

Custom Styling

Applying classNames and styles

The base styling for this component's contents are set using inline styling. You can customize or override this styling using various props that accept classNames and style objects.

javascript

…

jsx

…

jsx

…

jsx

…

jsx

Video Controls

controls

Type: boolean | Default: false

controls accepts a boolean value which toggles whether the video element should have the browser's video playback controls enabled.

javascript

controlsList

Type: string | Default: null

controlsList accepts a string describing buttons that should be excluded from the video's playback controls. The string can include the following possible values, with spaces separating each one:

  • "nodownload": Removes the download button from the video's controls
  • "nofullscreen": Removes the fullscreen button from the video's controls

Be aware that this feature is not currently supported across all major browsers.

javascript

disableRemotePlayback

Type: boolean | Default: true

disableRemotePlayback toggles whether the browser should show a remote playback UI on the video, which allows the user to cast the video to other devices.

javascript

disablePictureInPicture

Type: boolean | Default: true

disablePictureInPicture toggles whether the browser should show a picture-in-picture UI on the video, which allows the user to pop the video out into a floating window that persists over other tabs or apps.

javascript

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptcomponenthoverpreviewreact

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架