I have this code snippet that toggles text on button click:
<!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>
Currently, there is no smooth transition between the text changes when the button is clicked. I'm unsure whether to use CSS or jQuery for this effect.
I attempted to create a CSS
class named smooth-fade
:
.smooth-fade {
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
I then applied this class to all p
tags but it didn't produce the desired result. What would be the best approach to achieve a smooth transition in this scenario?