我输入,你生成代码,手写代码生成

简介: 我输入,你生成代码,手写代码生成

文章目录


先写个打字特效


借助Vue完成变量的替换


image.png

点击查看原视频链接


因为之前觉得这个打字特效很好玩,但是字数不一样就要重写或者改代码,麻烦,之前就看到网上有一些自动生成代码的工具,很好奇怎么实现的?于是我就想能不能自己也写一个自动生成代码的网页,别光想,戴上耳机,音乐陪伴,行动起来,30分钟,完成了下面的300行代码,实现了这个功能,通过这个,你大概就能理解一些所谓简单复杂的工具,其实也不过如此,你也可以🏄🏼


先写个打字特效



首先先写一个固定字长的打字特效,这就很easy了,思路就是使用 ch单位,1ch表示一个字长,再借助等宽字体,开局宽度 0ch ,动画过渡到字长,借助伪元素实现打字时的小横杠,这里使用了 css 变量,因为方便后期对变量的替换以及维护,推荐👍🏼


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>typora</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="https://img.fy6b.com/2022/04/26/f8fd03d60efd6.png" type="image/x-icon">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        :root {
            --Length: 6ch;
            --num: 6;
            --Time: 3s;
        }
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }
        p {
            position: relative;
            font-size: 60px;
            font-family: monospace;
            width: 0ch;
            overflow: hidden;
            white-space: nowrap;
            animation: Lengthen var(--Time) steps(var(--num)) forwards;
        }
        p::after {
            content: "";
            position: absolute;
            right: 0;
            width: 2px;
            height: 100%;
            background-color: #454545;
            animation: Flash 1s infinite;
        }
        @keyframes Lengthen {
            to {
                width: var(--Length);
            }
        }
        @keyframes Flash {
            50% {
                background-color: transparent;
            }
        }
    </style>
</head>
<body>
<p>typora</p>
</body>
</html>

1.gif


借助Vue完成变量的替换



接下来就是重点了,这里我使用了Vue框架,更快速点,原生js一样的,重点是获取css变量,根据输入的length为css变量从新赋值,然后通过字符串拼接,完成代码的重用,可能很笨,但说起来只能这样,哈哈,还有就是点击复制这个实现,通过input框,这里切记不要使用display隐藏input,而是使用opacity = 进行隐藏


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>在线生成打字特效</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="http://zhouql.vip/images/ali/键盘,打字.png" type="image/x-icon">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            min-height: 100vh;
        }
        :root {
            --Length: 24ch;
            --num: 24;
            --Time: 4s;
        }
        #app {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .inputText {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 90%;
            margin: 15px auto;
        }
        input {
            outline: none;
            width: calc(100% - 90px);
            height: 40px;
            text-indent: .3em;
            font-size: 14px;
            font-family: "Microsoft YaHei UI Light";
            letter-spacing: .1em;
            border: 1px solid #69F;
            border-right: transparent;
            border-radius: 5px 0 0 5px;
        }
        button {
            border: none;
            background-color: #69F;
            height: 40px;
            width: 90px;
            color: #FFF;
            letter-spacing: .1em;
            padding: 5px;
            font-size: 16px;
            border-radius: 0 5px 5px 0;
            cursor: pointer;
            outline: none;
            font-family: "Microsoft YaHei UI Light";
        }
        .code {
            position: relative;
            width: 90%;
            height: 30px;
            background-color: transparent;
            overflow: auto;
        }
        p {
            position: relative;
            font-size: 20px;
            font-family: monospace;
            width: 0ch;
            overflow: hidden;
            white-space: nowrap;
            animation: Lengthen var(--Time) steps(var(--num)) forwards;
        }
        p::after {
            content: "";
            position: absolute;
            right: 0;
            width: 2px;
            height: 100%;
            background-color: #454545;
            animation: Flash 1s infinite;
        }
        @keyframes Lengthen {
            to {
                width: var(--Length);
            }
        }
        @keyframes Flash {
            50% {
                background-color: transparent;
            }
        }
        .co {
            position: relative;
            width: 90%;
            height: calc(100vh - 250px);
            overflow: auto;
            border: 1px solid pink;
        }
        .co .copyButton {
            position: absolute;
            width: 80px;
            height: 30px;
            border-radius: 0px;
            top: 5px;
            right: 8px;
            font-size: 13px;
        }
        .preView {
            width: 90%;
            display: inline-block;
            text-align: left;
            padding: 10px 1px;
            font-size: 15px;
            color: #555;
        }
        footer {
            position: absolute;
            bottom: 0px;
            width: 100%;
            height: 30px;
            background-color: #f3f3f3;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        footer span {
            font-size: 12px;
            color: #999;
        }
        footer span a {
            text-decoration: none;
            color: #008600;
        }
        [v-clock] {
            display: none;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="inputText">
        <input autofocus @input="cleanCode" maxlength="28" type="text" placeholder="输入字" v-model="typing">
        <button @click="generateCode">生成代码</button>
    </div>
    <span class="preView" v-show="flag">预览</span>
    <div class="code" v-show="flag">
        <p>{{zi}}</p>
    </div>
    <span class="preView viewCode" v-show="flag">源码</span>
    <div class="co" v-show="flag">
        <button class="copyButton" @click="copyCode">点击复制</button>
        <pre>
            <code v-cloak id="copyCodeAll">
                {{daziCode}}
            </code>
        </pre>
    </div>
    <input type="text" class="preCopyCode" style="opacity: 0;">
    <footer>
        <span>学习使用,豫备案号 <a href="https://seo.chinaz.com/zhouql.vip">zhouql.vip</a></span>
    </footer>
</div>
<!--引入Vue2-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!--创建Vue实例对象-->
<script>
    const vm = new Vue({
        el: '#app',
        data: {
            //要打的字
            typing: 'I Love You zhang yu miao',
            //决定显示还是隐藏
            flag:false,
            zi: '',
            //待复制的代码
            preCopyCode:''
        },
        computed: {
            daziCode() {
                return '\n' +
                    '<!DOCTYPE html>\n' +
                    '<html lang="en">\n' +
                    '<head>\n' +
                    '    <meta charset="UTF-8">\n' +
                    '    <title>'+this.typing+'</title>\n' +
                    '    <meta http-equiv="X-UA-Compatible" content="IE=edge">\n' +
                    '    <meta name="viewport" content="width=device-width, initial-scale=1.0">\n' +
                    '    <link rel="shortcut icon" href="https://img.fy6b.com/2022/04/26/f8fd03d60efd6.png" type="image/x-icon">\n' +
                    '    <style>\n' +
                    '        * {\n' +
                    '            margin: 0;\n' +
                    '            padding: 0;\n' +
                    '            box-sizing: border-box;\n' +
                    '        }\n' +
                    '\n' +
                    '        :root {\n' +
                    '            --Length: '+this.typing.length+'ch;\n' +
                    '            --num: '+this.typing.length+';\n' +
                    '            --Time: '+(this.typing.length * 0.5)+'s;\n' +
                    '        }\n' +
                    '\n' +
                    '        body {\n' +
                    '            display: flex;\n' +
                    '            align-items: center;\n' +
                    '            justify-content: center;\n' +
                    '            min-height: 100vh;\n' +
                    '        }\n' +
                    '\n' +
                    '        p {\n' +
                    '            position: relative;\n' +
                    '            font-size: 60px;\n' +
                    '            font-family: monospace;\n' +
                    '            width: 0ch;\n' +
                    '            overflow: hidden;\n' +
                    '            white-space: nowrap;\n' +
                    '            animation: Lengthen var(--Time) steps(var(--num)) forwards;\n' +
                    '        }\n' +
                    '\n' +
                    '        p::after {\n' +
                    '            content: "";\n' +
                    '            position: absolute;\n' +
                    '            right: 0;\n' +
                    '            width: 2px;\n' +
                    '            height: 100%;\n' +
                    '            background-color: #454545;\n' +
                    '            animation: Flash 1s infinite;\n' +
                    '        }\n' +
                    '\n' +
                    '        @keyframes Lengthen {\n' +
                    '            to {\n' +
                    '                width: var(--Length);\n' +
                    '            }\n' +
                    '        }\n' +
                    '\n' +
                    '        @keyframes Flash {\n' +
                    '            50% {\n' +
                    '                background-color: transparent;\n' +
                    '            }\n' +
                    '        }\n' +
                    '    </style>\n' +
                    '</head>\n' +
                    '<body>\n' +
                    '<p>'+this.typing+'</p>\n' +
                    '</body>\n' +
                    '</html>\n'
            }
        },
        methods: {
            generateCode() {
                this.zi = this.typing;
                document.documentElement.style.setProperty("--Length", (this.typing.length) + 'ch');
                document.documentElement.style.setProperty("--num", this.typing.length + "");
                document.documentElement.style.setProperty("--Time", this.typing.length * 0.4 + "s");
                if (this.typing.length != 0) {
                    this.flag = true;
                }
            },
            cleanCode() {
                this.flag = false;
                this.typing = this.typing.replace(/[^a-zA-Z .() 0-9;:""?{}=+-]/g, '');
            },
            copyCode() {
                let code = document.querySelector(".preCopyCode");
                code.value = this.daziCode;
                code.select();
                document.execCommand("Copy");
                alert("复制成功")
            }
        }
    })
</script>
</body>
</html>


相关文章
|
前端开发 安全 Java
Spring Boot 三招组合拳,手把手教你打出优雅的后端接口
Spring Boot 三招组合拳,手把手教你打出优雅的后端接口
220 0
|
存储 机器学习/深度学习 算法
聚合签名、门限签名、Multisigs 和多签名
该文章深入探讨了数字签名在区块链技术中的应用,并比较了聚合签名、门限签名和多签名三种方案的异同,同时介绍了MuSig2和FROST这两种旨在提高区块链平台多签名或门限签名效率的提案。
338 3
聚合签名、门限签名、Multisigs 和多签名
|
监控 NoSQL 算法
Redis集群模式:高可用性与性能的完美结合!
小米探讨Redis集群模式,通过一致性哈希分散负载,主从节点确保高可用性。节点间健康检测、主备切换、数据复制与同步、分区策略和Majority选举机制保证服务可靠性。适合高可用性及性能需求场景,哨兵模式则适用于简单需求。一起学习技术的乐趣!关注小米微信公众号“软件求生”获取更多内容。
509 11
Redis集群模式:高可用性与性能的完美结合!
|
消息中间件 监控 算法
Kafka Producer 的性能优化技巧
【8月更文第29天】Apache Kafka 是一个分布式流处理平台,它以其高吞吐量、低延迟和可扩展性而闻名。对于 Kafka Producer 来说,正确的配置和编程实践可以显著提高其性能。本文将探讨一些关键的优化策略,并提供相应的代码示例。
558 2
|
存储 运维 安全
Linux命令stat:深入了解文件与文件系统状态
`stat`命令在Linux中用于显示文件和文件系统的详细状态,包括权限、大小、时间戳等。它通过读取inode获取信息,特点是显示全面、易用且支持多种参数,如`-c`自定义格式,`-f`查看文件系统状态,`-L`处理符号链接。例如,`stat example.txt`显示文件详情,`stat -c &quot;%n 的大小是 %s 字节&quot; example.txt`输出文件大小。理解`stat`有助于系统管理和故障排查。
Flowable流程的挂起与激活详解
Flowable流程的挂起与激活详解
674 1
自定义View实现点击事件
自定义View实现点击事件
91 2
|
存储 JavaScript 前端开发
11.Vuex
11.Vuex
77 0
无源晶振匹配电容—计算方法
无源晶振匹配电容—计算方法
446 0
|
Java C++ 索引
leetcode-707:设计链表
leetcode-707:设计链表
92 0

热门文章

最新文章