【发布时间】:2020-07-29 00:57:47
【问题描述】:
我一直在尝试使用 React 制作轮盘赌旋转器。但我正在处理一些与转换和角色相关的问题。
这是算法的第一个版本:
回调函数在每次名为winner 的道具发生变化时运行
const slideRef = useRef(null);
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
const spinnerAnimation = useCallback(() => {
const numbers = {
0: 560,
11: -410,
5: 160,
10: 320,
6: 480,
9: 720,
7: 880,
8: 1050,
1: 1135,
14: 960,
2: 800,
13: 640,
3: 400,
12: 240,
4: 80,
};
const cycles = Math.floor(getRandomArbitrary(2, 4));
const dev = getRandomArbitrary(0, 80);
const scrollAmount = 480 + numbers[winner] + dev + 1200 * cycles;
slideRef.current.classList.remove('spin_animation');
slideRef.current.style = 'background-position-x: -212.5px';
setTimeout(() => {
slideRef.current.classList.add('spin_animation');
slideRef.current.style = `background-position-x: ${`-${scrollAmount}px
}, 10);
}, [winner]);
我的问题是,有时它不会停在正确的数字上,而且当我们调整浏览器窗口的大小时它也会中断。 有没有其他方法可以制作微调器,或者我该如何改进?
【问题讨论】:
标签: javascript css reactjs frontend