[英]How to fix translate which contains state, which changed by 'interval' (React)
I have a procedure for a timer for animation. 我有一个动画计时器的程序。 I want to make an animation of logos like https://stripe.com/us/customers .
我想制作徽标动画,例如https://stripe.com/us/customers 。 But I have an endless loading of the page, so it doesn't work.
但是页面的加载无尽,因此无法正常工作。
I tried to use a procedure in different parts of the code and tried to change interval for better optimization (I think my PC can't work with 1 ms interval), but it didn't help me. 我尝试在代码的不同部分中使用一个过程,并尝试更改间隔以进行更好的优化(我认为我的PC不能以1 ms的间隔运行),但这并没有帮助。
It all from one file. 全部来自一个文件。 State:
州:
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
submitError: false,
width: 0,
height: 0,
timer: 0
};
Procedure: 程序:
TimeForLogos() {
const time = setInterval(() => {
const passedTime = this.state.timer + 0.1;
if (passedTime === 20) {
clearInterval(time);
}
this.setState({
timer: passedTime,
});
}, 100);
}
I try to use it in render (I need that procedure begins when the site is opened) and try to make a button (I thought it help me to solve the problem of endless loading). 我尝试在渲染中使用它(我需要在打开网站时开始该过程),并尝试制作一个按钮(我认为这有助于解决无休止的加载问题)。 So part of code from render for now:
因此,现在部分来自render的代码:
<div className={s.logos}>
<span onClick={this.TimeForLogos()}>go</span>
{LogoData.map(logo => (
<div
className={s.logo}
style={{
right: `${logo.positionX}px`,
top: `${logo.positionY}px`,
width: logo.width * 1.1,
padding: `${(logo.width - logo.height) / 2} 0`,
transform: `translate(${this.state.timer * 10}px,0) scale(1)`,
}}
>
<img
src={logo.img}
alt=""
style={{
width: logo.width,
height: logo.height,
}}
/>
</div>
))}
</div>
So, a question. 所以,一个问题。 How can I update my code that it works?
如何更新其有效的代码? And I need to make so that procedure works when the site is opening (animation must play when the site is opened).
而且我需要使该程序在网站打开时起作用(在网站打开时必须播放动画)。 How I can do it?
我该怎么办?
You can create a separate component and handle the transition of each component in the timeout function. 您可以创建一个单独的组件,并在超时功能中处理每个组件的过渡。
class Home extends Component {
constructor(props) {
super(props);
this.logoData = [{
width: 100,
height: 100,
text: "text1"
},
{
width: 100,
height: 100,
text: "text2"
},
{ width: 100,
height: 100,
text: "text3"
}];
}
render() {
return (
<div className="mainContainer">
{this.logoData.map(item => <MovingDiv {...item}/>)}
</div>
);
}
}
Creating one more react-component say MovingDiv
. 再创建一个
MovingDiv
组件,例如MovingDiv
。
class MovingDiv extends Component {
constructor(props) {
super(props);
this.state = {
translateX: 0,
translateY: 0,
scale: 0
}
}
componentDidMount() {
const intervalLimit = setInterval(() => {
let { translateX, translateY} = this.state;
// logic of setting translateX goes here
if(--translateX < -300) { // -300 is just for example
translateX = 0;
}
this.setState({
translateX
})
}, 200)
}
/* will move the divs towards right every 200ms*/
render() {
return(<div style={{ width: this.props.width, height: this.props.height, transform: `translate(${this.state.translateX}px, 0px)`}}>{this.props.text}</div>);
}
}
Hope this helps! 希望这可以帮助!
I updated something in code of previous commentator. 我更新了以前评论者的代码中的某些内容。 I make everything in one class (Home).
我把所有东西都放在一堂课上(家庭)。 I make json file for logos and info contains in this file.
我制作用于徽标的json文件,此文件中包含信息。 So it's my code.
这就是我的代码。
class Home extends React.Component {
constructor(props) {
super(props);
this.state = {
translateX: 0,
};}
render() {
setInterval(() => {
let { translateX } = this.state;
translateX -= 2;
if (translateX < -1800) {
translateX = 0;
}
this.setState({
translateX,
});
}, 1000);
<div className={s.logos}>
{LogoData.map(logo => (
<div
className={s.logo}
style={{
left: `${logo.positionX}px`,
top: `${logo.positionY}px`,
width: logo.width * 1.1,
padding: `${(logo.width - logo.height) / 2} 0`,
transform: `translate(${this.state.translateX}px,0) scale(1)`,
}}
>
<img
src={logo.img}
alt=""
style={{
width: logo.width,
height: logo.height,
}}
/>
</div>
))}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.