英文:
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 'react';
// import tw from 'tailwindcss'
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.variablesDelay[index]}</p> */}
<p>{tablets.back[index]}</p>
</div>
</div>
))}
</div>
</div>
this is my code in tailwin.config:
/** @type {import('tailwindcss').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",
},
},
}
);
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: ["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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论