纯css3使用skew实现两边不规则边框动效

227 阅读1分钟

纯css3使用skew实现不规则边框

clip 属性剪裁绝对定位元素。

这个属性用于定义一个剪裁矩形。对于一个绝对定义元素,在这个矩形内的内容才可见。出了这个剪裁区域的内容会根据 overflow 的值来处理。剪裁区域可能比元素的内容区大,也可能比内容区小。

API

rect (top, right, bottom, left)

截屏2022-04-11 上午12.46.12.png 代码附上:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .box,
        .box::before,
        .box::after {
            position: absolute;

            top: 0;

            bottom: 0;

            left: 0;

            right: 0;

        }



        .box {

            width: 200px;

            height: 200px;

            margin: auto;

            color: #69ca62;

            box-shadow: inset 0 0 0 1px rgba(105, 202, 98, 0.5);

        }

        .box::before,
        .box::after {

            content: '';

            z-index: -1;

            margin: -5%;

            box-shadow: inset 0 0 0 2px;

            animation: clipMe 4s linear infinite;

        }

        .box::before {

            animation-delay: -2s;

        }

        .box:hover::after,
        .box:hover::before {
            background-color: rgba(255, 0, 0, 0.3);
        }

        @keyframes clipMe {
            0%,
            100% {
                clip: rect(0px, 220.0px, 2px, 0px);
            }
            25% {
                clip: rect(0px, 2px, 220.0px, 0px);
            }
            50% {
                clip: rect(218.0px, 220.0px, 220.0px, 0px);
            }

            75% {
                clip: rect(0px, 220.0px, 220.0px, 218.0px);
            }
        }

        html,
        body {
            height: 100%;

        }

        body {
            background-color: #0f222b;

        }
    </style>
    <div class="box">box</div>
    </body>

</html>