打字机效果的 Hitokoto(html+css+js)

my blog
Posts: 打字机效果的 Hitokoto(html+css+js)

打字机效果的 Hitokoto(html+css+js)

效果


{% video “https://assets.buasis.eu.org/posts/Posts-打字机效果的-Hitokoto(html-css-js)/21-17-37.mp4” %}

文本将写在<span class="text" id="hitokoto_text">:D 获取中...</span>

JavaScript 中

const typingSpeed = 180; // 定义打字速度(毫秒)
const deletingSpeed = 100; // 定义删除速度(毫秒)
const delayBetweenCycles = 2500; // 在句子完成显示和删除之前的延迟时间(毫秒)

完整源码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>打字机效果的 Hitokoto</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: rgb(43, 52, 82);
        }
        .text {
            font-family: 'fangsong';
            display: inline-block;
            position: relative;
            font-size: 40px;
            height: 60px;
            line-height: 60px;
            color: rgb(245, 245, 245);
        }
        .text::after {
            content: '';
            position: absolute;
            right: -10px;
            top: 5px;
            height: 50px;
            width: 3px;     
            background-color: #fff;
            animation: blink 0.5s steps(1) infinite;
        }
        @keyframes blink {
            0%, 100% {
                background-color: #fff;
            }
            50% {
                background-color: transparent;
            }
        }
    </style>
</head>
<body>
    <h1>
        <span class="text" id="hitokoto_text">:D 获取中...</span>
    </h1>
    <script>
        const textElement = document.querySelector('.text');
        let currentIndex = 0;
        let charIndex = 0;
        let isDeleting = false;
        let hitokotoText = '';

        const typingSpeed = 180; // 定义打字速度(毫秒)
        const deletingSpeed = 100; // 定义删除速度(毫秒)
        const delayBetweenCycles = 2500; // 在句子完成显示和删除之前的延迟时间(毫秒)

        function typeWriter() {
            if (isDeleting) {
                textElement.innerHTML = hitokotoText.slice(0, charIndex--);
            } else {
                textElement.innerHTML = hitokotoText.slice(0, ++charIndex);
            }

            if (!isDeleting && charIndex === hitokotoText.length) {
                setTimeout(() => isDeleting = true, delayBetweenCycles);
            } else if (isDeleting && charIndex === 0) {
                isDeleting = false;
                fetchHitokoto();
            }

            const speed = isDeleting ? deletingSpeed : typingSpeed;
            setTimeout(typeWriter, speed);
        }

        function fetchHitokoto() {
            fetch('https://v1.hitokoto.cn')
                .then(response => response.json())
                .then(data => {
                    hitokotoText = data.hitokoto;
                    typeWriter();
                })
                .catch(console.error);
        }

        fetchHitokoto();
    </script>
</body>
</html>

参考资料

  1. 打字机效果 html+css+js
  2. 语句接口 - 一言
  • 9
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值