Как сделать круговой эффект при клике на кнопку на CSS?

Как делать эффект круга при клике на кнопку? Возможно ли это сделать на чистом CSS?

введите сюда описание изображения


Ответы (1 шт):

Автор решения: Ein

Обычно для таких вещей подключается что-то вроде MUI, Vuetifyjs или что-то подходяще в конкретном проекте.

document.querySelectorAll('.button').forEach(button => {
  let container = document.createElement('div');
  container.className = "button-bubble-container";
  button.append(container);
  
  button.addEventListener('mousedown', (e) => {
    let size = Math.max(e.target.clientWidth, e.target.clientHeight) * 2;
    
    let bubble = document.createElement('div');
    bubble.className="button-bubble";
    
    bubble.style.height = size + "px";
    bubble.style.width = size + "px";
    bubble.style.top = (e.offsetY - (size / 2)) + "px";
    bubble.style.left = (e.offsetX - (size / 2)) + "px";
  
    container.append(bubble);
    
    const removeBubble = () => {
      document.removeEventListener("mouseup", removeBubble);
      
      bubble.style.opacity=0;
      setTimeout(() => bubble.remove(), 300);
    };
    
    document.addEventListener("mouseup", removeBubble);
  });
});
.button-bubble-container {
  position: absolute;
  z-index: 0;

  pointer-events: none;
  overflow: hidden;
  inset: 0px;
  border-radius: inherit;
}

.button-bubble {
  position: absolute;
  width: 200px;
  height: 200px;
  background-color: black;
  opacity: 0.3;
  border-radius: 50%;
  transition-duration: 0.3s;

  animation-name: button-bubble;
  animation-duration: 550ms;
  animation-timing-function: cubic-bezier(0.29, 0.17, 0, 1);
}

@keyframes button-bubble {
  from {
    transform: scale(0);
  }

  to {
    transform: scale(1);
  }
}



.button {
  display: inline-flex;
  border: 1px solid gray;
  padding: 0.5em 1em;
  color: #fff;
  background-color: #bf0702;
  border-radius: 5px;
  position: relative;
  cursor: pointer;

  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


.button.big {
  font-size: 2em;
}
<div class="button">
  button
</div>

<div class="button big">
  bigger button
</div>

→ Ссылка