前端每日实战:16# 视频演示如何用纯 CSS 创做一个渐变色动画边框

图片描述

效果预览

按下右侧的“点击预览”按钮能够在当前页面预览,点击连接能够全屏预览。css

https://codepen.io/comehope/pen/odpRKXhtml

可交互视频教程

此视频是能够交互的,你能够随时暂停视频,编辑视频中的代码。git

请用 chrome, safari, edge 打开观看。github

https://scrimba.com/c/cmQV7Hdchrome

源代码下载

请从 github 下载。dom

https://github.com/comehope/front-end-daily-challenges/tree/master/016-colorful-gradient-animated-borderflex

代码解读

定义 dom,一个容器中包含一些文字:动画

<div class="box">
    you are my<br>
    FAVORITE
</div>

居中显示:spa

html,
body,
.box {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

设置页面背景色:code

body {
    background: #222;
}

设置容器和文字样式:

.box {
    color: white;
    font-size: 2.5em;
    width: 10em;
    height: 5em;
    background: #111;
    font-family: sans-serif;
    line-height: 1.5em;
    text-align: center;
    border-radius: 0.2em;
}

用伪元素增长一个背板:

.box {
    position: relative;
}

.box::after {
    content: '';
    position: absolute;
    width: 102%;
    height: 104%;
    background-color: orange;
    z-index: -1;
    border-radius: 0.2em;
}

把背板设置为渐变色的:

.box::after {
    /*background-color: orange;*/
    background-image: linear-gradient(60deg, aquamarine, cornflowerblue, goldenrod, hotpink, salmon, lightgreen, sandybrown, violet);
}

为背板设置动画效果:

.box::after {
    background-size: 300%, 300%;
    animation: animate_bg 5s ease infinite alternate;
}

@keyframes animate_bg {
    0% {
        background-position: 0%, 50%;
    }

    50% {
        background-position: 100%, 50%;
    }

    100% {
        background-position: 0%, 50%;
    }
}

最后,再为文字增长变色效果:

.box {
    animation: animate_text 2s linear infinite alternate;
}

@keyframes animate_text {
    from {
        color: lime;
    }

    to {
        color: yellow;
    }
}

大功告成!

知识点

相关文章
相关标签/搜索