<div class="buttons">
<h1>使用 <code>box-shadow</code> 制作一些简单的鼠标悬停效果</h1>
<button class="fill">填充</button>
<button class="pulse">脉冲</button>
<button class="close">闭合</button>
<button class="raise">上升</button>
<button class="up">向上填充</button>
<button class="slide">侧边填充</button>
<button class="offset">偏移</button>
</div>
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ... </head>
<body>
</body>
/*
https://developer.mozilla.org/en/docs/Web/CSS/box-shadow
box-shadow: [inset?] [top] [left] [blur] [size] [color];
提示:
把所有的模糊效果都设置为0,就能一个纯色的填充效果
加上inset关键字,这样box-shadow就会在元素的内部显示。
当你把鼠标悬停在上面时,动画效果会让元素从你指定的那一边开始填充(比如top和left接受负值来变成bottom和right)。
你可以堆叠多个阴影效果。
如果你正在动画多个阴影,记得在悬停/聚焦时保持与非悬停/聚焦时相同的阴影数量(即使你需要创建一个透明的阴影),这样动画才会更流畅。否则,动画就会显得断断续续的。
*/
// Animate the size, inside
.fill:hover,
.fill:focus {
box-shadow: inset 0 0 0 2em var(--hover);
}
// Animate the size, outside
.pulse:hover,
.pulse:focus {
animation: pulse 1s;
box-shadow: 0 0 0 2em transparent;
}
@keyframes pulse {
0% { box-shadow: 0 0 0 0 var(--hover); }
}
// Stack multiple shadows, one from the left, the other from the right
.close:hover,
.close:focus {
box-shadow:
inset -3.5em 0 0 0 var(--hover),
inset 3.5em 0 0 0 var(--hover);
}
// Size can also be negative; see how it's smaller than the element
.raise:hover,
.raise:focus {
box-shadow: 0 0.5em 0.5em -0.4em var(--hover);
transform: translateY(-0.25em);
}
// Animating from the bottom
.up:hover,
.up:focus {
box-shadow: inset 0 -3.25em 0 0 var(--hover);
}
// And from the left
.slide:hover,
.slide:focus {
box-shadow: inset 8.5em 0 0 0 var(--hover);
}
// Multiple shadows, one on the outside, another on the inside
.offset {
box-shadow:
0.3em 0.3em 0 0 var(--color),
inset 0.3em 0.3em 0 0 var(--color);
&:hover,
&:focus {
box-shadow:
0 0 0 0 var(--hover),
inset 6em 3.5em 0 0 var(--hover);
}
}
//=== Set button colors
// If you wonder why use Sass vars or CSS custom properties...
// Make a map with the class names and matching colors
$colors: (
fill: #a972cb,
pulse: #ef6eae,
close: #ff7f82,
raise: #ffa260,
up: #e4cb58,
slide: #8fc866,
offset: #19bc8b
);
// Sass variables compile to a static string; CSS variables are dynamic and inherited
// Loop through the map and set CSS custom properties using Sass variables
@each $button, $color in $colors {
.#{$button} {
--color: #{$color};
--hover: #{adjust-hue($color, 45deg)};
}
}
// Now every button will have different colors as set above. We get to use the same structure, only changing the custom properties.
button {
color: var(--color);
transition: 0.25s;
&:hover,
&:focus {
border-color: var(--hover);
color: #fff;
}
}
//=== Pen styling, ignore
body {
color: #fff;
background: hsl(227, 10%, 10%);
font: 300 1em 'Fira Sans', sans-serif;
// Center everything ever
justify-content: center;
align-content: center;
align-items: center;
text-align: center;
min-height: 100vh;
display: flex;
}
// Basic button styles
button {
background: none;
border: 2px solid;
font: inherit;
line-height: 1;
margin: 0.5em;
padding: 1em 2em;
}
h1 { font-weight: 400; }
code {
color: #e4cb58;
font: inherit;
}