BCLC BCLC - 4 years ago 229
Javascript Question

How to create a template for randomising pictures and quotes without correspondence?

I found templates for generating random quotes and templates for for generating random pictures.

I'm trying to create a template generates a random quote and a random picture much like this one but there is no correspondence between the quote and the picture.

How can I adjust the code below to achieve such?




HTML

<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Random Quote Machine</title>

<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<link href='https://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="col-md-4 text-center">
<h1>V for Vendetta</h1>

<div class="quoteBox">
<p class="quote">Behind this mask there is more than just flesh. Beneath this mask there is an idea... and ideas are bulletproof. </p>
<span class="hero">- Alan Moore, V for Vendetta</span>
</div>

<div class="buttons">
<a class="btn btn-danger quoteButton" id="quoteBtn"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></a>

<a class="btn btn-primary tweetButton" id="tweetBtn" href="https://twitter.com/intent/tweet?text=" target="_blank"><i class="fa fa-twitter"></i></a>
</div>
</div>

<div class="col-md-8 text-center">
<img class="img-responsive" src="http://download.1wallpaper.net/20150420/v-for-vendetta-movie-mask-black-background-1920x1200.jpg">
</div>
</div>
</div>


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</body>

</html>


CSS

body {
background-color: black;
color: white;
font-family: Raleway;
}

.container {
margin: 0 50px;
}

h1 {
font-size: 60px;
padding: 150px 0 5px 0;
}

.quoteButton,
.tweetButton {
padding: 5px 25px 5px 25px;
}

#quoteBtn {
margin-right: 5px;
}

.quoteBox {
padding: 25px 0 30px 0;
}

.quote {
font-size: 20px;
}

span {
font-style: italic;
}

img {
padding: 50px 0 0 0;
}


JS

$(document).ready(function() {
$('#quoteBtn').on('click', function() {
var quotes = [{
'author': '- Alan Moore, V for Vendetta',
'quote': "People shouldn't be afraid of their government. Governments should be afraid of their people.",
'img': 'http://images4.alphacoders.com/634/63444.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "Everybody is special. Everybody. Everybody is a hero, a lover, a fool, a villain. Everybody. Everybody has their story to tell.",
'img': 'http://www.wallpaperup.com/uploads/wallpapers/2014/03/24/307595/big_thumb_f38ae29be31e4c280ad5e2e9ed9fc716.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "Remember, remember the fifth of November of gunpowder treason and plot. I know of no reason why the gun powder treason should ever be forgot.",
'img': 'https://i.ytimg.com/vi/1S-J0PHx24g/maxresdefault.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "Love your rage, not your cage.",
'img': 'http://stylishhdwallpapers.com/wp-content/uploads/2015/01/V-For-Vendetta-Desktop-HD-Wallpaper.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "It seems strange that my life should end in such a terrible place, but for three years I had roses, and apologized to no one.",
'img': 'https://antiscribe.files.wordpress.com/2011/11/vs-dominoes-and-his-plot.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "There's no flesh or blood within this cloak to kill. There's only an idea. Ideas are bulletproof.",
'img': 'http://wfiles.brothersoft.com/v/v_for_vendetta_hd_66099-1600x1200.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "Knowledge, like air, is vital to life. Like air, no one should be denied it.",
'img': 'http://img1.goodfon.su/original/1920x1200/9/a2/v-znachit-vendetta-v-for.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "Happiness is the most insidious prison of all.",
'img': 'http://wfiles.brothersoft.com/v/v_for_vendetta_hd_66095-1600x1200.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "I shall die here. Every last inch of me shall perish. Except one. An inch. It's small and it's fragile and it's the only thing in the world worth having. we must never lose it, or sell it, or give it away. We must never let them take it from us.",
'img': 'https://i.ytimg.com/vi/uPuA7BX5L9g/maxresdefault.jpg'
}, {
'author': '- Alan Moore, V for Vendetta',
'quote': "The past can't hurt you anymore, not unless you let it.",
'img': 'https://i.ytimg.com/vi/oIvusE5Vudc/maxresdefault.jpg'
}];

var randomQuote = Math.floor((Math.random() * quotes.length));

$('.quote').html(quotes[randomQuote]['quote']);
$('.hero').html(quotes[randomQuote]['author']);
$('.img-responsive').attr('src', quotes[randomQuote]['img']);

$("#tweetBtn").on('click', function() {
$("#tweetBtn").attr('href', "https://twitter.com/intent/tweet?text=" + quotes[randomQuote]['quote'] + quotes[randomQuote]['author']);
});
})
});

Answer Source

Or: how can I adjust the code below to achieve such?

  $(".img-responsive")
  .attr("src", quotes[Math.floor((Math.random() * quotes.length))]["img"]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download