And*_*ont 2 javascript html5-video reactjs
在我的react组件中,我有一个视频标签,如下所示:
<video>
<source src="https://fake.com/file.mp4" type="video/mp4" />
</video>
Run Code Online (Sandbox Code Playgroud)
我还有一个按钮可以用来激活视频:
<button onClick={playVideoFunc?}/>
Run Code Online (Sandbox Code Playgroud)
我假设我将使用某种on click事件来触发视频播放,并且我知道我可以使用以下javascript代码来做到这一点:
var v = document.getElementsByTagName("video")[0];
v.play();
Run Code Online (Sandbox Code Playgroud)
但是感觉这是我第一次使用react我想知道是否有更“反应”的方式来做到这一点?另外,如果我必须像上面一样使用纯JavaScript,是否应该将该代码放入react组件本身?供参考,我的组件看起来像这样(减去一些细节):
import React, { Component } from 'react';
import Footer from './footerComponent'
import Navbar from './navbarComponent';
class Home extends React.Component{
render() {
return (
<div className="wrapper home-page">
<div className="jumbotron-video-wrapper">
<video>
<source src="https://fake.com/file.mp4" type="video/mp4" />
</video>
</div>
<Navbar type={"transparent"}/>
<div className="jumbotron margin-bottom-20" style={heroStyle}>
<div className="container-fluid">
<div className="row">
<div className="col-md-8 col-md-offset-2 text-center">
<h1>WORDS WORDS WORDS</h1>
<img onClick={playvideo??} width="130" src={playImage}/>
<p className="thin">Watch our video</p>
</div>
</div>
</div>
</div>
<Footer/>
</div>
)
}
};
export default Home;
Run Code Online (Sandbox Code Playgroud)
您可以将引用分配给您的视频元素,然后使用this.refs.video.play()调用play ()。
class Home extends React.Component{
render() {
return (
<div className="wrapper home-page">
<div className="jumbotron-video-wrapper">
<video ref="video">
<source src="https://fake.com/file.mp4" type="video/mp4" />
</video>
</div>
<Navbar type={"transparent"}/>
<div className="jumbotron margin-bottom-20" style={heroStyle}>
<div className="container-fluid">
<div className="row">
<div className="col-md-8 col-md-offset-2 text-center">
<h1>WORDS WORDS WORDS</h1>
<img onClick={() => {this.refs.video.play()}} width="130" src={playImage}/>
<p className="thin">Watch our video</p>
</div>
</div>
</div>
</div>
<Footer/>
</div>
)
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3366 次 |
| 最近记录: |