gpt4 book ai didi

javascript - 如何以更智能的方式使数字增长和减少?

转载 作者:行者123 更新时间:2023-11-30 13:59:28 25 4
gpt4 key购买 nike

<分区>

我用 JavaScript 创建了一个函数,它是屏幕上数字的倒计时,它会增加和减小它的大小,但是,代码太大了,我想知道如何在不使用的情况下以更智能的方式完成它很多行。谢谢

我的代码 can be found here

var mudar5 = document.getElementById("cinco");
var mudar4 = document.getElementById("quatro");
var mudar3 = document.getElementById("tres");
var mudar2 = document.getElementById("dois");
var mudar1 = document.getElementById("um");
var mudarGo = document.getElementById("go");
mudar3.style.fontSize = "20px";

var fonteAtual = 1;
var minSize = 1;
var maxSize = 80;
var intervalTime = 10;
var intervalTimeDecrease = 2;

increaseSize();

//liga o cinco
function increaseSize() {

var interval = setInterval(function() {
fonteAtual++;

mudar3.style.fontSize = fonteAtual + "px";

if (fonteAtual === maxSize) {
clearInterval(interval);
decreaseSize();
}

}, intervalTime);

}

function decreaseSize() {

var interval = setInterval(function() {
fonteAtual--;
mudar3.style.fontSize = fonteAtual + "px";

if (fonteAtual === minSize) {
clearInterval(interval);
liga2();
}

}, intervalTimeDecrease);

}
//liga o quatro
function liga2() {
document.querySelector("#tres").style.display = "none";
document.querySelector("#dois").style.display = "block";
increasetwo();
}

function increasetwo() {

var interval = setInterval(function() {
fonteAtual++;

mudar2.style.fontSize = fonteAtual + "px";

if (fonteAtual === maxSize) {
clearInterval(interval);
decreasetwo();
}

}, intervalTime);

}

function decreasetwo() {

var interval = setInterval(function() {
fonteAtual--;
mudar2.style.fontSize = fonteAtual + "px";

if (fonteAtual === minSize) {
clearInterval(interval);
liga1();
}

}, intervalTimeDecrease);

}


//liga o 3
function liga1() {
document.querySelector("#dois").style.display = "none";
document.querySelector("#um").style.display = "block";
increaseone();
}

function increaseone() {

var interval = setInterval(function() {
fonteAtual++;

mudar1.style.fontSize = fonteAtual + "px";

if (fonteAtual === maxSize) {
clearInterval(interval);
decreaseone();
}

}, intervalTime);
}

function decreaseone() {
var interval = setInterval(function() {
fonteAtual--;
mudar1.style.fontSize = fonteAtual + "px";

if (fonteAtual === minSize) {
clearInterval(interval);
ligaGo();
}

}, intervalTimeDecrease);
}


//Liga o Go
function ligaGo() {
document.querySelector("#um").style.display = "none";
document.querySelector("#go").style.display = "block";
increaseGo();
}

function increaseGo() {

var interval = setInterval(function() {
fonteAtual++;

mudarGo.style.fontSize = fonteAtual + "px";

if (fonteAtual === maxSize) {
clearInterval(interval);
mudarGo.style.fontSize = 80 + "px";
}

}, intervalTime);
}

//liga o Um
function liga1() {
document.querySelector("#dois").style.display = "none";
document.querySelector("#um").style.display = "block";
increaseone();
}

function increaseone() {

var interval = setInterval(function() {
fonteAtual++;

mudar1.style.fontSize = fonteAtual + "px";

if (fonteAtual === maxSize) {
clearInterval(interval);
decreaseone();
}

}, intervalTime);
}

function decreaseone() {
var interval = setInterval(function() {
fonteAtual--;
mudar1.style.fontSize = fonteAtual + "px";

if (fonteAtual === minSize) {
clearInterval(interval);
ligaGo();
}

}, intervalTimeDecrease);
}


//Liga o Go
function ligaGo() {
document.querySelector("#um").style.display = "none";
document.querySelector("#go").style.display = "block";
increaseGo();
}

function increaseGo() {

var interval = setInterval(function() {
fonteAtual++;

mudarGo.style.fontSize = fonteAtual + "px";

if (fonteAtual === maxSize) {
clearInterval(interval);
mudarGo.style.fontSize = 80 + "px";
}

}, intervalTime);
}
#dois,
#um,
#go {
display: none;
font-size: 0px;
}
<body style="text-align: center; margin: 0 auto">
<div id="tres">3</div>
<div id="dois">2</div>
<div id="um">1</div>
<div id="go">GO!</div>
</body>

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