Back

Creating a Playful Time Rewind Button with Tactile Motion

Learn to build an interactive button that visually rewinds time, reminiscent of a classic cassette tape rewinder, drawing users into a fun and functional experience.

Component Explanation

Creating a Playful Time Rewind Button with Tactile Motion

In this tutorial, explore how to craft a delightful button reminiscent of a nostalgic cassette tape rewinder. This guide will walk you through HTML, CSS, and JavaScript to produce a tactile, animated button experience.

HTML

The HTML structure defines a container that holds our interactive button. It makes use of semantic tags to wrap the button element. We include an aria-label for accessibility, providing descriptive text for screen readers.

<div class="rewind-container">
  <button onclick="rewindTime()" class="rewind-button" aria-label="Rewind Time Button">
    <span class="button-label">Rewind</span>
  </button>
</div>

JavaScript

The JavaScript function manages the toggle state of the button. The rewindTime function flips the state variable isRewinding, dynamically changing the button's appearance and label.

let isRewinding = false;
function rewindTime() {
  let button = document.querySelector('.rewind-button');
  let label = document.querySelector('.button-label');
  isRewinding = !isRewinding;
  if (isRewinding) {
    label.textContent = "Rewinding...";
    button.classList.add('rewinding');
  } else {
    label.textContent = "Rewind";
    button.classList.remove('rewinding');
  }
}

CSS

The CSS is where the visual magic happens. Styles are crafted for the button to ensure it is eye-catching and interactive, using shadows and smooth transitions. When activated, the .rewinding class applies a moving animation that mimics the playful motion of a rewind action.

.rewind-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.rewind-button {
  border: none;
  padding: 20px 40px;
  border-radius: 50px;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  background-color: #3a3d99;
  cursor: pointer;
  transition: all 0.4s ease;
  box-shadow: inset 0 0 10px rgba(0,0,0,0.2);
}
.rewind-button:focus {
  outline: 2px solid #ffc107;
  outline-offset: 4px;
}
.rewinding {
  animation: rewind-animation 2s infinite ease-in-out;
  background-color: #ff6f61;
}

@keyframes rewind-animation {
  0% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-10px);
  }
  50% {
    transform: translateX(0);
  }
  75% {
    transform: translateX(10px);
  }
  100% {
    transform: translateX(0);
  }
}

This design not only engages users visually but also adheres to best practices for accessibility and modern interaction design by providing motion that enhances UX without compromising functionality.

Whole code

<div class="rewind-container">
  <button onclick="rewindTime()" class="rewind-button" aria-label="Rewind Time Button">
    <span class="button-label">Rewind</span>
  </button>
</div>


<script>
let isRewinding = false;
function rewindTime() {
  let button = document.querySelector('.rewind-button');
  let label = document.querySelector('.button-label');
  isRewinding = !isRewinding;
  if (isRewinding) {
    label.textContent = "Rewinding...";
    button.classList.add('rewinding');
  } else {
    label.textContent = "Rewind";
    button.classList.remove('rewinding');
  }
}
</script>

<style>
.rewind-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.rewind-button {
  border: none;
  padding: 20px 40px;
  border-radius: 50px;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  background-color: #3a3d99;
  cursor: pointer;
  transition: all 0.4s ease;
  box-shadow: inset 0 0 10px rgba(0,0,0,0.2);
}
.rewind-button:focus {
  outline: 2px solid #ffc107;
  outline-offset: 4px;
}
.rewinding {
  animation: rewind-animation 2s infinite ease-in-out;
  background-color: #ff6f61;
}

@keyframes rewind-animation {
  0% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-10px);
  }
  50% {
    transform: translateX(0);
  }
  75% {
    transform: translateX(10px);
  }
  100% {
    transform: translateX(0);
  }
}
</style>
      
Thank you for reading this article.

Comments

No comments yet. Be the first to comment!

More buttons

Similar

See also