【问题标题】:Vue.js: Outputting contents of an array one element at a time with delayVue.js:一次输出一个元素的数组内容,延迟
【发布时间】:2017-11-03 01:06:33
【问题描述】:

在我的 Vue 实例的数据属性中,我有一个字符串数组。

data() {
  return { myArray: [] }
}

这个数组填充了一个句子的单词。例如:

['Hi', 'my', 'name', 'is', 'foo']

我想一次一个单词地将句子输出到模板中的段落标签,中间有 1 秒的延迟,类似于提词器的工作方式:

[0 秒] - 嗨

[1 秒] - 嗨,我的

[2 秒] - 你好,我的名字

[3 秒] - 你好,我的名字是

[4 秒] - 你好,我叫 foo

感谢任何帮助。

【问题讨论】:

  • 您应该先尝试自己编写一些代码,然后在出现问题时寻求帮助 - 请使用tour 并阅读How to Ask
  • @picard 是的,但有什么好玩的:) vue.js 标签的日子过得很慢。 (JK :)
  • @Picard 哈哈,我的尝试涉及创建一个新数组并将其字符串插值到模板,同时将第一个数组的内容延迟移动到新数组。觉得不值得发帖。谢谢你的链接,我会看看。

标签: javascript html arrays vue.js


【解决方案1】:

您可以使用setInterval 并在每次迭代时添加一个单词:

new Vue({
  el: '#app',
  data: {
    array: ['Hi', 'my', 'name', 'is', 'foo'],
    string: '' // here your string will be stored
  },
  methods: {
    appendWords: function() {
      const it = this.array[Symbol.iterator](); // convenient for yeilding values
      const int = setInterval(() => { // time interval
        const next = it.next(); // next value 
        if (!next.done) { // done = true when the end of array reached
          this.string += ' ' + next.value; // concatenate word to the string
        } else {
          clearInterval(int); // when done - clear interval
        }
      }, 1000) // interval duration, 1s
    }
  },
  mounted() {
    this.appendWords(); // invoke function when ready
  }
});
<script src="https://unpkg.com/vue/dist/vue.js"></script>

<div id="app">
  <p>{{ string }}</p>
</div>

【讨论】:

  • 正是我想要的。感谢您教我 javascript 中的迭代器!
  • 我喜欢这不是他所要求的,但接受了哈哈 :) 该死的用户!还有文字程序员(我)。
  • @BertEvans 我刚刚看到你的回答!我的不好,我的描述不清楚。我想要提词器的感觉,这就是这个解决方案所提供的。
  • @BertEvans 他要求“一次一个单词地将句子输出到我的模板中的段落标签”,这很清楚。我猜你只是被这个“时间线”输出误导了。
  • @KevinShi 伙计们,我在取笑自己 :) 我非常从字面上解释了问题示例输出。
【解决方案2】:

代码

new Vue({
  el:"#app",
  data:{
    base: ['Hi', 'my', 'name', 'is', 'foo'],
    timed: []
  },
  mounted(){
    this.base.forEach((item, i) => {
      setTimeout(() => {
        this.timed.push(`[${i} sec] - ${this.base.slice(0, i + 1).join(" ")}`)
      }, i * 1000)
    })
  }
})

模板

<div id="app">
  <p v-for="time in timed">{{time}}</p>
</div>

Example.

编辑

所以,为了好玩,我考虑了如何使用 javascript 进行提词器模拟。这很幼稚,但在我投入的时间内有效。

new Vue({
  el:"#app",
  data:{
    lines: [
      'Hi, my name is Foo. I am a literal', 
      'programmer. This is a teleprompter',
      'simulation. These lines should all',
      'be about the same length. There are ',
      'different strategies for making that',
      'happen.'
    ],
    show:[]
  },
  mounted(){
    let currentIndex = 0
    
    this.$el.addEventListener('animationend', () => {
      this.show.push(this.lines[++currentIndex])
    })
    this.show.push(this.lines[0])
  }
})
@keyframes revealText {
  0% {
    width:0%;

   }
  100% {
    width:100%;

  }
}

#app{
  width: 500px; 
  margin: auto
}

h1 {
  white-space:nowrap;
  overflow: hidden;
  animation: revealText 5s;
}
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>

<div id="app">
  <h1 v-for="line in show">{{line}}</h1>
</div>

【讨论】:

  • @kevinshi 我很好奇并玩弄了这个。如果你在乎的话,看看:)
  • 这看起来不错。
  • @wostex 谢谢! CSS 真的不是我的强项。我希望有一种体面的方法可以使用过渡组或其他东西来做到这一点。
猜你喜欢
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 2012-08-01
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 2019-06-25
相关资源
最近更新 更多