回忆我的textille动画的问题

时间:2018-04-08 14:28:13

标签: javascript jquery jquery-textillate

我想为我的段落制作动画,点击按钮后会更换内容。因此按钮#start_animation应该启动textillate in动画,而按钮#out_animation应该会导致out动画。然后我点击第三个按钮#replaced_word,它取代了我的段落的内容。

重要提示:我总是点击同一行:

  1. #start_animation

  2. #out_animation

  3. #replaced_word

  4. 所以这个词应该是一个动画(fadeIn),然后是另一个动画(fadeOut),然后被替换,然后我再次点击第一个按钮#start_animation和它一遍又一遍。问题是它只是第一次起作用。我的意思是直到我第二次点击#start_animation。我之前用代码写了这个问题,但是它太长了所以我以片段的形式做了。有什么想法吗?

    $('#start_animation').click(function() {
      $('#sample_text').textillate('in');
      $('#paragraph').textillate('start');
      $('#paragraph').addClass('show');
    });
    
    /*-----------------------------------------------*/
    
    $("#out_animation").click(function() {
      $('#sample_text').textillate('out');
      $('#paragraph').textillate('out');
      setTimeout(function() {
        $('#paragraph').removeClass('show');
      }, 1000);
    });
    
    var words = ["peter", "michael", "john", "derek", "chris"];
    
    function replacing() {
      document.getElementById('paragraph').innerHTML = getRandomItem(words);
    }
    
    var btn2 = document.getElementById("replaced_word");
    
    btn2.addEventListener("click", replacing);
    
    function getRandomItem(array) {
      return array.splice(Math.floor(Math.random() * array.length), 1)[0];
    }
    
    /*-----------------------------------------------*/
    
    $(function() {
      $('#paragraph').textillate({
        loop: false,
        autoStart: false,
        in: {
          effect: 'fadeIn',
          shuffle: false,
          delay: 80
        },
        out: {
          effect: 'fadeOut',
          shuffle: false,
          delay: 40,
          reverse: true
        }
      });
    })
    .show {
      opacity: 1!important;
    }
    
    #paragraph {
      opacity: 0;
    }
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
    <script src="https://cdn.rawgit.com/jschr/textillate/3ca7326c/jquery.textillate.js"></script>
    <script src="https://cdn.rawgit.com/davatron5000/FitText.js/0b4183af/jquery.fittext.js"></script>
    <script src="https://cdn.rawgit.com/davatron5000/Lettering.js/d06bb733/jquery.lettering.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
    
    <button id="start_animation">start animation</button>
    <button id="out_animation">out animation</button>
    <button id="replaced_word">replace word</button>
    <p id="paragraph">TEST</p>

2 个答案:

答案 0 :(得分:1)

问题是,每次更换段落中的文本时,都需要“重置”插件。好吧,你还需要替​​换元素DOM本身,否则插件将无法执行任何操作,因为它已经附加了DOM元素。

$('#start_animation').click(function() {
  $('#sample_text').textillate('in');
  $('#paragraph').textillate('start');
  $('#paragraph').addClass('show');
});

/*-----------------------------------------------*/

$("#out_animation").click(function() {
  $('#sample_text').textillate('out');
  $('#paragraph').textillate('out');
  setTimeout(function() {
    $('#paragraph').removeClass('show');
  }, 1000);
});

var words = ["peter", "michael", "john", "derek", "chris"];

function replacing() {
  $('#paragraph').replaceWith('<p id="paragraph">' + getRandomItem(words) + '</p>');
  init();
}

var btn2 = document.getElementById("replaced_word");

btn2.addEventListener("click", replacing);

function getRandomItem(array) {
  return array.splice(Math.floor(Math.random() * array.length), 1)[0];
}

function init() {
console.log('init');
  $('#paragraph').textillate({
    loop: false,
    autoStart: false,
    in: {
      effect: 'fadeIn',
      shuffle: false,
      delay: 80
    },
    out: {
      effect: 'fadeOut',
      shuffle: false,
      delay: 40,
      reverse: true
    }
  });
}

/*-----------------------------------------------*/

$(function() {
  init();
});
.show {
  opacity: 1!important;
}

#paragraph {
  opacity: 0;
}
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://cdn.rawgit.com/jschr/textillate/3ca7326c/jquery.textillate.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/FitText.js/0b4183af/jquery.fittext.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/Lettering.js/d06bb733/jquery.lettering.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">

<button id="start_animation">start animation</button>
<button id="out_animation">out animation</button>
<button id="replaced_word">replace word</button>
<p id="paragraph">TEST</p>

顺便说一句,为什么你需要手动完成所有这些工作人员?该插件似乎具有手动替换文本转换的功能。

答案 1 :(得分:1)

当您操作DOM以更改段落的文本时,textillate事件不再绑定到它。

一个简单的解决方案是使用<p id="paragraph"><span id="paragraph_text">TEST</span></p>并仅在#paragraph_text

上执行DOM操作

此外,我注意到$('#sample_text')未被使用,可以删除。

$('#start_animation').click(function() {
  $('#paragraph').textillate('start');
  $('#paragraph').addClass('show');
});

/*-----------------------------------------------*/

$("#out_animation").click(function() {
  $('#paragraph').textillate('out');
  setTimeout(function() {
    $('#paragraph').removeClass('show');
  }, 1000);
});

var words = ["peter", "michael", "john", "derek", "chris"];

function replacing() {
  document.getElementById('paragraph_text').innerHTML = getRandomItem(words);
}

var btn2 = document.getElementById("replaced_word");

btn2.addEventListener("click", replacing);

function getRandomItem(array) {
  return array.splice(Math.floor(Math.random() * array.length), 1)[0];
}

/*-----------------------------------------------*/

$(function() {
  $('#paragraph').textillate({
    loop: false,
    autoStart: false,
    in: {
      effect: 'fadeIn',
      shuffle: false,
      delay: 80
    },
    out: {
      effect: 'fadeOut',
      shuffle: false,
      delay: 40,
      reverse: true
    }
  });
})
.show {
  opacity: 1!important;
}

#paragraph {
  opacity: 0;
}
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://cdn.rawgit.com/jschr/textillate/3ca7326c/jquery.textillate.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/FitText.js/0b4183af/jquery.fittext.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/Lettering.js/d06bb733/jquery.lettering.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">

<button id="start_animation">start animation</button>
<button id="out_animation">out animation</button>
<button id="replaced_word">replace word</button>
<p id="paragraph"><span id="paragraph_text">TEST</span></p>