Fade in and out using display property
Answers
Answers
function myFunction() {
document.getElementById("myDIV").style.display = "block";
document.getElementById("second").style.display = "none";
}
function myFunction2() {
document.getElementById("second").style.display ="block";
document.getElementById("myDIV").style.display = "none";
}
#myDIV {
width: 500px;
height: 500px;
background-color: lightblue;
display: none;
}
#second {
width: 500px;
height: 500px;
background-color: lightblue;
display: none;
}
<button onclick="myFunction()">Try it</button>
<button onclick="myFunction2()">Try this now</button>
<div id="myDIV">
This is my DIV element.
</div>
<div id="second">
This is my DIV2 element.
</div>
Answers
Use jQuery
for event handling and fade
animations:
HTML:
<button id="button1">Try it</button>
<button id="button2">Try this now</button>
Javascript:
$('#button1').on('click', function () {
$('#second').fadeOut(2000, function () {
$('#myDIV').fadeIn(2000);
});
});
$('#button2').on('click', function () {
$('#myDIV').fadeOut(2000, function () {
$('#second').fadeIn(2000);
});
});
Demo: http://jsfiddle.net/tusharj/rLvpqcLb/
Answers
You can do it with css3 no jquery required, instead of display none use opacity 0
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
width: 500px;
height: 500px;
background-color: lightblue;
opacity: 0;
transition: all 2s linear;
}
#second {
width: 500px;
height: 500px;
background-color: lightblue;
display: none;
}
</style>
</head>
<body>
<button onclick="myFunction()">Try it</button>
<button onclick="myFunction2()">Try this now</button>
<div id="myDIV">
This is my DIV element.
</div>
<div id="second">
This is my DIV2 element.
</div>
<script>
function myFunction() {
document.getElementById("myDIV").style.opacity = "1";
document.getElementById("second").style.display = "none";
}
</script>
コメント