css 链接悬停状态的动画渐变下划线

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了css 链接悬停状态的动画渐变下划线相关的知识,希望对你有一定的参考价值。

Animated Gradient Underline for Link Hover State
------------------------------------------------
This pen shows how CSS gradients can be used to create an animated gradient underline for a link hover state with relative sizing. Link text and underline colour can be changed by updating a single CSS variable.

A [Pen](https://codepen.io/GeorgePark/pen/NXjwaN) by [George W. Park](https://codepen.io/GeorgePark) on [CodePen](https://codepen.io).

[License](https://codepen.io/GeorgePark/pen/NXjwaN/license).
<p>Hover over link to see animated gradient underline:</p>
<a href="#">link gradient underline</a>
@import url(https://fonts.googleapis.com/css?family=Great+Vibes);

*,
*::before,
*::after {
	box-sizing: border-box;
}

body {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	text-align: center;
	min-height: 100vh;
	padding: 1em;
	background-color: #fff;

	/* Link color variable containing RGB value */
	--link-color: 102, 153, 255;
}

p {
	font-family: Arial, sans-serif;
	font-size: calc(1em + 1.5vw);
	margin-bottom: .8em;
	color: #222;
}

a {
	position: relative;
	display: inline-block;
	font-family: "Great Vibes", serif;
	font-size: calc(1.5em + 5vw);
	text-decoration: none;
	color: rgb(var(--link-color));
}

a:hover::after {
	content: "";
	display: block;
	position: absolute;
	width: 100%;

	/* Adjust height and bottom values to work with selected font */
	height: .05em;
	bottom: .1em;

	/* Create gradient */
	background-image: linear-gradient(
		to right,
		transparent 0%,
		rgba(var(--link-color), .6) 30%,
		rgba(var(--link-color), .7) 50%,
		rgba(var(--link-color), .6) 70%,
		transparent 100%
	);

	/* Set-up fade-in animation */
	opacity: 0;
	animation: fadeIn 400ms ease-out forwards;
}

/* Fade-in animation */
@keyframes fadeIn {
	100% {
		opacity: 1;
	}
}

以上是关于css 链接悬停状态的动画渐变下划线的主要内容,如果未能解决你的问题,请参考以下文章

CSS 动画链接悬停状态

css 悬停下划线动画

链接悬停动画下划线

链接动画悬停上的下划线和箭头

如何从活动导航链接中删除悬停时不必要的附加下划线?

使用 jQuery 自动为所有悬停状态设置动画