CSS 所有样式在页面加载时应用过渡

标签 css animation css-transitions

我有一些代码可以在悬停时为按钮设置动画,但是当我重新加载我的页面时,css 会应用我在按钮和按钮文本上设置的 1s 过渡。

我将过渡设置为仅适用于按钮的宽度,这有效,但如果我想为多个事物设置动画,我必须选择只为那些设置动画,这有点烦人,因为我以前没有遇到过这个问题.

我在 Chromebook 上使用 Chrome 72.0.3626.122

html {
	width: 100%;
	height: 100%;
}

body,
span {
	margin: 0;
	padding: 0;
}

.button {
	width: 120px;
	height: 40px;
	border: none;
	background-color: salmon;
	transition: 1s ease;
}

.button:hover {
	width: 200px;
}

.button-text {
	color: white;
	font-size: 17px;
	transition: 1s ease;
}

.button:hover .button-text {
	color: lightgray;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Button hover</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<button class="button">
		<span class="button-text">Hover me</span>
	</button>
</body>
</html>

这会在页面重新加载后 1 秒内应用所有 css,而不是立即加载所有 css 并在悬停时转换

最佳答案

This applies all css in 1 second after page reload, instead of loading all css instantly and transitioning on hover

所以您希望按钮在页面加载 1 秒后自动变宽,文本变暗,对吗?

CSS 动画应该能帮到您,因为它们是自调用的,而 CSS 过渡需要触发事件。

// Call the keyframes
.button { animation: expand 0.55s linear 2s 1 normal forwards running; }
.button-text { animation: color-shift 0.55s linear 2s 1 normal forwards running; }

// Keyframes
@keyframes expand {
    0% { width: 120px; }
    100% { width: 200px; }
}

@keyframes color-shift {
    0% { color: white; }
    100% { color: lightgray; }
}

我已经使用您的 HTML 和 CSS 来显示差异 in this CodePen , 以及一些额外的细节。希望对您有所帮助。

关于CSS 所有样式在页面加载时应用过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55620068/

相关文章:

javascript - 如何更改 CSS 中元素旋转的速度

html - 当 flex-wrap 使用纯 CSS 包裹元素时移除 margin-right

twitter-bootstrap - twitter bootstrap 支持的转换类名是什么?

jquery - 从百分比到初始的 CSS 过渡

.net - 椭圆绘图 WPF 动画

css - 文字从左到右出现

Firefox CSS3 转换不工作

css - 仅显示跨度的第一个字母、三个点和最后一个 N 个字母

javascript - 设置动画持续时间的时刻

android - 如何在其中心点旋转一个 android 图标?