使用 HTML、CSS 和 Javascript 建立 SnackBar


Snackbar 是出現在網頁底部的通知欄,用於顯示重要資訊,通常用於確認操作或向用戶提供反饋。它在螢幕上顯示幾秒鐘後就會消失。它們是使用者介面設計的必要元素,廣泛應用於 Web 應用程式中。

方法 1

在這裡,我們將建立一個簡單基本的 Snackbar,它將顯示 3 秒鐘,並在使用者點選“顯示 Snackbar”按鈕時消失。

演算法

  • 將 HTML 文件的標題設定為 Snackbar

  • 在樣式部分:

    • 將主體內容居中。

    • 根據設計為按鈕設定樣式。

    • 預設情況下隱藏 Snackbar。

    • 為 Snackbar 的顯示和隱藏新增動畫。

  • 將標題設定為 Snackbar。

  • 新增按鈕和 Snackbar 元素。

  • 在指令碼部分:

    • 建立一個函式來顯示 Snackbar,並在 3 秒後將其移除。

示例

<!DOCTYPE html>
<html>
<head>
   <title>Snackbar</title>
   <style>
      body {
         background-color:lavender;
         /* Center the contents of body */
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction:column;
      }
      /* Custom button styling */
      .button {
         display: inline-block;
         padding: 10px 20px;
         background-color: #4CAF50;
         color: white;
         border-radius: 4px;
         text-decoration: none;
         font-size: 16px;
         cursor: pointer;
      }

      /* Snackbar styling */
      #snackbar {
         /* Hide the snackbar by default */
         visibility: hidden;
         min-width: 250px;
         margin-left: -125px;
         background-color: #333;
         color: #fff;
         text-align: center;
         border-radius: 2px;
         padding: 16px;
         position: fixed;
         z-index: 1;
         left: 50%;
         bottom: 30px;
      }

      /* Show the snackbar */
      #snackbar.show {
         visibility: visible;
         /* Add animation here */
         animation: fadein 0.5s, fadeout 0.5s 2.5s;
      }

      /* Snackbar animation */
      @keyframes fadein {
         from {bottom: 0; opacity: 0;}
         to {bottom: 30px; opacity: 1;}
      }

      @keyframes fadeout {
         from {bottom: 30px; opacity: 1;}
         to {bottom: 0; opacity: 0;}
      }
   </style>
</head>
<body>
   <h1>Snackbar</h1>
   <!-- Custom button element -->
   <button class="button" onclick="showSnackbar()">Show Snackbar</button>
   <!-- Snackbar element -->
   <div id="snackbar">Welcome to Tutorialspoint!</div>
   <script>
      function showSnackbar() {
         // Get the snackbar element
         var snackbar = document.getElementById("snackbar");

         // Add the "show" class to the snackbar element
         snackbar.className = "show";

         // After 3 seconds, remove the "show" class from the snackbar element
         setTimeout(function(){ snackbar.className = snackbar.className.replace("show", ""); }, 3000);
      }
   </script>
</body>
</html>

方法 2

我們還可以為 Snackbar 新增一些操作,例如關閉,以便立即關閉或隱藏 Snackbar。

演算法

  • 將標題設定為帶有關閉操作的 Snackbar。

  • 在樣式部分:

    • 為關閉操作設定樣式。

    • 為其新增懸停效果。

  • 將標題設定為帶有關閉操作的 Snackbar。

  • 新增按鈕和 Snackbar 元素。

  • 在指令碼部分:

    • 建立一個函式來顯示 Snackbar。

    • 另一個用於關閉它的函式。

示例

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>Snackbar with dismiss action</title>
   <style>
      /* Styling for the body element */
      body {
         display: flex;
         justify-content: center;
         align-items: center;
         flex-direction: column;
         background-color: lavender;
      }
      /* Styling for the button element */
      button {
         display: inline-block;
         padding: 10px 20px;
         background-color: #4CAF50;
         color: white;
         border-radius: 4px;
         text-decoration: none;
         font-size: 16px;
         cursor: pointer;
      }
      /* Styling for the snackbar element */
      #snackbar {
         visibility: hidden;
         min-width: 250px;
         margin-left: -125px;
         background-color: #333;
         color: #fff;
         text-align: center;
         border-radius: 2px;
         padding: 16px;
         position: fixed;
         z-index: 1;
         left: 50%;
         bottom: 30px;
         font-size: 17px;
      }
      /* Styling for the snackbar element when it is visible */
      #snackbar.show {
         visibility: visible;
         -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
         animation: fadein 0.5s, fadeout 0.5s 2.5s;
      }
      /* Animation for fading in the snackbar element */
      @-webkit-keyframes fadein {
         from {bottom: 0; opacity: 0;}
         to {bottom: 30px; opacity: 1;}
      }
      @keyframes fadein {
         from {bottom: 0; opacity: 0;}
         to {bottom: 30px; opacity: 1;}
      }
      /* Animation for fading out the snackbar element */
      @-webkit-keyframes fadeout {
         from {bottom: 30px; opacity: 1;}
         to {bottom: 0; opacity: 0;}
      }
      @keyframes fadeout {
         from {bottom: 30px; opacity: 1;}
         to {bottom: 0; opacity: 0;}
      }
      /* Styling for the dismiss action within the snackbar */
      #snackbarAction {
         color: #fff;
         cursor: pointer;
         border-radius: 2px;
         padding: 4px 8px;
         font-size: 14px;
         background-color: #5f5f5f;
         display: inline-block;
         margin-left: 16px;
      }
      /* Styling for the dismiss action within the snackbar when hovered over */
      #snackbarAction:hover {
         background-color: #4d4d4d;
      }
   </style>
</head>
<body>
   <h1>Snackbar with dismiss action</h1>
   <button onclick="showSnackbar()">Show Snackbar</button>
   <div id="snackbar">Snackbar message<span id="snackbarAction" onclick="dismissSnackbar()">Dismiss</span></div>
   <script>
      // Function to show the snackbar element
      function showSnackbar() {
         var snackbar = document.getElementById("snackbar");
         snackbar.className = "show";
      }
      // Function to dismiss the snackbar element
      function dismissSnackbar() {
         var snackbar = document.getElementById("snackbar");
         snackbar.className = "";
      }
   </script>
</body>
</html>

方法 3

為了獲得簡潔優雅的外觀,需要自定義 Snackbar 以匹配網站設計。

演算法

  • 設定相關的標題。

  • 設定主體樣式。

  • 使用 CSS 自定義 Snackbar。

  • 新增 Javascript 功能。

示例

<!DOCTYPE html>
<html>
<head>
   <title>Custom Snackbar Example</title>
   <style>
      /* styling for the body */
      body {
         display: flex;
         align-items: center;
         justify-content: center;
         height: 100vh;
         margin: 0;
         padding: 0;
         background-color: lavender;
         flex-direction: column;
      }
      /* styling for the button */
      .button {
         display: inline-block;
         padding: 10px 20px;
         background-color: #4CAF50;
         color: white;
         border-radius: 4px;
         text-decoration: none;
         font-size: 16px;
         cursor: pointer;
      }
      /* styling for the snackbar */
      .snackbar {
         background-color: #61a4f0;
         color: #fff;
         display: flex;
         align-items: center;
         justify-content: space-between;
         position: fixed;
         bottom: 20px;
         left: 50%;
         transform: translateX(-50%);
         border-radius: 40px;
         padding: 20px;
         width: 300px;
         box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
         z-index: 1;
         animation: slideIn 0.5s ease-in-out;
      }
      /* hiding the snackbar */
      .snackbar.hidden {
         display: none;
      }
      /* styling for the snackbar dismiss button */
      .snackbar button {
         background-color: transparent;
         color: #fff;
         border:none;
         font-size: 1rem;
         font-weight: bold;
         cursor: pointer;
         margin-left: 10px;
      }
      /* defining the slide-in animation for snackbar */
      @keyframes slideIn {
         from {
            bottom: -100px;
            opacity: 0;
         }
         to {
            bottom: 20px;
            opacity: 1;
         }
      }
   </style>
</head>
<body>
   <h1>Coloured Snackbar</h1>
   <button class="button" onclick="showSnackbar()">Show Snackbar</button>
   <div id="snackbar" class="snackbar hidden">
      <span>Snackbar Message</span>
      <button onclick="hideSnackbar()">Dismiss</button>
   </div>
   <script>
      /* function to show the snackbar */
      function showSnackbar() {
         var snackbar = document.getElementById("snackbar");
         snackbar.classList.remove("hidden");
         /* hiding the snackbar after 3 seconds */
         setTimeout(function(){ snackbar.classList.add("hidden"); }, 3000);
      }

      /* function to hide the snackbar */
      function hideSnackbar() {
         var snackbar = document.getElementById("snackbar");
         snackbar.classList.add("hidden");
      }
   </script>
</body>
</html>  

結論

使用者介面中的 Snackbar 用於向用戶顯示簡短明瞭的資訊。使這些訊息可關閉至關重要。它應該僅用於顯示需要使用者注意的重要訊息。另一個重要說明是,這些訊息不應包含任何機密和敏感資訊。顯示 Snackbar 的持續時間必須較短。

更新於: 2023年5月19日

982 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.