Skip to content

Instantly share code, notes, and snippets.

@countaight
Created March 29, 2019 17:47
Show Gist options
  • Select an option

  • Save countaight/46d898b6605864b831eccf0a9ea41c61 to your computer and use it in GitHub Desktop.

Select an option

Save countaight/46d898b6605864b831eccf0a9ea41c61 to your computer and use it in GitHub Desktop.
React Video Scrolling Header
import React, { Component } from 'react';
import HeaderVideo from './HeaderVideo.mp4';
class Header extends Component {
vid = null;
scrollingDiv = null;
seek = () => {
const frame = window.pageYOffset / 250;
this.vid.currentTime = frame;
window.requestAnimationFrame(this.seek);
}
componentDidMount() {
this.vid.addEventListener('loadedmetadata', () => {
this.scrollingDiv.style.height = this.vid.duration * 250 + 'px';
});
window.requestAnimationFrame(this.seek);
}
render() {
return <div style={divStyle} ref={ref => { this.scrollingDiv = ref }}>
<p style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flex: 1,
alignSelf: 'stretch',
color: 'white',
fontSize: 52,
backgroundColor: 'rgba(100, 100, 100, 0.5)',
margin: 0
}}>Hi, I'm Ox... And I do techy stuff...</p>
<video tabIndex="0"
autobuffer="autobuffer"
preload="preload"
style={{
position: 'fixed',
width: '100%',
height: '100%',
objectFit: 'cover',
objectPosition: '100% 50%',
zIndex: -1
}}
playsInline
ref={(vid) => this.vid = vid}>
<source src={HeaderVideo} type="video/mp4" />
</video>
</div>
}
}
const divStyle = {
display: 'flex',
width: '100%',
}
export default Header;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment