如何使用 Tailwind CSS 制作令人惊叹的 CTA 动画

P粉547719424
2024-05-09 17:50:43 1004

现场演示/下载
号召性用语是界面中最重要的元素,因为它使用户能够将他们的意图转化为行动,并使产品能够最终确定特定目标(例如,将潜在客户转变为免费试用)。

在我们所有的Tailwind 模板中,您至少会找到一个号召性用语。我们以无限的形状和风格设计它们,最终目标是在产品和最终用户之间建立联系。

一般来说,我们喜欢设计简单直接的 CTA,但对于本教程(受到Glide 的启发),我们决定使用 Tailwind CSS 创建一个号召性用语动画。

我们将构建一个假按钮,当您将鼠标悬停在其上时会触发多个动画:

将部分的配色方案从浅色更改为深色。
为按钮上的文本添加滚动效果。
使用“闪亮”效果突出显示按钮的边框。
创建基础结构
好吧,让我们从创建 CTA 的基本结构开始。我们将使用<section>标签作为容器,并使用<a>标签使所有内容都可点击。

  1. <section class="relative z-0">
  2. <div class="w-full max-w-5xl mx-auto px-4 md:px-6 py-48">
  3. <div class="text-center">
  4. <a
  5. class="flex flex-col lg:flex-row space-y-4 lg:space-y-0 lg:space-x-6 items-center justify-center text-3xl sm:text-4xl md:text-5xl font-semibold text-slate-900"
  6. href="#0"
  7. >
  8. <!-- Fake Button -->
  9. <span>
  10. <!-- Default content: "Build the UI you need" -->
  11. <span>Build the UI you need</span>
  12. <!-- Hover content: "Create beautiful user interfaces" -->
  13. <span
  14. class="before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces']"
  15. aria-hidden="true"
  16. ></span>
  17. </span>
  18. <!-- Text: "with Cruip" -->
  19. <span>with Cruip</span>
  20. </a>
  21. </div>
  22. </div>
  23. </section>

设置非常简单。唯一要强调的是,我们对移动文本使用了伪属性before和。after这使我们可以复制文本而无需创建另一个元素。

更改悬停时的部分颜色模式
在定义其余样式之前,让我们看看当鼠标悬停在背景颜色上时如何更改背景颜色。我们将使用before伪属性来创建叠加层。

  1. <section class="relative z-0">
  2. <div class="w-full max-w-5xl mx-auto px-4 md:px-6 py-48">
  3. <div class="text-center">
  4. <a
  5. class="flex flex-col lg:flex-row space-y-4 lg:space-y-0 lg:space-x-6 items-center justify-center text-3xl sm:text-4xl md:text-5xl font-semibold text-slate-900 before:absolute before:inset-0 hover:before:bg-slate-900 before:-z-10 before:transition-colors before:duration-500 group"
  6. href="#0"
  7. >
  8. <!-- Fake Button -->
  9. <span>
  10. <!-- Default content: "Build the UI you need" -->
  11. <span class="group-hover:opacity-0 transition-opacity duration-500 ease-in-out">Build the UI you need</span>
  12. <!-- Hover content: "Create beautiful user interfaces" -->
  13. <span
  14. class="before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces']"
  15. aria-hidden="true"
  16. ></span>
  17. </span>
  18. <!-- Text: "with Cruip" -->
  19. <span>with Cruip</span>
  20. </a>
  21. </div>
  22. </div>
  23. </section>

创建假按钮交互
为了在默认内容和悬停内容之间实现平滑过渡,我们将后者绝对定位为不透明度 0。并使用 Tailwind 的group-hover:修改器,我们将切换不透明度。

  1. <section class="relative z-0">
  2. <div class="w-full max-w-5xl mx-auto px-4 md:px-6 py-48">
  3. <div class="text-center">
  4. <a
  5. class="flex flex-col lg:flex-row space-y-4 lg:space-y-0 lg:space-x-6 items-center justify-center text-3xl sm:text-4xl md:text-5xl font-semibold text-slate-900 before:absolute before:inset-0 hover:before:bg-slate-900 before:-z-10 before:transition-colors before:duration-500 group"
  6. href="#0"
  7. >
  8. <!-- Fake Button -->
  9. <span class="relative">
  10. <!-- Default content: "Build the UI you need" -->
  11. <span class="group-hover:opacity-0 transition-opacity duration-500 ease-in-out">Build the UI you need</span>
  12. <!-- Hover content: "Create beautiful user interfaces" -->
  13. <span
  14. class="absolute inset-0 opacity-0 group-hover:opacity-100 transition-opacity duration-500 before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces']"
  15. aria-hidden="true"
  16. ></span>
  17. </span>
  18. <!-- Text: "with Cruip" -->
  19. <span>with Cruip</span>
  20. </a>
  21. </div>
  22. </div>
  23. </section>

