在本教程中,我們將使用 HTML、CSS 和 JavaScript 建立一個項目,該項目將從 API (type.fit) 產生隨機引用。
#建立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,以便避免製作額外的文件,但為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" />
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; }
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);
Ramdom quote generator using HTML, CSS and JavaScript <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" /><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>
以上是如何使用 HTML、CSS 和 JavaScript 建立隨機報價產生器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!