Skip to content

Instantly share code, notes, and snippets.

@ehabdevel
Created December 19, 2017 04:30
Show Gist options
  • Select an option

  • Save ehabdevel/3e966d8342cc4b12bb81ee5bb798f24a to your computer and use it in GitHub Desktop.

Select an option

Save ehabdevel/3e966d8342cc4b12bb81ee5bb798f24a to your computer and use it in GitHub Desktop.
Scroll Animations with React
***About > styles.css***
------------------------
.about-wrapper {
height: 30em;
width: 100vw;
}
.about-text {
position: relative;
width: 30em;
height: 20em;
}
.show {
position: absolute;
left: -30em;
width: 30em;
height: 20em;
-webkit-animation: slide-in 1s forwards;
animation: slide-in 1s forwards;
}
@-webkit-keyframes slide-in {
100% { left: 0 }
}
@keyframes slide-in {
100% { left: 0 }
}
***About > index.js***
-----------------------
import React, { Component } from 'react';
import './styles.css';
class About extends Component {
render() {
return(
<div className="about-wrapper">
<div className="about-text">
<div className={this.props.className}>
<h3>Title</h3>
<p>This is a text that will appear.</p>
</div>
</div>
</div>
)
}
}
export default About;
***App > index.js***
--------------------
import React, { Component } from 'react';
import About from '../About';
import Header from '../Header';
class App extends Component {
constructor() {
super();
this.state = {
className: 'hidden'
}
}
handleScroll() {
if (document.documentElement.scrollTop > 430) {
this.setState({
className: 'show'
})
}
}
componentDidMount() {
window.onscroll = () => this.handleScroll()
}
render() {
return(
<div>
<Header />
<About className={this.state.className} />
</div>
)
}
}
export default App;
***Header > index.js***
-----------------------
import React from 'react';
import './styles.css';
const Header = () => (
<div className="header">
<h1>Title</h1>
</div>
)
export default Header;
***Header > styles.css***
-------------------------
.header {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
.header h1 {
font-size: 30px;
color: white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment