|
|
1
Temani Afif
6 年前
这里是我的想法,我将只依赖CSS和少数元素。我将详细说明每个部分,然后我将结合成一个动画这一点。
对于图标部分(明星),我会做同样的事情,但我可能会考虑
grayscale
过滤器具有通用效果,可用于任何元素和任何颜色。
.magic i{
color:red;
filter:grayscale(100%);
}
.magic:hover i{
animation:change 1s forwards;
}
@keyframes change{
50% {
transform:scale(0);
filter:grayscale(100%);
}
51% {
filter:grayscale(0%);
}
100% {
transform:scale(1);
filter:grayscale(0%);
}
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<span class="magic">
<i class="fas fa-star fa-5x"></i>
</span>
对于圆,我只考虑一个元素,这里的诀窍是依靠边界着色和背景色。我们最初将高度/宽度设置为
0
我们只有边界,因此它将是一个完整的圆。然后我们简单地减少边界的厚度,同时保持总宽度不变。因此,我们将:
-
0
和边框宽度
-
我们增加边框宽度以创建缩放效果
-
我们在增加宽度/高度的同时减小边框宽度,以保持整体宽度/高度不变。
.circle {
display:inline-block;
width:0px;
height:0px;
border-radius:50%;
border-color:orange;
border-style:solid;
border-width:0px;
box-sizing:border-box;
}
body:hover .circle {
animation:change 1s forwards;
}
@keyframes change {
50% {
border-width:25px;
}
100% {
border-width:0;
width:50px;
height:50px;
}
}
body {
min-height:100px;
}
<span class="circle"></span>
此解决方案有一个小缺点,因为它将使元素从左上角而不是从中心增加。我们可以通过使用比例而不是更改宽度/高度来纠正此问题:
.circle {
display:inline-block;
width:50px;
height:50px;
border-radius:50%;
border-color:orange;
border-style:solid;
border-width:25px;
transform:scale(0);
box-sizing:border-box;
}
body:hover .circle {
animation:change 1s linear forwards;
}
@keyframes change {
50% {
transform:scale(1);
border-width:25px;
}
100% {
transform:scale(1);
border-width:0;
}
}
body {
min-height:100px;
}
<span class=“圆圈”></span>
我们仍然可以简化考虑一个简单的过渡:
.circle {
display:inline-block;
width:50px;
height:50px;
border-radius:50%;
border-color:orange;
border-style:solid;
border-width:25px;
transform:scale(0);
box-sizing:border-box;
transition:
transform 0.5s,
border-width 0.5s 0.5s;
}
body:hover .circle {
border-width:0;
transform:scale(1);
}
body {
min-height:100px;
}
<span class=“圆圈”></span>
现在是棘手的部分和小圆圈。为此,我将依靠
radial-gradient
和规模。我们的想法是在一个元素内创建带有渐变的小圆,并使用比例我们将创建扩展效果。
.small {
display:inline-block;
width:100px;
height:100px;
background:
/*4 reds*/
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
/*4 oranges*/
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%);
background-size:16px 16px;
background-position:
calc(50% - 30px) calc(50% - 30px),
calc(50% + 30px) calc(50% - 30px),
calc(50% - 30px) calc(50% + 30px),
calc(50% + 30px) calc(50% + 30px),
calc(50% + 0px) calc(50% + 40px),
calc(50% + 40px) calc(50% + 0px),
calc(50% - 40px) calc(50% + 0px),
calc(50% + 0px) calc(50% - 40px);
background-repeat:no-repeat;
border-radius:50%;
}
<span class="small"></span>
我已经创建了8个圆,并通过从中心偏移来放置它们(检查此答案以获得有关如何放置的更多详细信息)
background-position
作品:
https://stackoverflow.com/a/51734530/8620333
)。您只需根据需要调整圆圈的大小、位置和颜色。
以下是动画:
.small {
display:inline-block;
width:100px;
height:100px;
background:
/*4 reds*/
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
/*4 oranges*/
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%);
background-size:16px 16px;
background-position:
calc(50% - 30px) calc(50% - 30px),
calc(50% + 30px) calc(50% - 30px),
calc(50% - 30px) calc(50% + 30px),
calc(50% + 30px) calc(50% + 30px),
calc(50% + 0px) calc(50% + 40px),
calc(50% + 40px) calc(50% + 0px),
calc(50% - 40px) calc(50% + 0px),
calc(50% + 0px) calc(50% - 40px);
background-repeat:no-repeat;
border-radius:50%;
transform:scale(0);
transition:transform 0.5s,opacity 0.4s 0.4s;
}
body {
min-height:200px;
}
body:hover .small {
transform:scale(1);
opacity:0;
}
<span class=“small”></span>
如果你想要一个更精确的动画,你也可以考虑减少圆圈。
background-size
.
.small {
display:inline-block;
width:100px;
height:100px;
background:
/*4 reds*/
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
/*4 oranges*/
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%);
background-size:16px 16px; /*at least 2x7px */
background-position:
calc(50% - 30px) calc(50% - 30px),
calc(50% + 30px) calc(50% - 30px),
calc(50% - 30px) calc(50% + 30px),
calc(50% + 30px) calc(50% + 30px),
calc(50% + 0px) calc(50% + 40px),
calc(50% + 40px) calc(50% + 0px),
calc(50% - 40px) calc(50% + 0px),
calc(50% + 0px) calc(50% - 40px);
background-repeat:no-repeat;
border-radius:50%;
transform:scale(0);
transition:transform 0.5s,opacity 0.4s 0.4s,background-size 0.5s 0.4s;
}
body {
min-height:200px;
}
body:hover .small {
transform:scale(1);
opacity:0;
background-size:0 0;
}
<span class=“small”></span>
现在,您只需通过更改一些值对其他小圆圈执行相同的操作。
让我们把这些放在一起!
.magic {
display:inline-block;
margin:50px;
position:relative;
}
.magic i{
color:orange;
filter:grayscale(100%);
position:relative;
}
.magic:hover i{
animation:change 1s forwards;
}
@keyframes change{
50% {
transform:scale(0);
filter:grayscale(100%);
}
51% {
filter:grayscale(0%);
}
100% {
transform:scale(1);
filter:grayscale(0%);
}
}
/**/
.magic:before {
content:"";
position:absolute;
top:calc(50% - 45px);
left:calc(50% - 45px);
width:90px;
height:90px;
border-radius:50%;
border-color:orange;
border-style:solid;
border-width:45px;
transform:scale(0);
box-sizing:border-box;
}
.magic:hover::before {
transition:
transform 0.5s,
border-width 0.5s 0.5s;
border-width:0;
transform:scale(1);
}
/**/
.magic::after {
content:"";
position:absolute;
width:160px;
height:160px;
left:calc(50% - 80px);
top:calc(50% - 80px);
background:
/*4 reds*/
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
/*4 oranges*/
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%);
background-size:16px 16px;
background-position:
calc(50% - 50px) calc(50% - 50px),
calc(50% + 50px) calc(50% - 50px),
calc(50% - 50px) calc(50% + 50px),
calc(50% + 50px) calc(50% + 50px),
calc(50% + 0px) calc(50% + 70px),
calc(50% + 70px) calc(50% + 0px),
calc(50% - 70px) calc(50% + 0px),
calc(50% + 0px) calc(50% - 70px);
background-repeat:no-repeat;
border-radius:50%;
transform:scale(0);
}
.magic:hover:after {
transform:scale(1);
opacity:0;
background-size:0 0;
transition:
transform 0.5s 0.5s,
opacity 0.4s 0.9s,
background-size 0.5s 0.9s;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<span class="magic">
<i class="fas fa-star fa-5x"></i>
</span>
<span class="magic">
<i class="fas fa-user fa-5x"></i>
</span>
正如我所说的,它不是完美的,但非常接近您想要的,只需要较少的元素和所需的细节,因此您可以轻松地调整不同的值。它也很容易与任何图标一起使用,因为您只需向图标添加一个包装器。
为了简单起见,我没有添加小圆圈,但是我们可以考虑另一个伪元素,并且很容易添加它们。
.magic {
display:inline-block;
margin:50px;
position:relative;
}
.magic i{
color:orange;
filter:grayscale(100%);
}
.magic:hover i{
animation:change 1s forwards;
}
@keyframes change{
50% {
transform:scale(0);
filter:grayscale(100%);
}
51% {
filter:grayscale(0%);
}
100% {
transform:scale(1);
filter:grayscale(0%);
}
}
/**/
.magic:before {
content:"";
position:absolute;
top:calc(50% - 45px);
left:calc(50% - 45px);
width:90px;
height:90px;
border-radius:50%;
border-color:orange;
border-style:solid;
border-width:45px;
transform:scale(0);
box-sizing:border-box;
}
.magic:hover::before {
border-width:0;
transform:scale(1);
transition:
transform 0.5s,
border-width 0.5s 0.5s;
}
/**/
.magic::after,
.magic i::after{
content:"";
position:absolute;
width:160px;
height:160px;
left:calc(50% - 80px);
top:calc(50% - 80px);
background:
/*4 reds*/
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
radial-gradient(circle,red 50%,transparent 60%),
/*4 oranges*/
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%),
radial-gradient(circle,orange 50%,transparent 60%);
background-size:16px 16px;
background-position:
calc(50% - 50px) calc(50% - 50px),
calc(50% + 50px) calc(50% - 50px),
calc(50% - 50px) calc(50% + 50px),
calc(50% + 50px) calc(50% + 50px),
calc(50% + 0px) calc(50% + 70px),
calc(50% + 70px) calc(50% + 0px),
calc(50% - 70px) calc(50% + 0px),
calc(50% + 0px) calc(50% - 70px);
background-repeat:no-repeat;
border-radius:50%;
transform:scale(0);
}
.magic i::after {
background-size:10px 10px;
transform:rotate(10deg) scale(0);
}
.magic:hover:after {
transform:scale(1);
opacity:0;
background-size:0 0;
transition:transform 0.5s 0.5s,opacity 0.4s 0.9s,background-size 0.5s 0.9s;
}
.magic:hover i:after {
transform:rotate(10deg) scale(1);
opacity:0;
background-size:0 0;
transition:transform 0.5s 0.5s,opacity 0.4s 0.9s,background-size 0.5s 0.9s;
}
/**/
<link rel=“样式表”href=”https://use.fontawesome.com/releases/v5.7.1/css/all.css“>
<span class=“magic”>
<i class=“fas fa star fa-5x”></i>
<span class=“magic”>
<i class=“fas fa用户fa-5x”></i>
</span>
使现代化
.magic {
display:inline-block;
margin:50px;
position:relative;
--r:45px;
}
.magic i{
color:orange;
filter:grayscale(100%);
}
.magic:hover i{
animation:change 1s forwards;
}
@keyframes change{
50% {
transform:scale(0);
filter:grayscale(100%);
}
51% {
filter:grayscale(0%);
}
100% {
transform:scale(1);
filter:grayscale(0%);
}
}
/**/
.magic:before {
content:"";
position:absolute;
top:calc(50% - var(--r));
left:calc(50% - var(--r));
width:calc(2*var(--r));
height:calc(2*var(--r));
border-radius:50%;
border:solid orange var(--r);
transform:scale(0);
box-sizing:border-box;
}
.magic:hover::before {
border-width:0;
transform:scale(1);
transition:
transform 0.5s,
border-width 0.5s 0.5s;
}
/**/
.magic::after,
.magic i::after{
content:"";
position:absolute;
width: calc(4*var(--r));
height:calc(4*var(--r));
left:calc(50% - 2*var(--r));
top: calc(50% - 2*var(--r));
--c1:radial-gradient(circle,red 50% ,transparent 60%);
--c2:radial-gradient(circle,orange 50%,transparent 60%);
background:
/*4 reds*/
var(--c1),var(--c1),var(--c1),var(--c1),
/*4 oranges*/
var(--c2),var(--c2),var(--c2),var(--c2);
background-size:calc(var(--r)/3) calc(var(--r)/3);
background-position:
calc(50% - var(--r)) calc(50% - var(--r)),
calc(50% + var(--r)) calc(50% - var(--r)),
calc(50% - var(--r)) calc(50% + var(--r)),
calc(50% + var(--r)) calc(50% + var(--r)),
calc(50% + 0px) calc(50% + var(--r)*1.414),
calc(50% + var(--r)*1.414) calc(50% + 0px),
calc(50% - var(--r)*1.414) calc(50% + 0px),
calc(50% + 0px) calc(50% - var(--r)*1.414);
background-repeat:no-repeat;
transform:scale(0);
}
.magic i::after {
background-size:calc(var(--r)/5) calc(var(--r)/5);
transform:rotate(55deg) scale(0);
}
.magic:hover:after {
transform:scale(1);
opacity:0;
background-size:0 0;
transition:
transform 0.5s 0.5s,
opacity 0.4s 0.9s,
background-size 0.5s 0.9s;
}
.magic:hover i:after {
transform:rotate(55deg) scale(1);
opacity:0;
background-size:0 0;
transition:
transform 0.5s 0.5s,
opacity 0.4s 0.9s,
background-size 0.5s 0.9s;
}
/**/
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
<span class="magic" style="--r:80px;">
<i class="fas fa-star fa-10x"></i>
</span>
<span class="magic">
<i class="fas fa-user fa-5x"></i>
</span>
<span class="magic" style="--r:20px;">
<i class="far fa-bell fa-3x"></i>
</span>
基本上是变量
r
将定义整个形状的半径,您可以根据图标的大小轻松更改。
|