Is there a easier way to toggle 2 or more text visibility? When I've used a .toggle() method, the text jumps up and down, because they are toggled at the same time. With .fadeIn() and .fadeOut() it's working correctly, but the code is messy.
var triggerTitle = function() {
var hasClassHide = $(".hero-title.1").hasClass("hide");
if (hasClassHide) {
$(".hero-title.1").removeClass("hide");
$(".hero-title.1").fadeIn(1000);
$(".hero-title.2").addClass("hide");
$(".hero-title.2").fadeOut(1000);
$(".hero-title.2").css("display", "none");
} else {
$(".hero-title.2").removeClass("hide");
$(".hero-title.2").fadeIn(1000);
$(".hero-title.1").addClass("hide");
$(".hero-title.1").fadeOut(1000);
$(".hero-title.1").css("display", "none");
}
};
setInterval(triggerTitle, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 class="hero-title 1">WEB DEVELOPER</h1>
<h1 class="hero-title 2 hide" style="display: none;">WEB DESIGNER</h1>
fadeInandfadeoutyou don't need set alsocss("display", "none").if (hasClassHide) { $(".hero-title.1").removeClass("hide"); $(".hero-title.2").fadeOut(1000); $(".hero-title.1").fadeIn(1000); $(".hero-title.2").addClass("hide"); } else { $(".hero-title.2").removeClass("hide"); $(".hero-title.1").fadeOut(1000); $(".hero-title.2").fadeIn(1000); $(".hero-title.1").addClass("hide"); }