const ControlCenter = styled.img` position: absolute; //設置絕對位置 left: 50%; top: 50%; //把圖標放在距離左/上50%的位置 margin-left: -20px; margin-top: -20px; //移動圖標自身大小一半的距離,令它居中 width: 40px; height: 40px; `;
JSX:javascript
<div style={{position:'relative'}}> //爲內部設置相對位置 <video /> <ControlCenter src={ImgPlay} /> </div>
效果:css
outline: none;
preload="none"
nodownload 隱藏下載控件html
nofullscreen 隱藏全屏控件java
noremoteplayback 隱藏remote playback controlweb
示例:瀏覽器
<video controls controlslist="nodownload" id="video" src=""></video>
播放視頻時相關控制按鈕的css類(支持基於webkit內核的Chrome瀏覽器)ide
video[0]['disablePictureInPicture'] = true;
在用戶開始從新定位視頻/音頻時觸發。在這裏被用來解決移動進度條後自動暫停的問題……記得在JSX裏不要搞錯大小寫!函數
<video onSeeking="myFunction()">
需求:this
①暫停播放時顯示播放按鈕、點擊播放按鈕時開始播放spa
②僅當開始播放時顯示contorl控件
③當一個視頻開始播放,其餘視頻暫停播放
state:
state = { playingIndex:null, }
綁定函數:
handleClick = (index) => { this.setState({ playingIndex: index, }) const videos = document.getElementsByTagName('video'); videos[index].play(); }
handlePause = (index) => { if (this.state.playingIndex === index) this.setState({ playingIndex: null, }) }
handlePlay = (index) => { this.setState({ playingIndex: index, }) const videos = document.getElementsByTagName('video'); videos[index].disablePictureInPicture = true; for (let j = 0; j <= videos.length - 1; j += 1 ) { //其餘視頻暫停播放 if (j !== index) videos[j].pause(); } }
render():
VIDEO_ITEM.map((item,index) => ( <div style={{position:'relative', margin: '0px 0px 40px 0px'}}> <video controls={this.state.playingIndex === index} //僅當開始播放時顯示contorl控件 onPlay={()=>this.handlePlay(index)} onPause={()=>this.handlePause(index)} /> { this.state.playingIndex !== index && <ControlCenter onClick={()=>this.handleClick(index)} /> } </div> ))
在<video>之上的<container>內:
onContextMenu={this.handleRightClick}
handleRightClick:
handleRightClick = (e) => { if (e.button === 2) { //0爲左鍵,1爲中鍵,2爲右鍵 e.preventDefault(); //不要執行與事件關聯的默認動做 e.returnValue = false; //同上,已被廢棄 return false; } return true; }