javascript - 如何动画这个功能?

标签 javascript jquery css

我需要为 函数 descramble() 设置动画。因此,它会更改第一个字符,然后移动到下一个字符,依此类推。我试过使用 setTimeout() 但这只会延迟函数的启动。我只需要逐个字母地为 ROT13 转换字母制作动画。

我正在使用 slice() 删除字符串的第一个元素,用 ROT13 替换它,然后用新字母加上整个字符串替换字符串。我无法找到一种方法来只删除该段的第一个字母。你可以点击jsfiddle中的第三段来查看转换。

$("#last-second-inside p:nth-child(3)").one('click', function() {
  $(this).css('cursor', 'default');
  var str = "Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.";
  var strArr = str.split('');
  var decodedArr = [];
  var increaseNum = -1;
  descramble();

  function descramble() {
    strArr.map(function(num) {
      increaseNum++;
      var currentLetter = num.charCodeAt();
      if (currentLetter >= 65 && currentLetter <= 90 || currentLetter >= 97 && currentLetter <= 122) {
        if (currentLetter >= 78 && currentLetter <= 90 || currentLetter >= 110 && currentLetter >= 97) {
          decodedArr.push(String.fromCharCode(currentLetter - 13));
        } else {
          decodedArr.push(String.fromCharCode(currentLetter + 13));
        }
      } else {
        decodedArr.push(num);
      }
      var sliced = str.slice(increaseNum + 1, str.length - 1);
      $("#last-second-inside p:nth-child(3)").text(decodedArr.join('') + sliced);
    })
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="last-second-inside">
  <p>You probably now understand that how binary works and how simple it is.</p>
  <p>Now, I need you to think about that how you might display 8 in binary. After giving it a shot, you can click on the next paragraph to descramble it.</p>
  <p>Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.</p>
</div>

最佳答案

不使用 map(),让 descramble() 根据 increaseNum 的值一次只处理一个字母.

descramble() 运行后,使用 setTimeout() 在适当的时间间隔后再次调用它:

$("#last-second-inside p:nth-child(3)").one('click', function() {
  $(this).css('cursor', 'default');
  var str = "Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.";
  var strArr = str.split('');
  var decodedArr = [];
  var increaseNum = 0;  // changed from -1
  descramble();

  function descramble() {
    var num = str[increaseNum++],
        currentLetter = num.charCodeAt();

    if (currentLetter >= 65 && currentLetter <= 90 || currentLetter >= 97 && currentLetter <= 122) {
      if (currentLetter >= 78 && currentLetter <= 90 || currentLetter >= 110 && currentLetter >= 97) {
        decodedArr.push(String.fromCharCode(currentLetter - 13));
      } else {
        decodedArr.push(String.fromCharCode(currentLetter + 13));
      }
    } else {
      decodedArr.push(num);
    }
    var sliced = str.slice(increaseNum + 1, str.length - 1);
    $("#last-second-inside p:nth-child(3)").text(decodedArr.join('') + sliced);
    if(increaseNum < str.length) {
      setTimeout(descramble, 10);
    }
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="last-second-inside">
  <p>You probably now understand that how binary works and how simple it is.</p>
  <p>Now, I need you to think about that how you might display 8 in binary. After giving it a shot, you can click on the next paragraph to descramble it.</p>
  <p>Vg'f fvzcyr lbh jvyy whfg nqq nabgure pbyhza nsgre sbhe'f cynpr naq gung pbyhza jvyy unir gur cynpr inyhr bs rvtug'f.</p>
</div>

关于javascript - 如何动画这个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34755274/

相关文章:

javascript - 如何在这个函数调用中使 "modal conformation"功能起作用?

javascript - React-JavaScript 代码中一行的用途

javascript - 第一次和第二次点击之间的操作不同?

javascript - 使用node.js异步系列插入错误

javascript - $.getJSON 从 angular.element(document).ready 或 initController 调用

javascript - 在单个函数中使用 JavaScript 和 jQuery (Nodes & Stuff)

javascript - jQuery 对话框上的 onclick 事件不适用于 IOS

jquery - 只需使用 CSS 或 jQuery 设置宽度

javascript - 由 jQuery 设置的内联 CSS 'resize event' 在窗口调整大小后重置为默认值

css - 垂直定位表格单元格元素在 Chrome 中有效,但在 FF 和 IE 中无效,反之亦然。垂直对齐换行文本的替代方法?