探索CSS动画下的按钮交互美学

ops/2024/10/25 7:20:41/

效果演示

这段代码通过SVG和CSS动画创建了一个具有视觉吸引力的按钮,当用户与按钮交互时(如悬停、聚焦或按下),按钮会显示不同的动画效果。
在这里插入图片描述

HTML

html"><button class="button"><div class="dots_border"></div><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="sparkle"><path class="path" stroke-linejoin="round" stroke-linecap="round" stroke="black" fill="black"d="M14.187 8.096L15 5.25L15.813 8.096C16.0231 8.83114 16.4171 9.50062 16.9577 10.0413C17.4984 10.5819 18.1679 10.9759 18.903 11.186L21.75 12L18.904 12.813C18.1689 13.0231 17.4994 13.4171 16.9587 13.9577C16.4181 14.4984 16.0241 15.1679 15.814 15.903L15 18.75L14.187 15.904C13.9769 15.1689 13.5829 14.4994 13.0423 13.9587C12.5016 13.4181 11.8321 13.0241 11.097 12.814L8.25 12L11.096 11.187C11.8311 10.9769 12.5006 10.5829 13.0413 10.0423C13.5819 9.50162 13.9759 8.83214 14.186 8.097L14.187 8.096Z"></path><path class="path" stroke-linejoin="round" stroke-linecap="round" stroke="black" fill="black"d="M6 14.25L5.741 15.285C5.59267 15.8785 5.28579 16.4206 4.85319 16.8532C4.42059 17.2858 3.87853 17.5927 3.285 17.741L2.25 18L3.285 18.259C3.87853 18.4073 4.42059 18.7142 4.85319 19.1468C5.28579 19.5794 5.59267 20.1215 5.741 20.715L6 21.75L6.259 20.715C6.40725 20.1216 6.71398 19.5796 7.14639 19.147C7.5788 18.7144 8.12065 18.4075 8.714 18.259L9.75 18L8.714 17.741C8.12065 17.5925 7.5788 17.2856 7.14639 16.853C6.71398 16.4204 6.40725 15.8784 6.259 15.285L6 14.25Z"></path><path class="path" stroke-linejoin="round" stroke-linecap="round" stroke="black" fill="black"d="M6.5 4L6.303 4.5915C6.24777 4.75718 6.15472 4.90774 6.03123 5.03123C5.90774 5.15472 5.75718 5.24777 5.5915 5.303L5 5.5L5.5915 5.697C5.75718 5.75223 5.90774 5.84528 6.03123 5.96877C6.15472 6.09226 6.24777 6.24282 6.303 6.4085L6.5 7L6.697 6.4085C6.75223 6.24282 6.84528 6.09226 6.96877 5.96877C7.09226 5.84528 7.24282 5.75223 7.4085 5.697L8 5.5L7.4085 5.303C7.24282 5.24777 7.09226 5.15472 6.96877 5.03123C6.84528 4.90774 6.75223 4.75718 6.697 4.5915L6.5 4Z"></path></svg><span class="text_button">点赞关注会变聪明~</span>
</button>
  • button:定义了一个按钮元素,它将应用下面的CSS样式。
  • dots_border:一个用于创建背景动画效果的DIV元素。
  • sparkle:一个SVG元素,包含三条路径(path),用于创建一个闪光效果。
  • text_button:按钮上显示的文本。

CSS

css">.button {--black-700: hsla(0 0% 12% / 1);--border_radius: 9999px;--transtion: 0.3s ease-in-out;--offset: 2px;cursor: pointer;position: relative;display: flex;align-items: center;gap: 0.5rem;transform-origin: center;padding: 1rem 2rem;background-color: transparent;border: none;border-radius: var(--border_radius);transform: scale(calc(1 + (var(--active, 0) * 0.1)));transition: transform var(--transtion);
}
.button::before {content: "";position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);width: 100%;height: 100%;background-color: var(--black-700);border-radius: var(--border_radius);box-shadow: inset 0 0.5px hsl(0, 0%, 100%), inset 0 -1px 2px 0 hsl(0, 0%, 0%),0px 4px 10px -4px hsla(0 0% 0% / calc(1 - var(--active, 0))),0 0 0 calc(var(--active, 0) * 0.375rem) hsl(260 97% 50% / 0.75);transition: all var(--transtion);z-index: 0;
}
.button::after {content: "";position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);width: 100%;height: 100%;background-color: hsla(260 97% 61% / 0.75);background-image: radial-gradient(at 51% 89%,hsla(266, 45%, 74%, 1) 0px,transparent 50%),radial-gradient(at 100% 100%, hsla(266, 36%, 60%, 1) 0px, transparent 50%),radial-gradient(at 22% 91%, hsla(266, 36%, 60%, 1) 0px, transparent 50%);background-position: top;opacity: var(--active, 0);border-radius: var(--border_radius);transition: opacity var(--transtion);z-index: 2;
}
.button:is(:hover, :focus-visible) {--active: 1;
}
.button:active {transform: scale(1);
}
.button .dots_border {--size_border: calc(100% + 2px);overflow: hidden;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);width: var(--size_border);height: var(--size_border);background-color: transparent;border-radius: var(--border_radius);z-index: -10;
}
.button .dots_border::before {content: "";position: absolute;top: 30%;left: 50%;transform: translate(-50%, -50%);transform-origin: left;transform: rotate(0deg);width: 100%;height: 2rem;background-color: white;mask: linear-gradient(transparent 0%, white 120%);animation: rotate 2s linear infinite;
}
@keyframes rotate {to {transform: rotate(360deg);}
}
.button .sparkle {position: relative;z-index: 10;width: 1.75rem;
}
.button .sparkle .path {fill: currentColor;stroke: currentColor;transform-origin: center;color: hsl(0, 0%, 100%);
}
.button:is(:hover, :focus) .sparkle .path {animation: path 1.5s linear 0.5s infinite;
}
.button .sparkle .path:nth-child(1) {--scale_path_1: 1.2;
}
.button .sparkle .path:nth-child(2) {--scale_path_2: 1.2;
}
.button .sparkle .path:nth-child(3) {--scale_path_3: 1.2;
}
@keyframes path {0%,34%,71%,100% {transform: scale(1);}17% {transform: scale(var(--scale_path_1, 1));}49% {transform: scale(var(--scale_path_2, 1));}83% {transform: scale(var(--scale_path_3, 1));}
}
.button .text_button {position: relative;z-index: 10;background-image: linear-gradient(90deg,rgb(238, 101, 10) 0%,hsla(0 0% 100% / var(--active, 0)) 120%);background-clip: text;font-size: 1.1rem;font-weight: 600;color: transparent;
}
  • .button:定义按钮的基本样式,包括光标样式、定位、显示方式、间距、填充、背景颜色、边框、圆角和变换。
  • .button::before和.button::after:使用伪元素创建按钮的背景和发光效果,包括阴影和渐变效果。
  • .button:is(:hover, :focus-visible):定义当鼠标悬停在按钮上或按钮获得焦点时的样式,包括激活状态的变量。
  • .button:active:定义按钮被按下时的样式,包括缩放效果。
  • .button .dots_border:定义背景动画效果的样式,包括位置、大小、圆角和层级。
  • .button .dots_border::before:定义背景动画的样式,包括位置、旋转动画和渐变效果。
  • @keyframes rotate:定义旋转动画的关键帧。
  • .button .sparkle:定义SVG元素的位置和层级。
  • .button .sparkle .path:定义SVG路径的样式,包括填充颜色和变换。
  • .button:is(:hover, :focus) .sparkle .path:定义当鼠标悬停或按钮获得焦点时SVG路径的动画效果。
  • @keyframes path:定义SVG路径的缩放动画的关键帧。
  • .button .text_button:定义按钮文本的样式,包括背景渐变和文本剪辑。

http://www.ppmy.cn/ops/128265.html

相关文章

[k8s理论知识]3.docker基础(二)隔离技术

容器其实是一种沙盒技术&#xff0c;其核心是通过约束和修改进程的动态表现&#xff0c;为其创建一个边界。这个边界确保了应用与应用之间不会相互干扰&#xff0c;同时可以方便在不同的环境中迁移&#xff0c;这是PaaS最理想的状态。 程序是代码的可执行镜像&#xff0c;通常…

React 列表 Keys

React 列表 & Keys 在 React 中,处理列表数据是构建用户界面时的常见需求。无论是展示一个待办事项列表,还是显示一组用户信息,理解如何使用列表和键(keys)都是至关重要的。本文将深入探讨 React 中列表和键的概念、用法,以及最佳实践。 列表渲染 在 React 中,渲…

使用flask构建一个简单的文件同步系统

使用Python构建文件同步系统&#xff1a;步骤指南 在当今互联网时代&#xff0c;能够在本地机器和远程服务器之间同步文件的能力变得至关重要。无论是备份重要文档、与团队成员共享大文件&#xff0c;还是在多个设备间保持数据一致性&#xff0c;一个强大的文件同步系统都能发…

Docker加载并运行别人的容器的同时挂在本地其他文件

配置环境失败后迫不得已入坑docker 踩坑1.sudo docker start hunyuandit_new Error response from daemon: could not select device driver "" with capabilities: [[gpu]] Error: failed to start containers: hunyuandit_new 解决方法&#xff1a; 安装Install…

PyQt 入门教程(3)基础知识 | 3.1、使用QtDesigner创建.ui文件

文章目录 一、使用QtDesigner创建.ui文件1、创建.ui文件2、生成.py文件3、使用新生成的.py文件4、编辑新生成的.py文件 一、使用QtDesigner创建.ui文件 1、创建.ui文件 打开PyCharm&#xff0c;使用自定义外部工具QtDesigner创建mydialog.ui文件&#xff0c;如下&#xff1a; …

植物健康,Spring Boot来保障

5系统详细实现 5.1 系统首页 植物健康系统需要登录才可以看到首页。具体界面的展示如图5.1所示。 图5.1 系统首页界面 5.2 咨询专家 可以在咨询专家栏目发布消息。具体界面如图5.2所示。 图5.2 咨询专家界面 5.3 普通植物检查登记 普通员工可以对普通植物检查登记信息进行添…

【深度学习代码调试5】标准化数据集:TensorFlow Datasets (TFDS)自动化数据加载与预处理

【标准化数据集】TensorFlow Datasets、TFDS&#xff1a;自动化数据加载与预处理 写在最前面1. 什么是 TensorFlow Datasets (TFDS)?主要特点&#xff1a; 2. TFDS 的核心 API&#xff1a;tfds.builder 和 download_and_preparetfds.builder&#xff1a;创建数据集构建器示例&…

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-18

计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-18 目录 文章目录 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-18目录1. On the Reliability of Large Language Models to Misinformed and Demographically-Informed Prompts2. SafeLLM: Dom…