【发布时间】:2021-07-21 02:24:24
【问题描述】:
我从一位用户那里复制了这个文件,该文件展示了如何使用名为 videoJS 的 npm 库显示视频:
import React, { useEffect, useRef } from 'react'
import VideoJs from 'video.js'
const videoJsOptions = {
controls: true,
autoplay: true,
fluid: false,
loop: true,
width: '100%',
aspectRatio: '4:3',
children: [
'MediaLoader'
],
}
const VideoPlayer = ({ url }) => {
const videoContainer = useRef()
// Setup the player
useEffect(() => {
// Setting content like this because player.dispose() remove also the html content
videoContainer.current.innerHTML = `
<div data-vjs-player>
<video class="video-js" />
</div>
`
// Setting logger level to all for dev
if (process.env.NODE_ENV === 'development') {
VideoJs.log('all')
}
const player = VideoJs(videoContainer.current.querySelector('video'), videoJsOptions, async () => {
player.src({ src: url, type: 'video/mp4' });
})
// When destruct dispose the player
return () => player.dispose()
}, [url])
console.log(videoContainer)
return <div ref={videoContainer} />
}
export default VideoPlayer
我想删除这个父 div,同时只保留视频元素。我该怎么做?
【问题讨论】:
-
当您设置
videoContainer.current.innerHTML = `<div data-vjs-player><video class="video-js" /></div>`时,您正在积极渲染父div...为什么不只渲染<video />元素? -
嗨,@SethLutske 感谢您的建议。不幸的是,这是我尝试的第一件事。似乎会自动生成这个父 div:
-
-
为什么不直接使用 css 定位父 div 并删除所有边距和填充?
-
成功了!谢谢你。我在 .video-js 类的 padding-top 上使用了 !important
标签: reactjs react-hooks jsx video.js react-dom