Home >Web Front-end >CSS Tutorial >Build a Dice Roll Simulator Website
Hello, fellow developers! Today, I'm excited to share a project I recently completed: a Dice Roll Simulator. This project is a fun and interactive way to simulate the rolling of a dice, making it a great way to practice JavaScript, especially in the areas of DOM manipulation and event handling. Whether you're looking to build something playful or need a simple dice roll feature for a game, this project is a perfect start.
The Dice Roll Simulator allows users to simulate the roll of a six-sided dice with a simple click of a button. The result is displayed in a visually appealing way, mimicking the appearance of a real dice. This project is perfect for developers who want to enhance their skills in building interactive web applications while working with animations and event listeners.
Here's a quick look at the project structure:
Dice-Roll-Simulator/ ├── index.html ├── style.css └── script.js
To get started with the project, follow these steps:
Clone the repository:
git clone https://github.com/abhishekgurjar-in/Dice-Roll-Simulator.git
Open the project directory:
cd Dice-Roll-Simulator
Run the project:
The index.html file contains the structure of the webpage, including the dice display, the roll button, and the roll history list. Below is a snippet of the HTML code:
8b05045a5be5764f313ed5b9168a17e6 49099650ebdc5f3125501fa170048923 93f0f5c25f18dab9d176bd4f6de5d30e 7c8d9f814bcad6a1d7abe4eda5f773e5 acd8feeb3a0ea7477b979779de32785a 26faf3d1af674280d03ba217d87e9421 b2386ffb911b14667cb8f0f91ea547a7Dice Roll Simulator6e916e0f7d1e588d4f442bf645aedb2f 90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0 af75c476cdb7e6c074ca6da9b40841de 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 4a249f0d628e2318394fd9b75b4636b1Dice Roll Simulator473f0a7621bec819994bb5020d29372a 0f518d525528904adc812d7ec5557770⚄16b28748ea4df4d9c2150843fecfba68 9766fbc41ad5a9efefddfe0fa8af0f49Roll Dice65281c5ac262bf6d81768915a4a77ac0 46e6f1c63052ca773d4c27bdcfd98f44 0d9fbdb23b176f720131df826f3e11af929d1f5ca49e04fdcb27f9465b944689 16b28748ea4df4d9c2150843fecfba68 ffd6ba4147bda351239915f463e46e38 e388a4556c0f65e1904146cc1a846beeMade With ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
The style.css file contains styles that ensure the webpage is visually appealing and includes animations for the dice roll. Here are some key styles:
body { font-family: "Open Sans", sans-serif; text-align: center; margin: 0; } h1 { font-size: 3rem; margin-top: 2rem; } .dice { font-size: 7rem; margin: 5px; animation-duration: 1s; animation-fill-mode: forwards; } .roll-animation { animation-name: roll; } @keyframes roll { 0% { transform: rotateY(0deg) rotateX(0deg); } 100% { transform: rotateY(720deg) rotateX(720deg); } } button { background-color: #47a5c4; color: white; font-size: 1.5rem; padding: 1rem 2rem; border: none; border-radius: 1rem; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #2e8baf; } .roll-list { min-height: 270px; } ul { list-style: none; padding: 0; max-width: 600px; margin: 2rem auto; } li { font-size: 1.5rem; padding: 0.5rem; margin: 0.5rem; background-color: #f2f2f2; border-radius: 0.5rem; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); display: flex; justify-content: space-between; align-items: center; } li span { font-size: 3rem; margin-right: 1rem; } .footer { margin: 50px; }
The script.js file manages the dice roll logic, updates the roll history, and handles the dice roll animation. Below is a snippet of the JavaScript code:
const buttonEl = document.getElementById("roll-button"); const diceEl = document.getElementById("dice"); const rollHistoryEl = document.getElementById("roll-history"); let historyList = []; function rollDice() { const rollResult = Math.floor(Math.random() * 6) + 1; const diceFace = getDiceFace(rollResult); diceEl.innerHTML = diceFace; historyList.push(rollResult); updateRollHistory(); } function updateRollHistory() { rollHistoryEl.innerHTML = ""; for (let i = 0; i b0ea8d5d87b846bf4a6bd4575a8eff42${getDiceFace(historyList[i])}54bdf357c58b8a65c66d7c19c8e4d114`; rollHistoryEl.appendChild(listItem); } } function getDiceFace(rollResult) { switch (rollResult) { case 1: return "⚀"; case 2: return "⚁"; case 3: return "⚂"; case 4: return "⚃"; case 5: return "⚄"; case 6: return "⚅"; default: return ""; } } buttonEl.addEventListener("click", () => { diceEl.classList.add("roll-animation"); setTimeout(() => { diceEl.classList.remove("roll-animation"); rollDice(); }, 1000); });
You can check out the live demo of the Dice Roll Simulator here.
Building this Dice Roll Simulator was a fun and rewarding experience that allowed me to experiment with JavaScript animations and DOM manipulation. I hope this project inspires you to create your own interactive applications. Feel free to explore the code, customize it, and use it in your own projects. Happy coding!
This project was inspired by the need for a simple and interactive dice roll tool.
The above is the detailed content of Build a Dice Roll Simulator Website. For more information, please follow other related articles on the PHP Chinese website!