原生JS给可编辑DIV添加文字阴影

2020-11-26 10:32:12 浏览数 (1)

给大家分享一个用原生JS给可编辑DIV添加文字阴影的特效,效果如下:

以下是代码实现,欢迎大家复制粘贴和收藏。

代码语言:javascript复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生JS给可编辑DIV添加文字阴影</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-family: '微软雅黑', sans-serif;
        }

        body {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background: #00fff3;
        }

        #text {
            position: relative;
            color: #fff;
            font-size: 9em;
            font-weight: 700;
            outline: none;
            text-align: center;
            text-shadow: 1px 1px 0px #01d1d3;
        }
    </style>
</head>

<body>
    <div id="text" contenteditable>Long Shadow</div>
    <script>
        let text = document.getElementById('text')
        let shadow = ''
        for (let i = 0; i < 500; i  ) {
            shadow  = (shadow ? ',' : '')   `${i * 1}px ${i * 1}px 0px #01d1d3`
        }
        text.style.textShadow = shadow
    </script>
</body>

</html>

0 人点赞