I am using another Stackoverflow post's JavaScript example to rotate the text of a span
that sits inside of an h3
. I don't understand why the text that is significantly longer, but is a single word, stays on one line, but the text that is much shorter but has two words goes to two lines?
var arr = ['signficantly_longtext_than_others', 'Data Science', 'Front-End Development', 'Programming', 'Product Management', 'Digital Marketing', 'Mobile Development', 'Web Design'],
i = 0, // Start Index
len = arr.length,
$el = $('.rotate span'),
$temp = $('<span />'); // Helper - Will measure Text width
console.log($temp)
$temp.hide().appendTo( $el.parent() ); // Setup Helper
(function loop() {
console.log(arr[i%=len])
var w = $temp.text( arr[i%=len] ).width(); // set text + get width
console.log(w);
$el.fadeTo(600,0).animate({width: w}, 600, function(){
$(this).text( arr[i++] ).fadeTo(600, 1);
});
setTimeout(loop, 3000);
}());
View it live: http://codepen.io/maudulus/pen/RWRMwj