炫酷的打字机效果

实现炫酷的打字机效果,需求:
打开网站光标闪烁,逐字出现,显示完整后光标消失


1、template中的代码

<div id="slogan"></div>
<div class="cursors" id="cursors"></div>

2、script中的代码


import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'

onMounted(() => {
  getSlogan()
})


//光标动画
let time = null
const getSlogan = () => {
  let index = 0;
  const typingContainer = document.getElementById('slogan');
  const cursorElement = document.getElementById('cursors');
  const text = 'AI数字员工,赋能智慧政务新未来'
  function typeLetter () {
    cursorElement.style.display = 'block'; // 开始显示光标
    if (index < text.length) {
      typingContainer.innerHTML += text.charAt(index);
      index++;
      time = setTimeout(typeLetter, 110); // 调整打字速度
    } else {
      cursorElement.style.display = 'none'; // 文字结束后隐藏光标
      clearTimeout(time)
      time = null
    }
  }

  typeLetter();
}

onBeforeUnmount(() => {
  clearTimeout(time)
})

3、

       #slogan {
          font-weight: 400;
          font-size: 36px;
          display: inline-block;
          // font-family: Arial, sans-serif;
          margin: 50px 0 20px;
        }

        .cursors {
          height: 36px;
          margin: 34px 0 0 2px;
          /* 初始不显示光标 */
          display: none;
          width: 10px;
          background-color: #fff;
          /* 光标 */
          animation: blink steps(40) 1s step-start infinite;
        }


       @keyframes blink {
        50% {
          background-color: transparent;
        }
      }


 

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值