gpt4 book ai didi

javascript - 如何动画这个功能?

转载 作者:行者123 更新时间:2023-11-30 16:20:57 25 4
gpt4 key购买 nike

我需要为 函数 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com