【发布时间】:2021-07-06 08:40:19
【问题描述】:
有没有办法在 React 中通过 fade 滚动动画来实现改变背景颜色?我可以在滚动上实现背景变化效果,但我无法添加淡入淡出效果。
import React from "react";
export default class ScrollBackground extends React.Component {
state = {
bgcolor: "black",
color: "white",
};
listenScrollEvent = (e) => {
if (window.scrollY < 5300) {
return this.setState({
bgcolor: "black",
color: "white",
opacity: 1 - window.scrollY / 5300,
});
} else {
return this.setState({ bgcolor: "white", color: "black", opacity: 0 });
}
};
componentDidMount() {
window.addEventListener("scroll", this.listenScrollEvent);
}
render() {
return (
<div
style={{
background: this.state.bgcolor,
color: this.state.color,
height: "800px",
}}
>
<div id="header">
<h1>fsefd</h1>
</div>
<div id="section_1" className="section">
This is section 1
</div>
<div id="section_2" className="section">
This is section 2
</div>
<div id="section_5" className="section">
This is section 5
</div>
</div>
);
}
}
【问题讨论】:
-
@AjeetShah,供您参考,我在我的问题中添加了我的代码。
标签: javascript css reactjs css-transitions