如何使用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>
因此,我們學習瞭如何製作名言生成器應用程式,希望對您有所幫助。
廣告