溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Button實現(xiàn)點擊波紋效果

發(fā)布時間:2024-08-31 10:47:43 來源:億速云 閱讀:82 作者:小樊 欄目:移動開發(fā)

要在按鈕上實現(xiàn)點擊波紋效果,您可以使用HTML、CSS和JavaScript。這里是一個簡單的示例:

  1. 首先,創(chuàng)建一個HTML文件并添加一個按鈕元素:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Ripple Effect</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
   <button class="ripple-button" onclick="createRipple(event)">點擊我</button>
   <script src="script.js"></script>
</body>
</html>
  1. 接下來,創(chuàng)建一個名為styles.css的CSS文件,并添加以下樣式:
.ripple-button {
  position: relative;
  overflow: hidden;
  background-color: #3f51b5;
  color: white;
  padding: 10px 20px;
  border: none;
  cursor: pointer;
  font-size: 16px;
  border-radius: 4px;
}

.ripple {
  position: absolute;
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  transform: scale(0);
  animation: ripple-animation 1s linear;
  pointer-events: none;
}

@keyframes ripple-animation {
  to {
    transform: scale(4);
    opacity: 0;
  }
}
  1. 最后,創(chuàng)建一個名為script.js的JavaScript文件,并添加以下代碼:
function createRipple(event) {
  const button = event.currentTarget;
  const ripple = document.createElement("span");
  const diameter = Math.max(button.clientWidth, button.clientHeight);
  const radius = diameter / 2;

  ripple.style.width = ripple.style.height = `${diameter}px`;
  ripple.style.left = `${event.clientX - button.getBoundingClientRect().left - radius}px`;
  ripple.style.top = `${event.clientY - button.getBoundingClientRect().top - radius}px`;

  ripple.classList.add("ripple");
  button.appendChild(ripple);

  setTimeout(() => {
    ripple.remove();
  }, 1000);
}

現(xiàn)在,當(dāng)您點擊按鈕時,應(yīng)該會看到一個波紋效果。這個效果是通過在按鈕上創(chuàng)建一個圓形的<span>元素并應(yīng)用CSS動畫來實現(xiàn)的。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI