文字边框环绕效果的实现方式

最近有人要我帮做一个字段拖拽编辑功能的页面,由于页面上可编辑的字段有些多,那么如何突出显示当前正在被编辑的字段而不影响其编辑样式呢?于是我就脑洞大开地想到了边框环绕。

1、用原始虚线边框来实现边框环绕

css原始边框不支持环绕,于是我就想到了利用虚线边框加动画的方式来实现边框环绕。主要思路是被环绕的元素里面放四个额外的子元素,利用他们来做边框。

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            position: relative;
            margin: 100px auto;
            width: 200px;
            height: 200px;
            overflow: hidden;
        }
        div>span {
            position: absolute;
        }
        span:nth-of-type(1) {
            width: 200%;
            height: 1px;
            top: 0;
            right: 0;
            box-sizing: border-box;
            border-top: 1px dashed #333;
            animation: flash1 8s infinite linear;
        }
        span:nth-of-type(2) {
            width: 200%;
            left: 0;
            height: 1px;
            bottom: 0;
            box-sizing: border-box;
            border-top: 1px dashed #333;
            animation: flash2 8s infinite linear;
        }
        span:nth-of-type(3) {
            height: 200%;
            width: 1px;
            left: 0;
            top: 0;
            box-sizing: border-box;
            border-right: 1px dashed #333;
            animation: flash3 8s infinite linear;
        }
        span:nth-of-type(4) {
            height: 200%;
            bottom: 0;
            width: 1px;
            right: 0;
            box-sizing: border-box;
            border-right: 1px dashed #000;
            animation: flash4 8s infinite linear;
        }
        @keyframes flash1 {
            0% {
                right: 0;
            }
            100% {
                right: -100%;
            }
        }
        @keyframes flash2 {
            0% {
                left: 0;
            }
            100% {
                left: -100%;
            }
        }
        @keyframes flash3 {
            0% {
                top: 0;
            }
            100% {
                top: -100%;
            }
        }
        @keyframes flash4 {
            0% {
                bottom: 0;
            }
            100% {
                bottom: -100%;
            }
        }
    </style>
</head>
<body>
    <div>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
</body>
</html>

然而这种方式的效果并不好,于是我想到了用canvas来画虚线作为边框。

2、用canvas画边框来实现边框环绕

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            position: relative;
            margin: 100px auto;
            width: 200px;
            height: 200px;
            overflow: hidden;
        }
        div>canvas {
            position: absolute;
        }
        canvas:nth-of-type(1) {
            top: 0;
            right: 0;
            animation: flash1 12s infinite linear;
        }
        canvas:nth-of-type(2) {
            right: 0;
            bottom: 0;
            animation: flash2 12s infinite linear;
        }
        canvas:nth-of-type(3) {
            left: 0;
            bottom: 0;
            animation: flash3 12s infinite linear;
        }
        canvas:nth-of-type(4) {
            top: 0;
            left: 0;
            animation: flash4 12s infinite linear;
        }
        @keyframes flash1 {
            0% {
                right: 0;
            }
            100% {
                right: -100%;
            }
        }
        @keyframes flash2 {
            0% {
                bottom: 0;
            }
            100% {
                bottom: -100%;
            }
        }
        @keyframes flash3 {
            0% {
                left: 0;
            }
            100% {
                left: -100%;
            }
        }
        @keyframes flash4 {
            0% {
                top: 0;
            }
            100% {
                top: -100%;
            }
        }
    </style>
</head>
<body>
    <div id="fa">666
        <canvas id="canvas1" height="1" ></canvas>
        <canvas id="canvas2" width="1" ></canvas>
        <canvas id="canvas3" height="1" ></canvas>
        <canvas id="canvas4"  width="1"></canvas>
    </div>
    <script>
        Array.from(document.querySelectorAll('canvas')).forEach((_, i) => {            
            i % 2 === 0 ? _.width = 400 : _.height = 400;
            let ctx = _.getContext('2d');
            ctx.strokeStyle = '#000';
            ctx.setLineDash([5, 5]);
            ctx.beginPath();
            ctx.moveTo(0, 0);
            i % 2 === 0 ? ctx.lineTo(_.width, 0) : ctx.lineTo(0, _.height);
            ctx.stroke();
        });
    </script>
</body>
</html>

注意为了确保四条边移动的速度一致,四条边的动画移动距离和执行动画的时间的比例要相等,不懂的话全部写成一样就行了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,514评论 2 59
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,054评论 3 119
  • 天足辽枰伪辶,我见到了品不圭环“人,邴个时厌岳亥在居条1勁邳7
    读简书的人阅读 1,558评论 1 1
  • throttle()是函数调用的频度控制器,控制连续执行时的时间间隔。 主要应用场景: 鼠标移动,mousemov...
    勿以浮沙筑高台阅读 1,720评论 0 0
  • 即墨如是也。。。。
    ziuu阅读 3,173评论 0 0

友情链接更多精彩内容