前端打字效果

页面效果链接,点击查看icon-default.png?t=N7T8https://live.csdn.net/v/419208?spm=1001.2014.3001.5501

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打字机效果</title>
    <style>
        .flex {
            display: flex;
        }

        .flex-1 {
            flex: 1;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>

    <div id="app">
        <div v-for="(item, index) in clientpreferenceVOListCopy">
            <div class="flex">
                <div>
                    {{ item.type }}
                </div>:
                <div class="flex-1" v-html="item.summary"></div>
            </div>
        </div>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                clientpreferenceVOList: [
                    { "type": "意外", "summary": "虽然客户目前为赋闲大众,但生活中总会有意外发生。根据中国保险监督管理委员会的数据,2019年中国交通事故死亡人数约为3.7万人,平均每天有877人因交通事故而丧生。意外风险不仅包括交通事故,还包括家庭意外、职业意外等。考虑到客户目前为单身贵族,可能需要承担更多的家庭责任,因此意外风险不容忽视。购买意外险可以为客户提供意外伤害的保障,确保在意外发生时能够得到及时的经济支持。" },
                    { "type": "医疗", "summary": "客户目前无社保,意味着在医疗方面可能会面临较大的经济压力。根据中国保险监督管理委员会的数据,2019年中国医疗费用总支出约为6.5万亿元,其中个人自付比例约为20%,而客户作为单身贵族,可能需要承担更多的自付费用。医疗风险不仅包括治疗费用,还包括药物费用、检查费用、住院费用等。因此,购买医疗保险对于客户来说至关重要,可以有效减轻因疾病带来的经济负担。" },
                    { "type": "重疾", "summary": "客户目前为36岁-45岁,处于单身贵族阶段,无家庭负担,但关注点为医疗健康。这意味着客户可能面临较高的重疾风险。根据统计数据,中国新发恶性肿瘤的发病数每年都在增加,且发病年龄呈下降趋势,35-44岁组发病人数占全部恶性肿瘤发病人数的40%,且发病与工作压力、生活压力、环境污染、不良生活习惯等密切相关。考虑到客户目前的职业为赋闲大众,生活压力可能较大,因此重疾风险不容忽视。建议客户购买重疾险,以减轻因重疾带来的经济负担,保障自己能够得到及时和有效的治疗。" }
                ],
                clientpreferenceVOListCopy: [],
                index: 0,
                ind: 0,
                timer: null
            },
            mounted() {
                this.typeWriter();
            },
            methods: {
                typeWriter() {
                    const item = this.clientpreferenceVOList[this.ind];
                    let o = { type: item.type, summary: '' };
                    this.clientpreferenceVOListCopy.push(o);
                    console.log('this.clientpreferenceVOListCopy', this.clientpreferenceVOListCopy);
                    this.timer = setInterval(() => {
                        this.dealO();
                    }, 50);
                },
                dealO() {
                    console.log('this.index', this.index, this.clientpreferenceVOList[this.ind].summary.charAt(this.index));
                    if (this.index < this.clientpreferenceVOList[this.ind].summary.length) {
                        // 将当前字符添加到文本元素中
                        this.clientpreferenceVOListCopy[this.ind].summary += this.clientpreferenceVOList[this.ind].summary.charAt(this.index);
                        this.index++;
                    } else {
                        // 设置延迟后继续执行函数
                        clearInterval(this.timer);
                        this.timer = null;
                        this.index = 0;
                        this.ind++;
                        if (this.ind <= 2) {
                            this.typeWriter();
                        }
                    }
                }
            },
            beforeDestroy() {
                // 清除定时器
                clearInterval(this.timer);
                this.timer = null;
            }
        })
    </script>

</body>

</html>

### 前端实现打字效果的方法与代码示例 在前端开发中,实现打字效果可以通过多种方式完成。以下将详细介绍几种常见的实现方法,并提供相应的代码示例。 #### 1. 使用纯 HTML 和 CSS 实现 通过 CSS 的 `animation` 属性,可以模拟逐打印的效果。这种方式适用于简单的场景,但无法态控制文本内容或速度。 ```html <div class="typewriter"> Hello World! </div> <style> .typewriter { overflow: hidden; /* 确保文本溢出时不可见 */ white-space: nowrap; /* 防止换行 */ width: 0; animation: typing 2s steps(12, end); /* 模拟逐打印 */ } @keyframes typing { from { width: 0 } to { width: 100% } } </style> ``` 这种方法简单高,但功能有限[^1]。 #### 2. 结合 JavaScript 实现更复杂的行为 如果需要态控制文本内容、打印速度或其他参数,可以引入 JavaScript 来增强效果。 ```html <div id="typing"></div> <script> let text = "Hello World!"; let i = 0; function typeWriter() { if (i < text.length) { document.getElementById("typing").innerHTML += text.charAt(i); i++; setTimeout(typeWriter, 100); // 调整打印速度 } } typeWriter(); </script> ``` 此方法允许开发者灵活调整打印速度和内容,适合大多数需求[^1]。 #### 3. 使用第三方库 Typed.js Typed.js 是个流行的 JavaScript 库,用于轻松实现打字效果。它提供了丰富的配置选项,如循环播放、光标样式等。 ##### 安装 Typed.js 可以通过 npm 或直接引用 CDN 安装: ```bash npm install typed.js ``` 或者使用 CDN: ```html <script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12"></script> ``` ##### 使用 Typed.js ```html <div id="typed-text"></div> <script> const options = { strings: ["Hello World!", "Welcome to the Typing Effect!"], typeSpeed: 50, backSpeed: 30, loop: true }; const typed = new Typed("#typed-text", options); </script> ``` Typed.js 提供了强大的功能,例如多段文本循环、删除画等[^2]。 #### 4. 在 Vue 3 中使用 TypeIt 实现 对于基于 Vue 的项目,TypeIt 是个优秀的选择。它可以无缝集成到 Vue 3 中,并支持丰富的配置选项。 ##### 安装 TypeIt ```bash npm install typeit ``` ##### Vue 3 示例代码 ```vue <template> <div ref="typingElement"></div> </template> <script> import { onMounted, ref } from "vue"; import TypeIt from "typeit"; export default { setup() { const typingElement = ref(null); onMounted(() => { new TypeIt(typingElement.value, { strings: "Hello World!", speed: 50, cursor: true }).go(); }); return { typingElement }; } }; </script> ``` TypeIt 的优势在于其易用性和灵活性,特别适合现代框架的开发环境[^3]。 #### 5. 使用 Typewriter-SSE 实现 Typewriter-SSE 是个基于服务器发送事件(SSE)的库,适用于需要实时数据更新的场景。 ##### 示例代码 ```javascript import TypewriterSSE from 'typewriter-sse'; const writer = new TypewriterSSE({ container: '#output', endpoint: '/sse?q=hello world', cursor: { blink: true, color: '#0f0', char: '|' }, onComplete: () => console.log('Finished typing!') }); writer.start(); ``` Typewriter-SSE 适合需要与后端交互的态场景[^4]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值