You're display the text via jQuery's .text()
. Nothing you put in the string will cause a line break in the output, since newlines are just whitespace within HTML elements -- they're not going to be interpreted specially.
If you were to use .html()
instead, and include a <br />
tag in the midst of your string, things would go better:
var textarray = [
"\"Message\"<br /> Name",
];
// later ...
$(this).html(textarray[rannum]).fadeIn('fast');
Fixed example:
$(window).load(function() {
$(window).resize(function() {
var windowHeight = $(window).height();
var containerHeight = $(".container").height();
$(".container").css("top", (windowHeight / 2 - containerHeight * 0.7) + "px");
});
var textarray = [
"\"Example\" <br> Name"
];
var firstTime = true;
function RndText() {
var rannum = Math.floor(Math.random() * textarray.length);
if (firstTime) {
$('#random_text').fadeIn('fast', function() {
$(this).html(textarray[rannum]).fadeOut('fast');
});
firstTime = false;
}
$('#random_text').fadeOut('fast', function() {
$(this).html(textarray[rannum]).fadeIn('fast');
});
var windowHeight = $(window).height();
var containerHeight = $(".container").height();
$(".container").css("top", (windowHeight / 2 - containerHeight * 0.7) + "px");
}
$(function() {
// Call the random function when the DOM is ready:
RndText();
});
var inter = setInterval(function() {
RndText();
}, 3000);
});
body {
-webkit-animation: pulse 200s infinite;
animation: pulse 15s infinite;
}
@-webkit-keyframes pulse {
0% {
background: #FBFFF7
}
3% {
background: #FBFFF7
}
30% {
background: #FBFFF7
}
60% {
background: #FBFFF7
}
90% {
background: #FBFFF7
}
100% {
background: #FBFFF7
}
}
@keyframes pulse {
0% {
background: #FBFFF7
}
3% {
background: #FBFFF7
}
30% {
background: #FBFFF7
}
60% {
background: #FBFFF7
}
90% {
background: #FBFFF7
}
100% {
background: #FBFFF7
}
}
.container {
position: relative;
vertical-align: middle;
margin: auto;
}
#random_text {
font-size: 3vw;
text-align: center;
vertical-align: middle;
text-align: -moz-center;
text-align: -webkit-center;
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div id="random_text"></div>
</div>