首页 > 文章列表 > 如何使用 HTML、CSS 和 JavaScript 构建随机报价生成器?

如何使用 HTML、CSS 和 JavaScript 构建随机报价生成器?

152 2023-09-04

在本教程中,我们将使用 HTML、CSS 和 JavaScript 构建一个项目,该项目将从 API (type.fit) 生成随机引用。

步骤

< p>我们将遵循一些基本步骤 -

  • 创建 HTML 元素和模板

  • 使用添加样式CSS

  • JavaScript 逻辑

创建 HTML 元素和模板

第一步是创建HTML 元素和模板。我们首先添加一个将显示项目的框,然后添加一个按钮,当单击该按钮时,它将在框中显示一个新的随机报价,然后使用 span 标签来显示报价符号类型字体很棒的图标。

HTML

<!DOCTYPE html>
<html>
<head>
   <title>Random quote generator using HTML, CSS and JavaScript</title>
</head>
<body>
   <div class="boxSize">
   <h1>
   <i class="fas fa-quote-left"></i>
   <span class="QuoteText" id="QuoteText"></span>
   <i class="fas fa-quote-right"></i>
   </h1>
   <p class="QuoteWR" id="author"></p>
   <hr/>
   <div class="QuoteBtn">
   <button class="GenerateQuote_next" onclick="GenerateQuote()">Next quote</button>
   </div>
   </div>
</body>
</html>

使用 CSS 添加样式

现在我们将向我们编写的 HTML 项目添加样式。我们将向框添加框阴影、填充和边距等属性,对于作者,我们将使用草书字体系列,我们还将向框以及主体添加背景颜色,使其看起来很棒。

我们将致力于内部CSS,以便避免制作额外的文件,但为CSS和JavaScript制作外部文件被认为是一个很好的做法。

我们将在我们的头脑中添加CDN字体很棒的链接为了在我们的应用程序中使用 font Awesome 图标。

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />

CSS

body{
   min-height:100vh;
   transition: 0.5s;
   display: flex;
   background-color: #A4E5E0;
   align-items: center;
   justify-content: center;

}
.boxSize {
   margin: 10px;
   border-radius: 10px;
   width: 800px;
   display: flex;
   flex-direction: column;
   align-items: center;
   padding: 30px;
   box-shadow: 0 4px 10px rgba(0, 0, 0, 0.6);
   background-color: rgba(255, 255, 255, 0.3);
}
.fa-quote-left, .fa-quote-right {
   font-size: 35px;
   color: blue;
}
.QuoteText {
   text-align: center;
   font-size: 40px;
   font-weight: bold;
}
.author {
   margin:10px;
   text-align: right;
   font-size: 25px;
   font-style: italic;
   font-family: cursive;
}
.QuoteBtn {
   width: 100%;
   display: flex;
   margin-top:10px;
}
.GenerateQuote_next {
   font-size:18px;
   border-radius: 5px;
   cursor:pointer;
   padding: 10px;
   margin-top: 5px;
   font-weight: bold;
   color: white;
   background-color: #2C5E1A
}
.GenerateQuote_next:hover{
   background-color: black;
}

JavaScript 逻辑

现在逻辑部分出现在场景中,这部分将是 JavaScript,我们将定义哪些元素将执行哪些工作以及使用 API 来获取和显示数据所以让我们来制作深入了解我们的 JavaScript 函数。

步骤

我们必须按照以下步骤来完成我们的工作 -

  • 从 type.fit API 获取报价数据。

  • 接收到的数据将存储在数组中。

  • 获取名为“randomIdx”的随机索引变量。

  • 然后将“randomIdx”最大大小设置为报价列表长度。

  • 使用生成的随机索引获取引用和作者

  • 现在我们将获得的值分配给项目元素。

JavaScript

var url="https://type.fit/api/quotes";
const response=await fetch(url);
const Quote_list = await response.json();
const randomIdx = Math.floor(Math.random()*Quote_list.length);
const quoteText=Quote_list[randomIdx].text;
const auth=Quote_list[randomIdx].author;
if(!auth) author = "Anonymous";
console.log (quoteText);
console.log (auth);

让我们嵌入 JavaScript 函数代码以使其工作。

示例 - 完整程序

下面是构建随机报价生成器的完整程序。

<!DOCTYPE html>
<html>
<head>
   <title>Ramdom quote generator using HTML, CSS and JavaScript</title>
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
   <style>
      body{
         min-height:100vh;
         transition: 0.5s;
         display: flex;
         background-color: #A4E5E0;
         align-items: center;
         justify-content: center;

      }
      .boxSize {
         margin: 10px;
         border-radius: 10px;
         width: 800px;
         display: flex;
         flex-direction: column;
         align-items: center;
         padding: 30px;
         box-shadow: 0 4px 10px rgba(0, 0, 0, 0.6);
         background-color: rgba(255, 255, 255, 0.3);
      }
      .fa-quote-left, .fa-quote-right {
         font-size: 35px;
         color: blue;
      }
      .QuoteText {
         text-align: center;
         font-size: 40px;
         font-weight: bold;
      }
      .author {
         margin:10px;
         text-align: right;
         font-size: 25px;
         font-style: italic;
         font-family: cursive;
      }
      .QuoteBtn {
         width: 100%;
         display: flex;
         margin-top:10px;
      }
      .GenerateQuote_next {
         font-size:18px;
         border-radius: 5px;
         cursor:pointer;
         padding: 10px;
         margin-top: 5px;
         font-weight: bold;
         color: white;
         background-color: #2C5E1A
      }
      .GenerateQuote_next:hover {
         background-color: black;
      }
   </style>
</head>
<body>
   <div class="boxSize">
   <h1>
      <i class="fas fa-quote-left"></i>
      <span class="QuoteText" id="QuoteText"></span>
      <i class="fas fa-quote-right"></i>
   </h1>
   <p class="QuoteWR" id="author"></p>

   <hr/>
   <div class="QuoteBtn">
   <button class="GenerateQuote_next" onclick="GenerateQuote()">Next quote</button>
   </div>
   </div>
   <script>
      const GenerateQuote = async () =>{
         var url="https://type.fit/api/quotes";

         const response=await fetch(url);
         const Quote_list = await response.json();
         const randomIdx = Math.floor(Math.random()*Quote_list.length);
         const quoteText=Quote_list[randomIdx].text;
         const auth=Quote_list[randomIdx].author;
         
         if(!auth) author = "Anonymous";
         document.getElementById("QuoteText").innerHTML=quoteText;
         document.getElementById("author").innerHTML="~ "+auth;
      }
      GenerateQuote();
   </script>
</body>
</html>

因此,我们已经学习了报价生成器应用程序的制作,希望它有所帮助。