如何使用 react-spring 创建从右到左的移动文本?
How to create moving text from right to left using react-spring?
使用 react-spring 移动文本动画?
我正在开发一个使用 reactjs 作为前端的网站,在页眉中我想自动从右向左移动一些文本,但我想只用 react-spring 来制作动画!谁能解决我的问题?
由于我是 React 的新手-spring,我找不到正确的解决方案!
您首先必须将 react-spring
安装到您的项目中。
例如,您可以将代码包含到 App.js 文件中,如下所示:
import { render } from 'react-dom'
import React, { useState, useCallback } from 'react'
import { useTransition, animated } from 'react-spring'
const pages = [
({ style }) => <animated.div style={{ ...style, background: 'lightpink' }}>A</animated.div>,
({ style }) => <animated.div style={{ ...style, background: 'lightblue' }}>B</animated.div>,
({ style }) => <animated.div style={{ ...style, background: 'lightgreen' }}>C</animated.div>,
]
export default function App() {
const [index, set] = useState(0)
const onClick = useCallback(() => set(state => (state + 1) % 3), [])
const transitions = useTransition(index, p => p, {
from: { opacity: 0, transform: 'translate3d(100%,0,0)' },
enter: { opacity: 1, transform: 'translate3d(0%,0,0)' },
leave: { opacity: 0, transform: 'translate3d(-50%,0,0)' },
})
return (
<div className="simple-trans-main" onClick={onClick}>
{transitions.map(({ item, props, key }) => {
const Page = pages[item]
return <Page key={key} style={props} />
})}
</div>
)
}
render(<App />, document.getElementById('root'))
我希望这对你有用。
React-spring是基于物理的,这种类型的动画并不是它的强项。我会做这样的事情。
import React, { useState } from "react";
import { useSpring, animated } from "react-spring";
const TextScroller = ({ text }) => {
const [key, setKey] = useState(1);
const scrolling = useSpring({
from: { transform: "translate(60%,0)" },
to: { transform: "translate(-60%,0)" },
config: { duration: 2000 },
reset: true,
//reverse: key % 2 == 0,
onRest: () => {
setKey(key + 1);
}
});
return (
<div key={key}>
<animated.div style={scrolling}>{text}</animated.div>);
</div>
);
};
export default TextScroller;
还有改进的余地。不处理文本长度。可以禁用滚动条。但我有东西留给你。 :)
工作演示:https://codesandbox.io/s/basic-text-scroller-with-react-spring-siszy
使用 react-spring 移动文本动画?
我正在开发一个使用 reactjs 作为前端的网站,在页眉中我想自动从右向左移动一些文本,但我想只用 react-spring 来制作动画!谁能解决我的问题?
由于我是 React 的新手-spring,我找不到正确的解决方案!
您首先必须将 react-spring
安装到您的项目中。
例如,您可以将代码包含到 App.js 文件中,如下所示:
import { render } from 'react-dom'
import React, { useState, useCallback } from 'react'
import { useTransition, animated } from 'react-spring'
const pages = [
({ style }) => <animated.div style={{ ...style, background: 'lightpink' }}>A</animated.div>,
({ style }) => <animated.div style={{ ...style, background: 'lightblue' }}>B</animated.div>,
({ style }) => <animated.div style={{ ...style, background: 'lightgreen' }}>C</animated.div>,
]
export default function App() {
const [index, set] = useState(0)
const onClick = useCallback(() => set(state => (state + 1) % 3), [])
const transitions = useTransition(index, p => p, {
from: { opacity: 0, transform: 'translate3d(100%,0,0)' },
enter: { opacity: 1, transform: 'translate3d(0%,0,0)' },
leave: { opacity: 0, transform: 'translate3d(-50%,0,0)' },
})
return (
<div className="simple-trans-main" onClick={onClick}>
{transitions.map(({ item, props, key }) => {
const Page = pages[item]
return <Page key={key} style={props} />
})}
</div>
)
}
render(<App />, document.getElementById('root'))
我希望这对你有用。
React-spring是基于物理的,这种类型的动画并不是它的强项。我会做这样的事情。
import React, { useState } from "react";
import { useSpring, animated } from "react-spring";
const TextScroller = ({ text }) => {
const [key, setKey] = useState(1);
const scrolling = useSpring({
from: { transform: "translate(60%,0)" },
to: { transform: "translate(-60%,0)" },
config: { duration: 2000 },
reset: true,
//reverse: key % 2 == 0,
onRest: () => {
setKey(key + 1);
}
});
return (
<div key={key}>
<animated.div style={scrolling}>{text}</animated.div>);
</div>
);
};
export default TextScroller;
还有改进的余地。不处理文本长度。可以禁用滚动条。但我有东西留给你。 :)
工作演示:https://codesandbox.io/s/basic-text-scroller-with-react-spring-siszy