Back

Unveiling the Glass Shuffling Dice Button

Marvel at a glassmorphic button that mimics the tactile motion of shuffling dice, each click revealing a new number.

Component Explanation

Creating a Glass Shuffling Dice Button

Have you ever considered bringing the captivating randomness of a dice roll to the digital realm? The Glass Shuffling Dice Button does just that, combining the elegance of glassmorphism with the delightful uncertainty of rolling dice. In this tutorial, we'll guide you step-by-step in creating a button that shuffles dice numbers upon each click, providing visual feedback through a rotating motion.

HTML Setup

To start, our HTML structure is simple yet effective. We use a button element that will trigger the dice shuffle. The button is wrapped in a container to center it visually.

        <div class="dice-container">
          <button onclick="shuffleDice()" class="glass-dice-button" aria-label="Shuffle Dice Button">
            <span class="dice-number" aria-live="polite">1</span>
          </button>
        </div>
      

This structure ensures keyboard accessibility, and the aria-label provides information to screen readers. The aria-live attribute makes sure users are notified when the dice number changes.

JavaScript Functionality

Our JavaScript function shuffleDice is responsible for generating a new number between 1 and 6, then updating the button's content. A temporary CSS class adds the spinning animation.

        function shuffleDice() {
          let button = document.querySelector('.glass-dice-button');
          let diceNumber = document.querySelector('.dice-number');
          let newNumber = Math.ceil(Math.random() * 6);
          diceNumber.textContent = newNumber;
          button.classList.add('shuffle-motion');
          setTimeout(() => button.classList.remove('shuffle-motion'), 300);
        }
      

The function uses Math.random() and Math.ceil() to simulate a dice roll. After updating the number, the button performs a classy 360-degree spin to mimic dice shuffling.

CSS Styling

The visual aspect of our button uses glassmorphism principles. The semi-transparent background and blur effect give the button its unique glass look.

        .glass-dice-button {
          background: rgba(255, 255, 255, 0.2);
          border: 1px solid rgba(255, 255, 255, 0.4);
          box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
          backdrop-filter: blur(10px);
          -webkit-backdrop-filter: blur(10px);
          border-radius: 10px;
          padding: 20px;
          font-size: 24px;
          font-weight: bold;
          color: #fff;
          cursor: pointer;
          transition: transform 0.3s ease-out;
        }
      

These styles not only captivate visually but ensure our button is delightful to interact with. The shuffle-motion class adds dynamic flair whenever the button is pressed.

        .shuffle-motion {
          transform: rotate(360deg);
        }
      

Try combining glassmorphism with interactive animations and see how they elevate standard UI elements into memorable experiences. Enjoy your new, magical Glass Shuffling Dice Button!

Whole code

<div class="dice-container">
  <button onclick="shuffleDice()" class="glass-dice-button" aria-label="Shuffle Dice Button">
    <span class="dice-number" aria-live="polite">1</span>
  </button>
</div>


<script>
function shuffleDice() {
  let button = document.querySelector('.glass-dice-button');
  let diceNumber = document.querySelector('.dice-number');
  let newNumber = Math.ceil(Math.random() * 6);
  diceNumber.textContent = newNumber;
  button.classList.add('shuffle-motion');
  setTimeout(() => button.classList.remove('shuffle-motion'), 300);
}
</script>

<style>
.dice-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.glass-dice-button {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.4);
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 10px;
  padding: 20px;
  font-size: 24px;
  font-weight: bold;
  color: #fff;
  cursor: pointer;
  transition: transform 0.3s ease-out;
}
.shuffle-motion {
  transform: rotate(360deg);
}
.dice-number {
  display: block;
  text-align: center;
}
</style>
      
Thank you for reading this article.

Comments

More buttons

Similar

See also