gpt4 book ai didi

javascript - 在 React 的秒表中每秒旋转秒数

转载 作者:行者123 更新时间:2023-11-30 20:02:53 25 4
gpt4 key购买 nike

我正在结束我在 React 中的秒表项目,并且在 countTimers 方法中定义的每秒旋转秒数时遇到问题。我用 0(deg) 定义了旋转状态。然后在setInterval中的countTimers函数中将旋转状态更改为360(deg),然后有条件: if(this.state.seconds > 0 || this.state.seconds < 60 && this.state.rotation === 360)尝试每秒执行一次转换,并在每次转换后将旋转状态设置为 0(度)。

import React, {Component} from 'react';

class Timer extends Component {
constructor(props){
super(props);

this.state = {
count: 0,
pause: true,
rotation: 0
};
}

componentDidMount() {
this.countTimers();
}

countTimers = () => {
let counter = setInterval(() => {
if (!this.state.pause) {
this.setState({
count: this.state.count + 1,
rotation: 360
});

if(this.state.seconds > 0 || this.state.seconds < 60 && this.state.rotation === 360) {
document.querySelector('.seconds').style.transition = "all 0.1s ease";
document.querySelector('.seconds').style.transform= "rotateX(360deg)";
}

this.setState({
rotation: 0
});
}
}
, 1000);
}

startHandler = () => {
this.setState({
pause: false
})
}

pauseHandler = () => {
this.setState({
pause: true
})
}

转换仅完成一次,在 componentDidMount 之后并通过单击开始按钮。

    render () {
let days = Math.floor(this.state.count / (1 * 60 * 60 * 24));
let hours = Math.floor((this.state.count % (1 * 60 * 60 * 24)) / (1 * 60 * 60));
let minutes = Math.floor((this.state.count % (1 * 60 * 60)) / (1 * 60));
let seconds = Math.floor((this.state.count % (1 * 60)) / 1);

return (
<div className="Timer">
<h1>{'STOPWATCH'}</h1>
<div className="stopwatch-wrapper">
<span className="days">{days}:</span>
<span className="hours">{hours}:</span>
<span className="minutes">{minutes}:</span>
<span className={"seconds"}>{seconds}</span>
</div>
<div className="buttons-wrapper">
<button id="start" onClick={this.startHandler}>START</button>
<button id="pause" onClick={this.pauseHandler}>PAUSE</button>
</div>
</div>
);
}
}

export default Timer;

有人知道如何解决这个问题吗?

最佳答案

你有很多问题,首先 seconds 状态不存在,所以你改变旋转的条件永远不会返回 true。接下来,您需要将 span display css 属性设置为 inline-block。最后,我们正在使用 React!无需直接操作元素样式,在渲染函数中执行它就可以正常工作,而且 react 更灵敏。

运行下面的代码片段以查看其工作情况

class Timer extends React.Component {
constructor(props) {
super(props);

this.state = {
count: 0,
pause: true,
rotation: 0
};
}

componentDidMount() {
this.countTimers();
}

countTimers = () => {
let counter = setInterval(() => {
if (!this.state.pause) {
this.setState({
count: this.state.count + 1,
rotation: this.state.rotation === 359.9 ? 0 : 359.9,
});
}
}, 1000);
}

startHandler = () => {
this.setState({
pause: false
})
}

pauseHandler = () => {
this.setState({
pause: true
})
}

render() {
const {rotation, count} = this.state;
let days = Math.floor(count / (1 * 60 * 60 * 24));
let hours = Math.floor((count % (1 * 60 * 60 * 24)) / (1 * 60 * 60));
let minutes = Math.floor((count % (1 * 60 * 60)) / (1 * 60));
let seconds = Math.floor((count % (1 * 60)) / 1);

return (
<div className="Timer">
<h1>{'STOPWATCH'}</h1>
<div className="stopwatch-wrapper">
<span className="days">{days}:</span>
<span className="hours">{hours}:</span>
<span className="minutes">{minutes}:</span>
<span className="seconds" style={{transform: `rotate(${rotation}deg)`}}>{seconds}</span>
</div>
<div className="buttons-wrapper">
<button id="start" onClick={this.startHandler}>START</button>
<button id="pause" onClick={this.pauseHandler}>PAUSE</button>
</div>
</div>
);
}
}

ReactDOM.render( <
Timer / > ,
document.getElementById("react")
);
.seconds{
display: inline-block;
transition: transform 500ms;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>

关于javascript - 在 React 的秒表中每秒旋转秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com