Layout · 18 of 23

Delete Wipe

Collapse animation

HTML · CSS · JavaScript · No dependencies · MIT

HTML

<div class="delete-item" id="deleteItem">
  <span>Swipe to Delete</span>
  <button class="delete-btn">&times;</button>
</div>
<button class="reset-btn" id="resetDelete" style="display:none">Reset</button>

CSS

.delete-item {
  width: 100%;
  max-width: 320px;
  padding: 1rem;
  background: #333;
  color: #f0f0f5;
  border-radius: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
}

.delete-item.deleting {
  animation: deleteSwipe 0.5s forwards;
}

/* Phase 1 (0-50%): slide out to the right and fade.
   Phase 2 (50-100%): collapse the row height so the list closes up. */
@keyframes deleteSwipe {
  0% { transform: translateX(0); opacity: 1; max-height: 60px; padding: 1rem; }
  50% { transform: translateX(100%); opacity: 0; max-height: 60px; padding: 1rem; }
  100% { transform: translateX(100%); opacity: 0; max-height: 0; padding: 0; }
}

.delete-btn {
  background: none;
  border: none;
  color: #aaa;
  font-size: 1.5rem;
  cursor: pointer;
}

.reset-btn {
  margin-top: 1rem;
  background: #333;
  color: #fff;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
}

JavaScript

var deleteBtn = document.querySelector('.delete-btn');
var deleteItem = document.getElementById('deleteItem');
var resetDelete = document.getElementById('resetDelete');
var DURATION = 500; // ms — matches the CSS animation

deleteBtn.addEventListener('click', function () {
  deleteItem.classList.add('deleting');
  // Remove from the flow once the animation finishes
  setTimeout(function () {
    deleteItem.style.display = 'none';
    resetDelete.style.display = 'inline-block';
  }, DURATION);
});

resetDelete.addEventListener('click', function () {
  deleteItem.style.display = 'flex';
  deleteItem.classList.remove('deleting');
  resetDelete.style.display = 'none';
});