Toggle text in JavaScript -
is there easier way toggle 2 or more text visibility? when i've used .toggle()
method, text jumps , down, because toggled @ same time. .fadein()
, .fadeout()
working correctly, code 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>
try approach:
$('h1').hide(); var titles = ['web developer', 'web designer']; var index = -1; var triggertitle = function() { $('h1').fadeout(); settimeout(function() { $('h1').text(titles[index].touppercase()); $('h1').fadein(); }, 1000); index = (index < titles.length - 1) ? index + 1 : 0; console.log(index); }; setinterval(triggertitle, 3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1></h1>
this way store messages single array , can add many titles want. , have single <h1>
tag in html. code becomes more cleaner.
hope helps!
Comments
Post a Comment