js实现古诗打字机效果

前言

中秋快到了,又可以吟诗作对,品茶赏月了。那么我来分享一个使用js实现的打字机的展示效果,来显示我们的古诗。

效果图

代码实现

html部分

html 复制代码
<div class="content"></div>

js部分

javascript 复制代码
const text = "床前明月光\n疑似地上霜\n举头望明月\n低头思故乡"
const content = document.querySelector(".content");

var i = 0;
var timer;
(function typing() {
    content.innerHTML += text[i]
    i++
    timer = setTimeout(typing, 100)
    if (i == text.length) {
        clearTimeout(timer);
    }
})()

css部分

css 复制代码
* {
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Times New Roman', Times, serif;
    background-color: #000;
    background-size: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    position: relative;
}

.content {
    white-space: break-spaces;
    color: #fff;
    line-height: 30px;
    letter-spacing: 5px;
    font-size: 24px;
    text-shadow: white 0 0 5px;
    background-image: url("./moon.webp");
    background-size: cover;
    background-repeat: no-repeat;
    height: 100vh;
    min-width: 80vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

总结

实际实现代码非常简单,感兴趣的可以自己尝试以下。有问题的地方欢迎评论区指点。

相关推荐
程序员爱钓鱼4 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
PineappleCoder4 小时前
工程化必备!SVG 雪碧图的最佳实践:ID 引用 + 缓存友好,无需手动算坐标
前端·性能优化
JIngJaneIL5 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码5 小时前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web
澄江静如练_5 小时前
列表渲染(v-for)
前端·javascript·vue.js
JustHappy6 小时前
「chrome extensions🛠️」我写了一个超级简单的浏览器插件Vue开发模板
前端·javascript·github
Loo国昌6 小时前
Vue 3 前端工程化:架构、核心原理与生产实践
前端·vue.js·架构
sg_knight6 小时前
拥抱未来:ECMAScript Modules (ESM) 深度解析
开发语言·前端·javascript·vue·ecmascript·web·esm
LYFlied6 小时前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展