I'm creating a side-project website for myself using React to get a better understanding of it. I'm trying to animate a div to fade in when the user scrolls to its position. I've successfully worked the on-scroll event to trigger a function which changes the class name of the div (according to my understanding this should trigger the transition in opacity). However, this doesn't work; instead this leads to a display of only the text within the div, with no animation what so ever. I'm sure I'm doing something wrong with the way I'm setting the class name of the div but not sure what, here is the code:
class Home extends React.Component{
constructor(){
super()
this.state = {
name: "WhatIdo",
firstTransitionPosition: 0
}
this.handleScroll = this.checkForScroll.bind(this)
this.hasBeenSet = false
}
checkForScroll() {
if (window.scrollY >= this.state.firstTransitionPosition && this.hasBeenSet == false) {
console.log("this event is triggering")
this.setState({name: "WhatIdo--visible"})
this.hasBeenSet = true
}
}
componentDidMount(){
var transitionComp = this.refs.moveMe
var topOfElement = transitionComp.getBoundingClientRect().top
var heightOfElement = transitionComp.getBoundingClientRect().height
this.setState({firstTransitionPosition: topOfElement - heightOfElement})
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount(){
window.removeEventListener('scroll', this.handleScroll);
}
render(){
return(
<div>
<h1 className="PageTitle">Portfolio</h1>
<div className="AboutMe">
some stuff here
</div>
<div ref="moveMe" className={this.state.name}>
some more random stuff
</div>
</div>
)
}
}
And the CSS:
.AboutMe {
border-style: solid;
border-color: black;
border-width: thick;
width: 600px;
height: 300px;
position: relative;
left: 365px;
bottom: 300px;
-moz-animation: fadein 2s;
}
@-moz-keyframes fadein {
from { opacity: 0; }
to { opacity: 2; }
}
.WhatIdo {
border-style: solid;
border-color: black;
border-width: thick;
width: 600px;
height: 300px;
position: relative;
left: 365px;
bottom: 100px;
opacity: 0;
transition: opacity 2s;
}
.WhatIdo--visible {
opacity: 1
}
Thanks in advance for the help!