如何在使用Tailwind CSS时为组群悬停添加过渡效果?

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

How to add transition effect on group hover with tailwind css?

问题

以下是已翻译的代码部分:

<a
    href={url}
    className="ease-out duration-300 transition-all group inline-block rounded-xl bg-accent2 px-4 py-2 text-white font-bold hover:bg-accent1 hover:text-black"
>
    <span className="flex items-center gap-2">
        <span>{text}</span>
        <IconChevronRight className="ease-out duration-300 transition-all hidden group-hover:block" />
    </span>
</a>
英文:

I am trying to make an animated transition effect on my button so that when the user hovers, it displays a chevron. I'm building the site in NextJS and using TailwindCSS and I have a group hover so that when the user hovers over the button, it makes the chevron visible. It all works, except so the transition effect doesn't work. the chevron is appearing instantly, however, I want a slight transition duration so that it appears smoothly.

Below is the code I'm using, if anyone can help I would be very grateful.

&lt;a
	href={url}
    className=&quot;ease-out duration-300 transition-all group inline-block rounded-xl bg-accent2 px-4 py-2 text-white font-bold hover:bg-accent1 hover:text-black&quot;  
&gt;
    &lt;span className=&quot;flex items-center gap-2&quot;&gt;
        &lt;span&gt;{text}&lt;/span&gt;
        &lt;IconChevronRight className=&quot;ease-out duration-300 transition-all hidden group-hover:block&quot;/&gt;
    &lt;/span&gt;
&lt;/a&gt;

答案1

得分: 1

"hidden" 应用了 "display: none"。不幸的是,不能在移除/添加 "display: none" 的同一帧上进行过渡。相反,您可以考虑应用 "opacity" 以实现淡出效果:

function IconChevronRight({ className }) {
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      fill="none"
      viewBox="0 0 24 24"
      strokeWidth={1.5}
      stroke="red"
      className={`w-6 h-6 ${className}`}
    >
      <path strokeLinecap="round" strokeLinejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
    </svg>
  );
}

function App({ url, text }) {
  return (
    <a
      href={url}
      className="ease-out duration-300 transition-all group inline-block rounded-xl bg-accent2 px-4 py-2 text-white font-bold hover:bg-accent1 hover:text-black"
    >
      <span className="flex items-center gap-2">
        <span>{text}</span>
        <IconChevronRight className="ease-out duration-300 transition-all opacity-0 group-hover:opacity-100" />
      </span>
    </a>
  )
}

ReactDOM.createRoot(document.getElementById('app')).render(<App url="" text="Foo"/>);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js" integrity="sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js" integrity="sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.tailwindcss.com/3.3.2"></script>

<div id="app" class="bg-slate-500"></div>
英文:

hidden applies display: none. Unfortunately, one cannot transition on the same frame that display: none is removed/add. Instead, you could consider applying opacity for a fade effect:

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

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

function IconChevronRight({ className }) {
  return (
    &lt;svg
      xmlns=&quot;http://www.w3.org/2000/svg&quot;
      fill=&quot;none&quot;
      viewBox=&quot;0 0 24 24&quot;
      strokeWidth={1.5}
      stroke=&quot;red&quot;
      className={`w-6 h-6 ${className}`}
    &gt;
      &lt;path strokeLinecap=&quot;round&quot; strokeLinejoin=&quot;round&quot; d=&quot;M8.25 4.5l7.5 7.5-7.5 7.5&quot; /&gt;
    &lt;/svg&gt;
  );
}

function App({ url, text }) {
  return (
    &lt;a
      href={url}
      className=&quot;ease-out duration-300 transition-all group inline-block rounded-xl bg-accent2 px-4 py-2 text-white font-bold hover:bg-accent1 hover:text-black&quot;  
    &gt;
      &lt;span className=&quot;flex items-center gap-2&quot;&gt;
        &lt;span&gt;{text}&lt;/span&gt;
        &lt;IconChevronRight className=&quot;ease-out duration-300 transition-all opacity-0 group-hover:opacity-100&quot;/&gt;
      &lt;/span&gt;
    &lt;/a&gt;
  )
}

ReactDOM.createRoot(document.getElementById(&#39;app&#39;)).render(&lt;App url=&quot;&quot; text=&quot;Foo&quot;/&gt;);

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js&quot; integrity=&quot;sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js&quot; integrity=&quot;sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.tailwindcss.com/3.3.2&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot; class=&quot;bg-slate-500&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月18日 14:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709967.html
匿名

发表评论

匿名网友

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

确定