React Spring 循环属性
React Spring 是一个使UI元素动画化变得简单的动画库。它基于弹簧物理学,使其能够实现自然的外观和感觉。它与其他动画库不同,其他动画库需要处理曲线、缓动和时间持续等,而React Spring 能够使所有这些元素之间相互同步。
平台: React Spring 是一个跨平台库,它支持React,React Native,web和许多其他平台。它还支持所有浏览器。
循环属性: 它是一种会重复发生的动画循环,就像一个循环一样。
语法:
useSpring({
from: { ... },
to: { ... },
delay: ...,
onRest: () => ...
})
创建React Spring应用的步骤:
步骤1: 使用以下命令创建一个新的应用程序。
npx create-react-app reactspringdemo
步骤2:
现在使用以下命令移动已创建的项目文件夹。
cd reactspringdemo
步骤3: 安装React Spring库。
npm install react-spring
项目结构: 它将如下所示。
示例1: 在下面的代码中,我们将使用上面的变量来演示上面的语法的使用,以理解循环属性。
GFG.jsx
import React from 'react';
import { useSpring, animated } from 'react-spring'
const LoopingCard = () => {
/**
* Define the style for the animation
* using the useSpring hook
*/
const styles = useSpring({
loop: true,
from: { rotateZ: 0 },
to: { rotateZ: 180 },
duration: 2000,
});
/**
* Animated div is the extended version of div that
* accepts the properties defined above.
*/
return (<animated.div
style={{
width: 80,
height: 80,
backgroundColor: 'd6d6d6',
borderRadius: 16,
boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'green',
margin: 250,
...styles,
}} >GFG</animated.div>
);
}
export default LoopingCard;
App.js
import React from 'react'
import GFG from './GFG'
function App() {
console.log('hello')
return (
<>
<GFG />
</>
);
}
export default App;
运行应用程序: 运行以下命令。
npm start
输出:
示例2: 在下面的代码中,我们将使用上面的变量来演示上述语法的使用,以便理解循环属性。
GFG.jsx
import React from 'react';
import { useSpring, animated } from 'react-spring'
const LoopingCard = () => {
/**
* Define the style for the animation
* using the useSpring hook
*/
const styles = useSpring({
loop: { reverse: true },
from: { x: 0 },
to: { x: 100 },
})
/**
* Animated div is the extended version of div that
* accepts the properties defined above.
*/
return (<animated.div
style={{
width: 80,
height: 80,
backgroundColor: 'd6d6d6',
borderRadius: 16,
boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'green',
margin: 250,
...styles,
}} >GFG</animated.div>
);
}
export default LoopingCard;
App.js
import React from 'react'
import GFG from './GFG'
function App() {
console.log('hello')
return (
<>
<GFG />
</>
);
}
export default App;
输出:
参考: https://react-spring.dev/common/props#loop-prop