一个用于在悬停时播放视频的 React 组件,包括对鼠标和触摸事件的支持,以及用于添加缩略图和加载状态的简单 API
一个用于在悬停时播放视频的 React 组件,包括对鼠标和触摸事件的支持,以及用于添加缩略图和加载状态的简单 API
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!
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.
Want to help? Contributions are welcome! Check out the contributing guide for details
react-hover-video-player 10.0.0 includes some breaking API changes. See here for a breakdown of the changes and how to migrate.
npm install react-hover-video-playerimport HoverVideoPlayer from 'react-hover-video-player';
function MyComponent() {
return (
);
}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:
If you have multiple video sources, you can provide all of them as `` elements wrapped in a React fragment:
)}
/>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.
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:
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:
)}
// Enable the video's controls so that the user can select the caption track they want or toggle captions on and off
controls
/>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.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.
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.
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.
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.
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.
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.
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.
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.');
}}
/>Type: function | Default: null
onHoverStart accepts a callback function which will be fired when the user stops hovering on the player's hover target.
{
console.log('User just moused out of or touched outside of hover target.');
console.log('The video will now stop playing.');
}}
/>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.
…
jsx
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.
Type: boolean | Default: true
loop accepts a boolean value which toggles whether or not the video should loop when it reaches the end.
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.
const hoverVideoRef = useRef();
useEffect(() => {
const videoElement = hoverVideoRef.current;
videoElement.playbackRate = 2;
}, []);
return ;
…
jsx
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.
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.
…
jsx
…
jsx
…
jsx
…
jsx
Type: boolean | Default: false
controls accepts a boolean value which toggles whether the video element should have the browser's video playback controls enabled.
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 controlsBe aware that this feature is not currently supported across all major browsers.
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.
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.
暂无开放 Issues,或尚未同步最近议题。