1
I took this fadein and fadeOut code with pure Javascript on the internet and gave a small adapted, but I have a problem.
I have a login card appearing for the user, in case he click on the "CREATE ACCOUNT" button, the login card disappears and the account creation card appears, this is working, the problem is at the time of returning, in case the user click on the "BACK" buttonthe account creation card disappears and the login card reappears, but if I look at the Firefox "Inspect Element", I see the property display
keeps changing over and over and over and over again display: block
and display: none
, both on the login card and on the account creation card, the idea was that the account creation card would only receive 1 time display: none
(to disappear) and the login card received only 1 time display: block
(to appear again), but is alternating between display: block
and display: none
infinitely fast.
The card keeps blinking. (because it’s changing quite quickly to the property display
).
<!DOCTYPE html>
<html>
<head>
<title>Cinema</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
<link rel="stylesheet" type="text/css" href="css/materialdesignicons.min.css">
<script type="text/javascript">
function fadeOut(id, time) {
fade(id, time, 100, 0, 0);
}
function fadeIn(id, time) {
fade(id, time, 0, 100, 1);
}
function fade(id, time, ini, fin, inout) {
timer = (time * 1000) / 50;
if(inout == 1)
{
setInterval(function(){ document.getElementById(id).style.display = "block"; }, timer);
}
var target = document.getElementById(id);
var alpha = ini;
var inc;
if (fin >= ini) {
inc = 2;
} else {
inc = -2;
}
var i = setInterval(
function() {
if ((inc > 0 && alpha >= fin) || (inc < 0 && alpha <= fin)) {
clearInterval(i);
}
setAlpha(target, alpha);
alpha += inc;
}, timer);
if(inout == 0)
{
setInterval(function(){ document.getElementById(id).style.display = "none"; }, timer);
}
}
function setAlpha(target, alpha) {
target.style.filter = "alpha(opacity="+ alpha +")";
target.style.opacity = alpha/100;
}
</script>
</head>
<body style="background-color: rgb(238, 238, 238);">
<div class="cardLogin" id="cardLogin">
<div class="tituloCardLogin">
<span><i class="mdi mdi-movie mdi-48px"></i></span>
</div>
<div class="inputCardLogin" id="inputCardLogin">
<input type="text" name="username" placeholder="Username">
<br><br><br>
<input type="text" name="password" placeholder="Password">
</div>
<div class="btnCardLogin">
<button type="button">LOGIN</button><br>
<button type="button" onclick="irCriarConta()">CRIAR CONTA</button>
</div>
</div>
<div class="cardCriarConta" id="cardCriarConta">
<div class="tituloCardCriarConta">
<span><i class="mdi mdi-movie mdi-48px"></i></span>
</div>
<div class="inputCardCriarConta" id="inputCriarConta">
<input type="text" name="username" placeholder="Username">
<br><br><br>
<input type="text" name="password" placeholder="Password">
<br><br><br>
<input type="text" name="password2" placeholder="Confirm Password">
</div>
<div class="btnCardCriarConta">
<button type="button" onclick="voltarLogin()">VOLTAR</button><br>
<button type="button">SALVAR</button>
</div>
</div>
</body>
<script type="text/javascript">
function irCriarConta() {
fadeOut("cardLogin", 1.2);
fadeIn("cardCriarConta", 1.2);
}
function voltarLogin() {
fadeIn("cardLogin", 3);
fadeOut("cardCriarConta", 3);
}
</script>
</html>
Card Login:
Card Create Account:
Vc has 3 setIntervals, is killing only 1 (which has the name of
i
), soon the other 2 will be running endlessly, because the setInterval is infinite until you kill it. This is causing serious conflict in the code.– Sam
It’s a little difficult to analyze your code because I can’t see the effect without the CSS files, I was confused by the need to use the
setInterval
, I would trade only propertydisplay
andopacity
(have to put more attributes for different browsers) to give effect, I did a navigation by Tabs simulated this way. Waiting if you can use https://jsfiddle.net/– RpgBoss
I made an improvement in the code right when you voted. I don’t know if you saw it. The code got much better.
– Sam