使用Vue实现半自动打字机特效

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。使用Vue实现半自动打字机特效,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

最近遇到一个需求就是使用Web网页实现打字机特效,网上搜了一下相关插件,发现不完全符合我们的需求,于是手写实现一个半自动的打字机。

1、源码如下

<template>
  <div class="type-writer">
    <p class="type-writer-line">{{ line_1.typewriter }}<i class="type-writer-cursor" v-if="line_1.visible" /></p>
    <p class="type-writer-line">{{ line_2.typewriter }}<i class="type-writer-cursor" v-if="line_2.visible" /></p>
    <p class="type-writer-line">{{ line_3.typewriter }}<i class="type-writer-cursor" v-if="line_3.visible" /></p>
    <p class="type-writer-line">{{ line_4.typewriter }}<i class="type-writer-cursor" v-if="line_4.visible" /></p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 第一行文字
      line_1: {
        content: `git add '我的2023年度开端'`,
        typewriter: ``,
        index: 0,
        visible: false,
        timer: null
      },

      // 第二行文字
      line_2: {
        content: `git commit -m 'Hello,2023!'`,
        typewriter: ``,
        index: 0,
        visible: false,
        timer: null
      },

      // 第三行文字
      line_3: {
        content: `git pull`,
        typewriter: ``,
        index: 0,
        visible: false,
        timer: null
      },

      // 第四行文字
      line_4: {
        content: `git push`,
        typewriter: ``,
        index: 0,
        visible: false,
        timer: null
      },

      
    };
  },
  mounted() {
    this.typingLine_1(this.line_1)
  },
  methods: {
    /**
     * 打印第一行文字
     */
    typingLine_1(obj) {
      obj.visible = true
      if (obj.index <= obj.content.length) {
        obj.typewriter = obj.content.slice(0, obj.index ++)
        obj.timer = setTimeout(() => { this.typingLine_1(obj) }, 50)
      } else {
        clearTimeout(obj.timer)
        obj.visible = false
        this.typingLine_2(this.line_2);
      }
    },

    /**
     * 打印第二行文字
     */
    typingLine_2(obj) {
      obj.visible = true
      if (obj.index <= obj.content.length) {
        obj.typewriter = obj.content.slice(0, obj.index ++)
        obj.timer = setTimeout(() => { this.typingLine_2(obj) }, 50)
      } else {
        clearTimeout(obj.timer)
        obj.visible = false
        this.typingLine_3(this.line_3);
      }
    },

    /**
     * 打印第三行文字
     */
    typingLine_3(obj) {
      obj.visible = true
      if (obj.index <= obj.content.length) {
        obj.typewriter = obj.content.slice(0, obj.index ++)
        obj.timer = setTimeout(() => { this.typingLine_3(obj) }, 50)
      } else {
        clearTimeout(obj.timer)
        obj.visible = false
        this.typingLine_4(this.line_4);
      }
    },

    /**
     * 打印第四行文字
     */
    typingLine_4(obj) {
      obj.visible = true
      if (obj.index <= obj.content.length) {
        obj.typewriter = obj.content.slice(0, obj.index ++)
        obj.timer = setTimeout(() => { this.typingLine_4(obj) }, 50)
      } else {
        clearTimeout(obj.timer)
      }
    },
  },
};
</script>

<style lang="less" scoped>
.type-writer {
  width: auto;
  padding: 100px;
  background-color: #f9f2e1;

  p {
    height: 20px;
    line-height: 20px;
    font-size: 20px;
    margin: 7.5px 0;
    padding: 5px;
    background-color: #f5ecd7;
    position: relative;
    color: rgb(96, 109, 121);
    font-family: '楷体';
  }

  .type-writer-cursor {
    width: 0px;
    height: 100%;
    border-left: 2px solid transparent;
    animation: typing 3s steps(16) forwards, cursor 1s infinite;
    -webkit-animation: typing 3s steps(16) forwards, cursor 1s infinite;
  }
}

/* animation */
@keyframes typing {
  from {
    width: 100%;
  }
  to {
    width: 0;
  }
}
@keyframes cursor {
  50% {
    border-color: #5e7ce0;
  }
}

@-webkit-keyframes typing {
  from {
    width: 100%;
  }
  to {
    width: 0;
  }
}
@-webkit-keyframes cursor {
  50% {
    border-color: #5e7ce0;
  }
}
/* / animation */
</style>

2、效果如下

使用Vue实现半自动打字机特效

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/151131.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!