如何使用HTML、CSS和JavaScript构建一个随机名言生成器?
在本教程中,我们将使用HTML、CSS和JavaScript构建一个项目,该项目将从API (type.fit) 生成随机名言。
步骤
我们将遵循一些基本步骤:
创建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创建外部文件被认为是一种良好的实践。
为了在我们的应用程序中使用字体真棒图标,我们将在head内添加CDN字体真棒链接。
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" 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" 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>因此,我们学习了如何制作名言生成器应用程序,希望对您有所帮助。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP