javascript - 如何创建一个模板,用于在没有对应关系的情况下随机化图片和报价?

标签 javascript android css templates random

我找到了生成 random quotes 的模板和用于生成随机图片的模板。

我正在尝试创建一个模板来生成随机报价和随机图片,很像 this one但报价和图片之间没有对应关系。

如何调整下面的代码来实现这一点?


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']);
      });
    })
  });

最佳答案

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

  $(".img-responsive")
  .attr("src", quotes[Math.floor((Math.random() * quotes.length))]["img"]);

关于javascript - 如何创建一个模板,用于在没有对应关系的情况下随机化图片和报价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34579967/

相关文章:

javascript - 递归计算出现次数

Google Play 商店中的 Android 版本 : Where is the Opt-in URL?

Android 使用其 URL 检查文件是否存在于远程服务器中

android - 单击通知后启动器 Activity 将打开(应用程序已关闭)

jquery - 将图像添加到固定大小的容器而不更改尺寸

html - 移动设备上的引导列向上向下堆叠

javascript - 使用 jQuery 替换背景图像不起作用

javascript - 从 .val() JQuery 获取字符

javascript - jquery 显示从 php 返回的多个 json 对象

javascript - 按钮点击文本插入