乐闻世界logo
搜索文章和话题

How to using underline hover animation in tailwindcss

7 个月前提问
3 个月前修改
浏览次数112

5个答案

1
2
3
4
5

要在TailwindCSS中实现下划线悬停动画,我们可以利用其类的实用性和定制性。下面是一个基本示例,展示了如何创建一个当鼠标悬停时逐渐出现下划线的动画:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tailwind CSS 下划线悬停动画</title> <!-- 引入 Tailwind CSS --> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@latest/dist/tailwind.min.css" rel="stylesheet"> <!-- 自定义样式 --> <style> /* 在Tailwind配置中扩展这些类 */ .hover-underline-animation { display: inline-block; position: relative; color: #3490dc; text-decoration: none; } .hover-underline-animation::after { content: ''; position: absolute; width: 100%; transform: scaleX(0); height: 2px; bottom: 0; left: 0; background-color: #3490dc; transform-origin: bottom right; transition: transform 0.25s ease-out; } .hover-underline-animation:hover::after { transform: scaleX(1); transform-origin: bottom left; } </style> </head> <body class="flex items-center justify-center h-screen"> <!-- 下划线动画链接 --> <a href="#" class="hover-underline-animation text-xl"> 悬停我查看下划线动画 </a> </body> </html>

在上述代码中,我们定义了一个hover-underline-animation类,可以添加到任何你想要应用悬停下划线动画的元素上。这个类使用了伪元素:after来在悬停时显示下划线,并通过transform属性来控制下划线的动画效果。使用transition属性让动画平滑进行。

当然,你也可以使用TailwindCSS的@apply指令来在CSS中直接应用Tailwind的工具类,或者通过TailwindCSS的配置文件tailwind.config.js来扩展你的类。这些方式可以让你保持CSS的一致性并充分利用TailwindCSS提供的功能。

2024年6月29日 12:07 回复

您可以使用组 max-w-{x} 和transition-all 在纯 TailwindCSS 中实现这一点,在跨度上使用组悬停,这样当您经过链接时它就会启动动画

shell
<a href="#" class="group text-sky-600 transition duration-300"> Link <span class="block max-w-0 group-hover:max-w-full transition-all duration-500 h-0.5 bg-sky-600"></span> </a>
2024年6月29日 12:07 回复

如果您不想仅包含用于此功能的 CSS 文件,或者您只想在 Tailwind 中执行此操作 - 我在下面发布的代码片段纯粹基于 Tailwind CSS。

shell
<a class="group text-pink-500 transition-all duration-300 ease-in-out" href="#"> <span class="bg-left-bottom bg-gradient-to-r from-pink-500 to-pink-500 bg-[length:0%_2px] bg-no-repeat group-hover:bg-[length:100%_2px] transition-all duration-500 ease-out"> This text gets 'underlined' on hover </span> </a>

干杯,编码愉快!

2024年6月29日 12:07 回复

你可以为此过渡:

shell
.link-underline { border-bottom-width: 0; background-image: linear-gradient(transparent, transparent), linear-gradient(#fff, #fff); background-size: 0 3px; background-position: 0 100%; background-repeat: no-repeat; transition: background-size .5s ease-in-out; } .link-underline-black { background-image: linear-gradient(transparent, transparent), linear-gradient(#F2C, #F2C) } .link-underline:hover { background-size: 100% 3px; background-position: 0 100% } <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" integrity="sha512-wnea99uKIC3TJF7v4eKk4Y+lMz2Mklv18+r4na2Gn1abDRPPOeef95xTzdwGD9e6zXJBteMIhZ1+68QC5byJZw==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <div class="min-h-screen bg-gray-100 py-6 flex flex-col justify-center sm:py-12"> <div class="relative py-3 sm:max-w-xl sm:mx-auto"> <a href="#" class="font-display max-w-sm text-2xl font-bold leading-tight"> <span class="link link-underline link-underline-black text-black"> Link Hover Effect </span> </a> </div> </div>

运行代码片段Hide results

展开片段

2024年6月29日 12:07 回复

您可以使用伪元素在一行中完成所有操作after

shell
<section class="bg-[#0077b6] h-screen w-screen text-white flex items-center justify-center"> <a class="text-3xl relative after:bg-white after:absolute after:h-1 after:w-0 after:bottom-0 after:left-0 hover:after:w-full after:transition-all after:duration-300 cursor-pointer">Text you want to underline in just one line</a> </section>

这是一支带有用例的笔:

https://codepen.io/Marcos-Riverola/pen/poQmRKV?editors=1010

2024年6月29日 12:07 回复

你的答案