你可以通过使用'animation' css属性只用css(即Tailwindcss)来实现动画
I- 使用以下命令创建一个 nextjs 项目:$npx create-next-app my-app
II- 使用 nextjs 项目设置 tailwindcss:https://tailwindcss.com/docs/guides/nextjs
III- 在 pages/ 文件夹内创建新的 React 组件 animation.js
import React from 'react'
export default function Animation2() {
return (
<>
{/* Image Animation */}
<div className="animate">
<img src="/frise-2.2f579f.png" alt="" />
<img src="/frise-2.2f579f.png" alt="" />
</div>
{/* Image Animation - Reversed direction */}
<div className="animate-reversed">
<img src="/frise-2.2f579f.png" alt="" />
<img src="/frise-2.2f579f.png" alt="" />
</div>
{/* Text Animation */}
<div className="bg-[#332970] w-screen box-border p-4 flex items-center overflow-hidden fixed bottom-0">
<div className="animate">
{
[0,1,2,3,4,5,6,7,8,9,10,11].map((i) => (
<div className="text-[#139bac] whitespace-nowrap inline-flex items-center justify-center">• NEW SITE LAUNCHING SOON </div>
))
}
</div>
</div>
</>
);
}
-
第一个 div 负责图像的动画,默认方向是从右到左。我使用了 2 个 img 标签,因为它必须有 2 组不同的相同 img 标签。因为在无限循环中,当一个图像消失时,第二个图像会出现,它将重新开始循环,没有任何间隙(您可以评论第二个 img 标签以检查我正在谈论的间隙)
-
第二个 div 类似于第一个,但它具有反向属性。
-
对于文本动画,我们做同样的事情,我们必须多次创建文本以避免在为文本设置动画以进行无限循环时出现间隙。为了避免同一标签出现多行: • NEW SITE LAUNCHING SOON 我包装在一个数组中并循环遍历它
-
所有样式都使用tailwindcss集成在同一个组件中,除了将添加到globals.css文件中的动画,如下所示:
进入 globals.css 文件并添加动画 css 代码:
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.animate{
display: flex;
animation: scroll 20s linear infinite;
}
.animate:hover{
animation-play-state: paused;
}
.animate-reversed{
display: flex;
animation: scroll 20s linear infinite;
animation-direction: reverse;
}
.animate-reversed:hover{
animation-play-state: paused;
}
@keyframes scroll {
0%{
transform: translateX(0);
}
100%{
transform: translate(-50%);
}
}
}