【问题标题】:Animated bar / text with Tailwind CSS and react带有 Tailwind CSS 的动画条/文本并做出反应
【发布时间】:2022-01-29 01:00:49
【问题描述】:

任何人都可以帮我弄清楚如何使用react和tailwind css制作本网站https://worldofwomen.art/中使用的2个动画

第一:这张图片的动画条

第二个:动画文本: • 新网站即将推出

我期待有人的帮助,我真的通过了整个晚上在 react 和 tailwind css 中搜索动画,但我没有找到任何关于此的教程。

非常感谢您对我的问题的帮助和阻止

【问题讨论】:

  • 请帮帮我
  • 看起来他们正在使用 JavaScript 动态更改背景位置以创建该效果。

标签: css reactjs animation css-animations tailwind-css


【解决方案1】:

你可以通过使用'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&nbsp;</div>
            ))
          }
        </div>
      </div>
      
    </>
  );
}
  1. 第一个 div 负责图像的动画,默认方向是从右到左。我使用了 2 个 img 标签,因为它必须有 2 组不同的相同 img 标签。因为在无限循环中,当一个图像消失时,第二个图像会出现,它将重新开始循环,没有任何间隙(您可以评论第二个 img 标签以检查我正在谈论的间隙)

  2. 第二个 div 类似于第一个,但它具有反向属性。

  3. 对于文本动画,我们做同样的事情,我们必须多次创建文本以避免在为文本设置动画以进行无限循环时出现间隙。为了避免同一标签出现多行: • NEW SITE LAUNCHING SOON 我包装在一个数组中并循环遍历它

  4. 所有样式都使用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%);
    }
  }

}

【讨论】:

  • 非常感谢代码正常工作
  • 我的荣幸!我很高兴我的解决方案适用于您提供的堆栈。
【解决方案2】:

有几种方法可以做到这一点,但考虑到您引用的网站,我注意到它以无限增量工作,因此很可能使用 JS 完成。

PS.:我可以用 javascript 做任何事情。

const sleed1 = document.getElementById("sleed1");

let position1 = 0;

setInterval(() => {
  position1 = position1 + 10;
  sleed1.style.backgroundPosition = position1 + 'px';
  sleed1.style.transitionDuration = '100ms';
}, 40)
#sleed1 {
  width: 100%;
  height: 200px;
  background-color: #ccc;
  background-image: url("https://i.stack.imgur.com/zJXIx.png");
}
&lt;div id="sleed1" /&gt;

要控制速度,您可以更改 3 个值:

  1. 像素数增加了,我放了 10 个。
  2. 过渡的持续时间,我输入了“100ms”。
  3. setInterval 间隔,我放了40。

两者都会影响速度,但方式不同,因此请进行调整,直到找到最适合您的解决方案。

要改变雪橇边,将“position + n”改为“position-n”。

React 中的代码相同。将“useEffect”与“,[ ]”一起使用。重要的是不要忘记这一点,因为代码只运行一次并避免不良影响。

https://codesandbox.io/s/slide-image-animation-with-react-and-css-rfhvd?file=/src/App.js

【讨论】:

  • 在使用具有无限增量动画的元素时要小心。请注意,示例站点是一个简单的页面,否则可能会影响性能,但您应该对此进行测试。
  • 我的解决方案不起作用吗?
  • 我无法在 react 组件中集成 javascript。有什么方法可以通过我提供的堆栈来实现吗?
  • 你使用 styled-components 还是其他样式?
  • 我编辑了我的答案,请参阅链接,您可以在其中编辑和玩一下。使用 React 的 useEffect。不要忘记喜欢和评价答案。
猜你喜欢
  • 2021-08-19
  • 2023-01-13
  • 2021-02-02
  • 2021-03-18
  • 2023-01-10
  • 2017-05-16
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多