I have the following code -
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
function toggleText(){
if ($("#txt-1").css("display") != "none") {
$("#txt-1").css("display", "none");
$("#txt-2").css("display", "block");
$("#txt-3").css("display", "none");
} else if ($("#txt-2").css("display") != "none") {
$("#txt-1").css("display", "none");
$("#txt-2").css("display", "none");
$("#txt-3").css("display", "block");
} else {
$("#txt-1").css("display", "block");
$("#txt-2").css("display", "none");
$("#txt-3").css("display", "none");
}
};
</script>
</head>
<body>
<button onclick="toggleText()">Toggle</button>
<p id="txt-1">Hello</p>
<p id="txt-2" style="display: none;">How are you?</p>
<p id="txt-3" style="display: none;">See you soon!</p>
</body>
</html>
It toggles the text on button click
The thing that I am looking for is, to make a smooth transition between the texts (maybe fade in-out). I am not sure whether to write a CSS
or jQuery
.
What I tried is to write a CSS
class -
.smooth-fade {
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
and provide this class name smooth-fade
to all the h1
tags. It does not work, what is the best workaround to do this task ?