发布时间

两个漂亮的动态卡片

作者
  • avatar
    作者名字
    Kavin Wang
    Twitter

看了一个视频,按照视频中的说明,扣代码下来,表现良好。原视频是把卡片放在body下面直接使用的。 但当我给两个卡片进行包装时,出现了问题,特别是第一个卡片不能呈现出应该的交互效果。经过努力 修改,使得两个卡片都能正常呈现。

过去我一直忽视::before::after的使用,这算是个心病。一直想认真看看,懒病未愈,能拖就拖,一拖就拖到现在,这个心病算是基本解决了。

效果是,当鼠标移动到卡片上时,其边框会进行动态变化:

参考代码:

index.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="container">
        <div class="animated-card card-1">
            <span> 1st </span>
        </div>
        <div class="animated-card card-2">
            <span> 2nd </span> 
        </div>
    </div>
</body>

</html>

参考样式:

style.css
.container {
    display: flex;
    min-height: 100vh;
    justify-content: center;
    align-items: center;
    background: #000;
    gap: 5vw;
}
span {
    z-index: 10;
}
.animated-card {
    display: grid;
    place-items: center;
    position: relative;
    height: 500px;
    width: 350px;
    background: #222;
    border-radius: 10px;
    color: rgb(254, 254, 254, 0.1);
    cursor: pointer;
    font-size: 12em;
}
.animated-card:hover {
    color: #ccc;
}
.card-1::before {
    position: absolute;
    display: none;
    content: '';
    background: tomato;
    border-radius: 10px;
    transition: 0.3s;
    animation: animate 2s linear infinite;
}
.card-1:hover::before {
    display: block;
    height: calc(100% - 10px);
    width: calc(100% - 10px);
    background: transparent;
    border: 5px solid tomato;
}
.card-2 {
    overflow: hidden;
}
.card-2::before {
    position: absolute;
    content: '';
    width: 50%;
    height: 120%;
    background: #ff0800;
    transform: rotate(45deg);
}
.card-2:hover::before {
    animation: animate2 2s linear infinite;
}
.card-2::after {
    position: absolute;
    content: '';
    inset: 5px;
    background: #222;
    border-radius: 8px;
}
@keyframes animate {
    50% { filter: hue-rotate(350deg); }
}
@keyframes animate2 {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); filter: hue-rotate(-360deg) saturate(1000%); }
}

额外增加了一个,有兴趣可以试试:

.card-3:hover {
    background: repeating-conic-gradient(from var(--a), #ff2770 0%, #ff2770 5%, transparent 5%, transparent 40%, #ff2770 50%);
    animation: animate3 4s linear infinite;
}
.card-3:hover::before{
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    background: repeating-conic-gradient(from var(--a), #45f3ff 0%, #45f3ff 5%, transparent 5%, transparent 40%, #45f3ff 50%);
    animation: animate3 4s linear infinite;
    animation-delay: -1s;
    border-radius: 10px;
}

.card-3::after{
    position: absolute;
    content: '';
    inset: 5px;
    background: #222;
    border-radius: 10px;
}
 
@property --a {
    syntax: '<angle>';
    inherits: false;
    initial-value: 0deg;
}

@keyframes animate3 {
    0% {
        --a: 0deg;
    }
    100% {
        --a: 360deg;
    }
}