如何用 CSS 建立淡入淡出按鈕?


要在網頁上建立淡入淡出按鈕,請使用不透明度屬性。要透過懸停滑鼠游標啟用此功能,請使用 :hover 選擇器。在懸停時淡入時,如果實際按鈕的不透明度設定為小於 1,則在懸停時將不透明度設定為 1。對於懸停時淡出,效果相反。

懸停時淡出

將不透明度設定為小於 1 以實現淡出概念。要在懸停時實現此功能,請使用 :hover 選擇器 −

button:hover {
   opacity: 0.5;
}

示例

以下是在懸停時淡出的 CSS 淡入淡出按鈕的程式碼 −

<!DOCTYPE html>
<html>
<head>
   <style>
      button {
         background-color: rgb(255, 175, 222);
         border: none;
         color: black;
         padding: 25px;
         text-align: center;
         font-size: 20px;
         margin: 4px 2px;
         transition: 0.3s;
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
         font-weight: bolder;
         opacity: 1;
      }
      button:hover {
         opacity: 0.5;
      }
   </style>
</head>
<body>
   <h1>Fading Button Example</h1>
   <button>Hover Here</button>
   <p>Hover on the above button to see background fade on button</p>
</body>
</html>

懸停時淡入

將不透明度設定為 1 以實現淡入概念。要在懸停時實現此功能,請使用 :hover 選擇器 −

button:hover {
   opacity: 1;
}

示例

以下是在懸停時淡入的 CSS 淡入淡出按鈕的程式碼 −

<!DOCTYPE html>
<html>
<head>
   <style>
      button {
         background-color: rgb(255, 175, 222);
         border: none;
         color: black;
         padding: 25px;
         text-align: center;
         font-size: 20px;
         margin: 4px 2px;
         transition: 0.3s;
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
         font-weight: bolder;
         opacity: 0.5;
      }
      button:hover {
         opacity: 1;
      }
   </style>
</head>
<body>
   <h1>Fading Button Example</h1>
   <button>Hover Here</button>
   <p>Hover on the above button to see background fade on button</p>
</body>
</html>

更新日期:2023 年 12 月 14 日

298 次瀏覽

開啟你的 事業

完成課程後獲得認證

開始
廣告
© . All rights reserved.