更改 textarea 上的文本时动画停止

Animation stops when changing text on textarea

我有一个文本区域可以将内容复制到另一个 div。 div 虽然有带动画的文字。当我开始在 textarea 上书写并开始复制到 div 时,动画停止。如何为每个字符输入应用动画?动画通过 class 名称应用于 div。 HTML:

var $tlt = $('.tlt').textillate({ 
    in: {
        effect: 'fadeIn',
        shuffle: true
    },
    out: {
        effect: 'fadeOut',
        shuffle: true
    },
    loop: true,
    minDisplayTime: 1000,
});



$('.content:not(.focus)').keyup(function() {
    var value = $(this).val();
    var contentAttr = $(this).attr('name');
    $('.' + contentAttr + '').html(value.replace(/\r?\n/g, '<br/>'));
  
})


$(".manifestoWriter").on('change', '#toChange', function(){
   $tlt.textillate('start');
});
<textarea id="manifestoWriter" class="chatinput form-control content" placeholder="We want courage, audacity and hope without the fascism" rows="11" style="resize: none; background-color:#f7f7f7; border: 0cm; border-radius: 0cm; " maxlength="800" name="mas"></textarea>

<h1 class="tlt mas" id="toChange">
 We want courage, audacity and hope without the fascism
</h1>

JSFIDDLE

我认为您应该更改 textillate.js 代码。首先转到 jquery.textillate.js 中的第 247 行(或者可能是第 249 行)(您必须下载该文件)。你会发现

  if (!data) {
    $this.data('textillate', (data = new Textillate(this, options)));
  } else if (typeof settings == 'string') {
    data[settings].apply(data, [].concat(args));
  } else {
    data.setOptions.call(data, options);
  }

改为

$this.data('textillate', (data = new Textillate(this, options)));

    $(".manifestoWriter").on('change', '#toChange', function(){
      $tlt.textillate('start');
   });

$("#manifestoWriter").on('change', function(){
       $('.tlt').textillate({ 
        in: {
        effect: 'fadeIn',
        shuffle: true
      },
      out: {
        effect: 'fadeOut',
        shuffle: true
      },
      loop: true,
      minDisplayTime: 1000,
    });
});

在这里查看 jsfiddle

PS:这不是最好的做法。如果你使用 "stop" 或将任何字符串传递给 textillate 它将导致 bags

Textillate 并没有真正考虑到这个用例。它旨在帮助动画化句子列表,就像您在 https://textillate.js.org.

上看到的那样

我认为你最好直接使用 animate.css 和一些 javascript 将字符从文本区域复制到另一个容器。

这是一个让您入门的示例:https://jsfiddle.net/jschr/jdqpxyvm/31

const textarea = document.querySelector('textarea')
const container = document.querySelector('.container')

const createSpan = (char) => {
  const span = document.createElement('span')
  span.classList.add('fadeIn')
  span.classList.add('animated')
  span.innerText = char
  return span
}

const updateChars = () => {
  const updatedChars = []
  const currentSpans = Array.from(container.querySelectorAll('span'))
  const chars = textarea.value.split('')
  // Iterate over each character in the textarea and append 
  // new chars to the container.
  chars.forEach((char, index) => {
    const spanAtIndex = currentSpans[index]
    // Add new span if one doesn't exist at current index
    if (!spanAtIndex) {
      container.appendChild(createSpan(char))
    }
    // TODO: Handle deletions
  })
}

textarea.addEventListener('keypress', () => {
  setTimeout(updateChars)
}, false)