使用CSS新增按鈕點選按下效果


為了允許使用者與網站或應用程式互動,按鈕是網頁設計中必不可少的元件。透過使用CSS為按鈕新增視覺上美觀且創新的點選效果,可以極大地改善使用者體驗並使互動更有趣。

可以使用CSS的`active`偽類在按鈕點選時建立按下效果。此樣式透過定義按鈕被點選時的外觀來提供視覺反饋。在瞭解更多資訊之前,讓我們快速瞭解一下HTML中的按鈕。

HTML按鈕

在HTML中,可點選的按鈕由<button>標籤定義。我們可以在<button>標籤內使用文字和影像。不同瀏覽器的預設<button>型別不同,用於提交內容。我們可以使用CSS工具來設定按鈕的樣式。

語法

以下是HTML按鈕的語法

<button>....</button>

示例

讓我們來看下面的例子,我們將建立一個按鈕並應用CSS。

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         background-color: #34495E;
         text-align: center;
      }
      button {
         margin: 150px;
         position: relative;
         overflow: hidden;
         padding: 10px 40px;
         font-size: 30px;
         font-family: verdana;
         background-color: transparent;
         border: none;
         color: #DE3163;
         transition: 0.3s;

         &::before {
            content: '';
            height: 3px;
            width: 0;
            left: 0;
            bottom: 0;
            position: absolute;
            background: #E8DAEF;
            transition: 0.4s;
         }

         &:hover {
            &::before {
               width: 100%;
            }
         }
      }
      span.y {
         background-color: #D5F5E3;
         border-radius: 100%;
         position: absolute;
         transform: scale(0);
         animation: y .4s linear forwards;
      }
      @keyframes y {
         to {
            transform: scale(1);
            opacity: 0;
         }
      }
   </style>
</head>
<body>
   <div>
      <button class="tp">CLICK ME</button>
   </div>
   <script>
      var button = document.querySelectorAll('.tp');
      Array.prototype.forEach.call(button, function(a) {
         a.addEventListener('click', ripple)
      });

      function ripple(event) {
         var x = document.createElement('span');
         x.classList.add('y');
         var max = Math.max(this.offsetWidth, this.offsetHeight);
         x.style.width = x.style.height = max * 2 + 'px';
         var rect = this.getBoundingClientRect();
         x.style.left = event.clientX - rect.left - max + 'px';
         x.style.top = event.clientY - rect.top - max + 'px';
         this.appendChild(x);
      }
   </script>
</body>
</html>

執行以上程式碼後,它將在網頁上生成一個包含點選按鈕的輸出。當用戶嘗試點選按鈕時,它會顯示由CSS製作的效果。

示例

再來看另一個例子,我們將滑鼠懸停在按鈕上時新增星號。

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         background-color: #D5F5E3;
         text-align: center;
      }
      .tp {
         border-radius: 10px;
         background-color: #D2B4DE;
         border: none;
         color: #DE3163;
         font-size: 30px;
         font-family: verdana;
         padding: 10px;
         width: 150px;
         cursor: pointer;
         margin: 50px;
      }
      .tp span {
         position: relative;
         transition: 0.3s;
      }
      .tp span:after {
         content: '\2042';
         position: absolute;
         opacity: 0;
         top: -3px;
         right: -25px;
         transition: 0.5s;
      }
      .tp:hover span {
         padding-right: 30px;
      }
      .tp:hover span:after {
         opacity: 2;
         right: 1px;
      }
   </style>
</head>
<body>
   <button class="tp">
      <span>CLICK</span>
   </button>
</body>
</html>

執行以上程式碼後,輸出視窗將彈出,在網頁上顯示應用了CSS的按鈕。當用戶嘗試將滑鼠懸停在星號上時。

示例

在下面的例子中,我們將使用CSS建立一個簡單的按鈕按下效果。

<!DOCTYPE html>
<html>
<head>
   <style>
      .button {
         background-color: orange;
         color: white;
         border: none;
         border-radius: 7px;
         box-shadow: 0 5px #999;
         display: inline-block;
         padding: 10px;
         font-size: 16px;
         cursor: pointer;
         text-align: center;
         outline: none;
      }
      .button:hover {
         background-color: blue;
      }
      .button:active {
         background-color: orange;
         box-shadow: 0 5px gray;
         transform: translateY(2px);
      }
   </style>
</head>
<body>
   <button class="button">Result</button>
</body>
</html>

執行以上程式碼後,它將在網頁上生成一個包含應用了CSS的按下效果的輸出。

更新於:2024年1月8日

2K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告