设置按钮样式
现在,让我们设置按钮的样式。首先,我们将定义默认文本的样式。我们将使用一堆 Tailwind 实用程序类来设置填充、背景渐变、边框渐变等。

  1. <!-- Fake Button -->
  2. <span
  3. class="relative p-0.5 rounded-full bg-slate-200 group-hover:bg-slate-800 transition duration-500 overflow-hidden flex items-center justify-center"
  4. >
  5. <span class="relative whitespace-nowrap">
  6. <!-- Default content: "Build the UI you need" -->
  7. <span
  8. class="block px-8 py-6 rounded-full bg-gradient-to-r from-slate-200 to-slate-100 z-10 group-hover:opacity-0 transition-opacity duration-500 ease-in-out"
  9. >Build the UI you need</span
  10. >
  11. <!-- Hover content: "Create beautiful user interfaces" -->
  12. <span
  13. class="absolute inset-0 opacity-0 group-hover:opacity-100 transition-opacity duration-500 before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces']"
  14. aria-hidden="true"
  15. ></span>
  16. </span>
  17. </span>

一旦我们完成了这一点,我们就可以继续设计“悬停内容”的样式:

  1. <!-- Fake Button -->
  2. <span
  3. class="relative p-0.5 rounded-full bg-slate-200 group-hover:bg-slate-800 transition duration-500 overflow-hidden flex items-center justify-center"
  4. >
  5. <span class="relative whitespace-nowrap">
  6. <!-- Default content: "Build the UI you need" -->
  7. <span
  8. class="block px-8 py-6 rounded-full bg-gradient-to-r from-slate-200 to-slate-100 z-10 group-hover:opacity-0 transition-opacity duration-500 ease-in-out"
  9. >Build the UI you need</span
  10. >
  11. <!-- Hover content: "Create beautiful user interfaces" -->
  12. <span
  13. class="absolute inset-0 rounded-full bg-gradient-to-r from-slate-900 to-slate-800 z-10 inline-flex items-center whitespace-nowrap overflow-hidden opacity-0 group-hover:opacity-100 transition-opacity duration-500 before:bg-clip-text before:text-transparent before:bg-gradient-to-r before:from-indigo-500 before:to-indigo-300 after:bg-clip-text after:text-transparent after:bg-gradient-to-r after:from-indigo-500 after:to-indigo-300 before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces'] before:px-2 after:px-2"
  14. aria-hidden="true"
  15. ></span>
  16. </span>
  17. </span>

最后,我们将添加更多类来定义文本最后部分的样式:

  1. <span class="group-hover:text-slate-300 transition-colors duration-500 ease-in-out">with Cruip</span>

就是这样!当您将鼠标悬停在按钮上时,我们创建了从浅色模式到深色模式的平滑过渡,并且添加了很酷的交叉淡入淡出效果。

现在我们可以继续最有趣的部分:创建文本滚动动画和按钮边框发光效果。

创建文本滚动动画
要创建这种类型的效果,我们需要做的就是在 Tailwind 配置文件中定义自定义动画。我们将调用它infinite-scroll并将其设置为从右向左滚动。这是代码:

  1. tailwind.config = {
  2. theme: {
  3. extend: {
  4. animation: {
  5. 'infinite-scroll': 'infinite-scroll 6s linear infinite',
  6. },
  7. keyframes: {
  8. 'infinite-scroll': {
  9. from: { transform: 'translateX(0)' },
  10. to: { transform: 'translateX(-100%)' },
  11. },
  12. },
  13. },
  14. },
  15. };

一旦我们设置了动画,我们就可以通过使用类轻松地将其应用到我们的伪元素animate-infinite-scroll。像这样:

  1. <!-- Hover content: "Create beautiful user interfaces" -->
  2. <span
  3. class="absolute inset-0 rounded-full bg-gradient-to-r from-slate-900 to-slate-800 z-10 inline-flex items-center whitespace-nowrap overflow-hidden opacity-0 group-hover:opacity-100 transition-opacity duration-500 before:bg-clip-text before:text-transparent before:bg-gradient-to-r before:from-indigo-500 before:to-indigo-300 after:bg-clip-text after:text-transparent after:bg-gradient-to-r after:from-indigo-500 after:to-indigo-300 before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces'] before:px-2 after:px-2 before:animate-infinite-scroll after:animate-infinite-scroll"
  4. aria-hidden="true"
  5. ></span>

创建闪耀效果
这是完成 CTA 的最后一步。为了制作发光效果,我们将使用线性渐变并将其应用于按钮元素的 before 伪属性。由于这个梯度有点复杂,我们将利用 Tailwind 的任意值功能来实现它。

为了真正使闪耀效果流行,我们还将为渐变添加无限旋转。我们不需要为此创建自定义动画。我们可以使用 Tailwind 的animate-spin,并使用自定义类来更改默认旋转时间,就像这样:animate-[spin_3s_linear_infinite]。

所以,这是更新后的代码:

  1. <section class="relative z-0">
  2. <div class="w-full max-w-5xl mx-auto px-4 md:px-6 py-48">
  3. <div class="text-center">
  4. <a
  5. class="flex flex-col lg:flex-row space-y-4 lg:space-y-0 lg:space-x-6 items-center justify-center text-3xl sm:text-4xl md:text-5xl font-semibold text-slate-900 before:absolute before:inset-0 hover:before:bg-slate-900 before:-z-10 before:transition-colors before:duration-500 group"
  6. href="#0"
  7. >
  8. <!-- Fake Button -->
  9. <span
  10. class="relative p-0.5 rounded-full bg-slate-200 group-hover:bg-slate-800 transition duration-500 overflow-hidden flex items-center justify-center before:opacity-0 group-hover:before:opacity-100 before:absolute before:w-1/2 before:pb-[100%] before:bg-[linear-gradient(90deg,_theme(colors.indigo.500/0)_0%,_theme(colors.indigo.500)_35%,_theme(colors.indigo.200)_50%,_theme(colors.indigo.500)_65%,_theme(colors.indigo.500/0)_100%)] before:animate-[spin_3s_linear_infinite]"
  11. >
  12. <span class="relative whitespace-nowrap">
  13. <!-- Default content: "Build the UI you need" -->
  14. <span
  15. class="block px-8 py-6 rounded-full bg-gradient-to-r from-slate-200 to-slate-100 z-10 group-hover:opacity-0 transition-opacity duration-500 ease-in-out"
  16. >Build the UI you need</span
  17. >
  18. <!-- Hover content: "Create beautiful user interfaces" -->
  19. <span
  20. class="absolute inset-0 rounded-full bg-gradient-to-r from-slate-900 to-slate-800 z-10 inline-flex items-center whitespace-nowrap overflow-hidden opacity-0 group-hover:opacity-100 transition-opacity duration-500 before:bg-clip-text before:text-transparent before:bg-gradient-to-r before:from-indigo-500 before:to-indigo-300 after:bg-clip-text after:text-transparent after:bg-gradient-to-r after:from-indigo-500 after:to-indigo-300 before:content-['Create_beautiful_user_interfaces'] after:content-['Create_beautiful_user_interfaces'] before:px-2 after:px-2 before:animate-infinite-scroll after:animate-infinite-scroll"
  21. aria-hidden="true"
  22. ></span>
  23. </span>
  24. </span>
  25. <!-- Text: "with Cruip" -->
  26. <span class="group-hover:text-slate-300 transition-colors duration-500 ease-in-out">with Cruip</span>
  27. </a>
  28. </div>
  29. </div>
  30. </section>

结论
凭借版本 3 中新的任意值功能,Tailwind CSS 已成为一个非常灵活的工具。它使我们能够制作复杂的效果,就像我们向您展示的那样,而无需编写一行 CSS。


P粉547719424
总阅读量:1004
关注
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

等待您完成支付...

请在支付页面继续完成支付

支付完成
重新选择支付方式