使用Tailwind的翻转卡片在任意顺序下存在问题。

huangapple go评论76阅读模式
英文:

problem with tailwind filp card flip in any order

问题

我的问题是卡片没有按顺序翻转。

这是我的 tsx 代码:

import { useState } from 'react';

const LandingPage = () => {
  const [isHovering, setIsHovering] = useState(false);

  const handleMouseEnter = () => {
    setIsHovering(true);
  };

  const handleMouseLeave = () => {
    setIsHovering(false);
  };

  const tablets = {
    front: ['F', 'r', 'a', 'n', 'c', 'o', ' ', 'A', '.', 'M', 'a', 'r', 'c', 'u', 'z', 'z', 'i', ' '],
    back: ['D', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', ' ', 'B', 'a', 'c', 'k', 'e', 'n', 'd', ' '],
  };

  return (
    <section id="home">
      <div className="container mx-auto flex px-10 py-20 md:flex-row flex-col bg-black">
        <div
          className="bg-slate-800 flex-grow w-full flex items-center justify-center flex-col group"
          onMouseEnter={handleMouseEnter}
          onMouseLeave={handleMouseLeave}
        >
          <div className="grid grid-cols-9 gap-1 my-4 w-full text-center font-extrabold font text-3xl lg:text-6xl sm:text-6xl md:text-4xl">
            {tablets.front.map((e, index) => (
              <div className={`relative bg-red-800 transform transition ease-in-out hadow-xl shadow-black/40 ${isHovering ? `group-hover:[transform:rotateY(180deg)] duration-1000` : ''}`} key={index}>
                <div className="bg-red-800 p-2">{e}</div>
                <div className={`absolute top-0 left-0 w-full h-full bg-blue-500 p-2 text-white transform transition ease-in-out group-hover:[transform:rotateY(180deg)] ${isHovering ? `delay-${index}` : `opacity-0 delay-${index}`}`}>
                  <p>{tablets.back[index]}</p>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
};

这是我的 tailwin.config 中的代码:

const plugin = require('tailwindcss/plugin');

const Myclass = plugin(function ({ addUtilities }) {
  addUtilities({
    '.my-rotate-y-180': {},
  });
});

const animationDelay = plugin(
  function ({ matchUtilities, theme }) {
    matchUtilities(
      {
        delay: (value) => {
          return {
            'transition-delay': value,
          };
        },
      },
      {
        values: theme('animationDelay'),
      }
    );
  },
  {
    theme: {
      animationDelay: {
        0: '0ms',
        1: '200ms',
        2: '400ms',
        3: '600ms',
        4: '800ms',
        5: '1000ms',
        6: '1200ms',
        7: '1400ms',
        8: '1600ms',
        9: '1800ms',
        10: '2000ms',
        11: '2200ms',
        12: '2400ms',
        13: '2600ms',
        14: '2800ms',
        15: '3000ms',
        16: '3200ms',
        17: '3400ms',
      },
    },
  }
);
英文:

my problem is that the cards are not flipped sequentially

this is my code in tsx:
import { useState } from &#39;react&#39;;
// import tw from &#39;tailwindcss&#39;
const LandingPage = () =&gt; {
const [isHovering, setIsHovering] = useState(false);
const handleMouseEnter = () =&gt; {
setIsHovering(true);
};
const handleMouseLeave = () =&gt; {
setIsHovering(false);
};
const tablets ={
front: [&quot;F&quot;,&quot;r&quot;,&quot;a&quot;,&quot;n&quot;,&quot;c&quot;,&quot;o&quot;,&quot; &quot;,&quot;A&quot;,&quot;.&quot;,&quot;M&quot;,&quot;a&quot;,&quot;r&quot;,&quot;c&quot;,&quot;u&quot;,&quot;z&quot;,&quot;z&quot;,&quot;i&quot;,&quot; &quot;],
back: [&quot;D&quot;,&quot;e&quot;,&quot;v&quot;,&quot;e&quot;,&quot;l&quot;,&quot;o&quot;,&quot;p&quot;,&quot;e&quot;,&quot;r&quot;,&quot; &quot;,&quot;B&quot;,&quot;a&quot;,&quot;c&quot;,&quot;k&quot;,&quot;e&quot;,&quot;n&quot;,&quot;d&quot;,&quot; &quot;],
}
return (
&lt;section id=&quot;home&quot;&gt;
&lt;div className=&quot;container mx-auto flex px-10 py-20 md:flex-row flex-col bg-black&quot;&gt;
&lt;div className=&quot;bg-slate-800 flex-grow w-full flex items-center justify-center flex-col group&quot;
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}&gt;
&lt;div className=&quot;grid grid-cols-9 gap-1 my-4 w-full text-center font-extrabold font text-3xl lg:text-6xl sm:text-6xl md:text-4xl&quot;&gt;
{tablets.front.map((e, index) =&gt; (
&lt;div className={`relative bg-red-800  transform transition ease-in-out hadow-xl shadow-black/40 ${isHovering ? `group-hover:[transform:rotateY(180deg)] duration-1000` : &#39;&#39;}`} key={index}&gt;
&lt;div className=&quot;bg-red-800 p-2 &quot;&gt;{e}&lt;/div&gt;
&lt;div className={`absolute top-0 left-0 w-full h-full  bg-blue-500 p-2  text-white  transform transition ease-in-out group-hover:[transform:rotateY(180deg)] ${isHovering ? `delay-${index}` : `opacity-0  delay-${index} ` }`}&gt;
{/* &lt;p&gt;{tablets.variablesDelay[index]}&lt;/p&gt; */}
&lt;p&gt;{tablets.back[index]}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
))}
&lt;/div&gt;
&lt;/div&gt;

this is my code in tailwin.config:

/** @type {import(&#39;tailwindcss&#39;).Config} */
const plugin = require(&quot;tailwindcss/plugin&quot;);
const Myclass = plugin(function ({ addUtilities }) {
addUtilities({
&quot;.my-rotate-y-180&quot;: {},
});
});
const animationDelay = plugin(
function ({ matchUtilities, theme }) {
matchUtilities(
{
delay: (value) =&gt; {
return {
&quot;transition-delay&quot;: value,
};
},
},
{
values: theme(&quot;animationDelay&quot;),
}
);
},
{
theme: {
animationDelay: {
0: &quot;0ms&quot;,
1: &quot;200ms&quot;,
2: &quot;400ms&quot;,
3: &quot;600ms&quot;,
4: &quot;800ms&quot;,
5: &quot;1000ms&quot;,
6: &quot;1200ms&quot;,
7: &quot;1400ms&quot;,
8: &quot;1600ms&quot;,
9: &quot;1800ms&quot;,
10: &quot;2000ms&quot;,
11: &quot;2200ms&quot;,
12: &quot;2400ms&quot;,
13: &quot;2600ms&quot;,
14: &quot;2800ms&quot;,
15: &quot;3000ms&quot;,
16: &quot;3200ms&quot;,
17: &quot;3400ms&quot;,
},
},
}
);

I tried passing a variable in className="delay-${index}"

with which I access the index of each of the cards and I determine a delay for each one

but it doesn't work turn in any order

help me please !!

i am new to tailwind and tsx

答案1

得分: 0

你基本上已经把这个做好了。我移除了不需要的鼠标事件处理程序。这些导致与你正确设置的父元素上的 CSS 悬停属性产生冲突。请记住,在 Tailwind 中,你的类需要在你的源代码中存在。避免使用 JS 进行类操作。

另外还有一些小改动,包括去除不透明度并使用背面可见性,以及移除第二次悬停变换。这样想,当你在父元素上悬停时,卡片翻转,当你不悬停时,它们处于默认状态。因此,你不需要尝试通过卡片反面的变换来强制将卡片恢复到初始状态。

另外,实际上你不需要在配置中添加所有这些动画延迟。这似乎非常死板,如果你想加快或减慢速度,更新所有这些将会很痛苦。相反,使用 CSS 自定义属性。然后,你可以在每个字母卡片元素的样式中设置该变量。这样可以很容易地更改一个值来调整速度以适应你的喜好。

工作代码片段:

<!-- begin snippet: js hide: false console: false babel: true -->

<!-- language: lang-js -->

function LandingPage() {
  const tablets = {
    front: ["F","r","a","n","c","o"," ","A",".","M","a","r","c","u","z","z","i"," "],
    back: ["D","e","v","e","l","o","p","e","r"," ","B","a","c","k","e","n","d"," "],
  }
  return (
    <section id="home">
      <div className="container mx-auto flex px-10 py-20 md:flex-row flex-col bg-black">
        <div className="bg-slate-800 flex-grow w-full flex items-center justify-center flex-col group cursor-pointer">
          <div className="grid grid-cols-9 gap-1 my-4 w-full text-center font-extrabold font text-3xl lg:text-6xl sm:text-6xl md:text-4xl">
            {tablets.front.map((e, index) => (
              <div className="relative bg-red-800 transform transition ease-in-out shadow-xl shadow-black/40 [transform-style:preserve-3d] group-hover:[transform:rotateY(180deg)] duration-1000 delay-[var(--delay)]" style={{"--delay": index * .1 + "s"}} key={index}>
                <div className="bg-red-800">{e}</div>
                <div className="absolute inset-0 h-full w-full bg-blue-500 [transform:rotateY(180deg)] [backface-visibility:hidden]">
                  <p>{tablets.back[index]}</p>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  )
}

const root = ReactDOM.createRoot(document.getElementById("root"))
root.render(<LandingPage />);

<!-- language: lang-html -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<div id="root"></div>

<!-- end snippet -->
英文:

You mostly had this working. I removed the unneeded mouse event handlers. These were causing collisions with the CSS hover property that you correctly set using a group on the parent. Keep in mind with Tailwind that your classes need to be in your source. Avoid using JS for class manipulation.

Another couple of small changes were to remove the opacity and use backface-visibility, as well as to remove the second hover transform. Think of it this way, when you're hovering on the parent, the cards flip, and when you're not they are in their default state. So you don't need to try and force the cards back in the initial state with a transform on the reverse of the cards.

Also, you don't actually need to add in all of those animation delays in your config. It seems really rigid and it would be a pain to update all of them if you wanted to speed things up or slow them down. Instead, use a CSS custom property. You can then set that variable in the styles for each of the letter card elements individually. This makes it really easy to change one value to adjust the speed to your liking.

Working Snippet:

<!-- begin snippet: js hide: false console: false babel: true -->

<!-- language: lang-js -->

function LandingPage() {
const tablets = {
front: [&quot;F&quot;,&quot;r&quot;,&quot;a&quot;,&quot;n&quot;,&quot;c&quot;,&quot;o&quot;,&quot; &quot;,&quot;A&quot;,&quot;.&quot;,&quot;M&quot;,&quot;a&quot;,&quot;r&quot;,&quot;c&quot;,&quot;u&quot;,&quot;z&quot;,&quot;z&quot;,&quot;i&quot;,&quot; &quot;],
back: [&quot;D&quot;,&quot;e&quot;,&quot;v&quot;,&quot;e&quot;,&quot;l&quot;,&quot;o&quot;,&quot;p&quot;,&quot;e&quot;,&quot;r&quot;,&quot; &quot;,&quot;B&quot;,&quot;a&quot;,&quot;c&quot;,&quot;k&quot;,&quot;e&quot;,&quot;n&quot;,&quot;d&quot;,&quot; &quot;],
}
return (
&lt;section id=&quot;home&quot;&gt;
&lt;div className=&quot;container mx-auto flex px-10 py-20 md:flex-row flex-col bg-black&quot;&gt;
&lt;div className=&quot;bg-slate-800 flex-grow w-full flex items-center justify-center flex-col group cursor-pointer&quot;&gt;
&lt;div className=&quot;grid grid-cols-9 gap-1 my-4 w-full text-center font-extrabold font text-3xl lg:text-6xl sm:text-6xl md:text-4xl&quot;&gt;
{tablets.front.map((e, index) =&gt; (
&lt;div className=&quot;relative bg-red-800 transform transition ease-in-out shadow-xl shadow-black/40 [transform-style:preserve-3d] group-hover:[transform:rotateY(180deg)] duration-1000 delay-[var(--delay)]&quot; style={{&quot;--delay&quot;: index * .1 + &quot;s&quot;}} key={index}&gt;
&lt;div className=&quot;bg-red-800&quot;&gt;{e}&lt;/div&gt;
&lt;div className=&quot;absolute inset-0 h-full w-full bg-blue-500 [transform:rotateY(180deg)] [backface-visibility:hidden]&quot;&gt;
&lt;p&gt;{tablets.back[index]}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
))}
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
)
}
const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;))
root.render(&lt;LandingPage /&gt;);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月20日 23:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定