通知

使用通知元件,你可以顯示必要的訊息,看起來就像 Push(或本機)系統通知。

通知應用程式方法

讓我們看看相關的應用程式方法,以使用通知

app.notification.create(parameters)- 建立通知實例

  • parameters - object。具有通知參數的物件

方法傳回建立的通知實例

app.notification.destroy(el)- 銷毀通知實例

  • el - HTMLElementstring(使用 CSS 選擇器)或 object。要銷毀的通知元素或通知實例。

app.notification.get(el)- 透過 HTML 元素取得通知實例

  • el - HTMLElementstring(使用 CSS 選擇器)。通知元素。

方法傳回通知實例

app.notification.open(el, animate)- 開啟通知

  • el - HTMLElementstring(使用 CSS 選擇器)。要開啟的通知元素。
  • animate - boolean。使用動畫開啟通知。

方法傳回通知實例

app.notification.close(el, animate)- 關閉通知

  • el - HTMLElementstring(使用 CSS 選擇器)。要關閉的通知元素。
  • animate - boolean。使用動畫關閉通知。

方法傳回通知實例

通知參數

現在讓我們看看建立通知所需的可用參數清單

參數類型預設值說明
elHTMLElement
string
通知元件。如果你在 HTML 中已經有通知元件,而且想要使用這個元件建立新的執行個體,這會很有用
圖示string通知圖示 HTML 佈局,例如 <i class="f7-icons">house</i> 或圖片 <img src="path/to/icon.png" />
標題string通知標題
titleRightTextstring標題右側的額外文字
子標題string通知子標題
文字string通知內文
closeButton布林值false新增通知關閉按鈕
closeTimeout數字自動關閉通知的延遲時間(毫秒)
closeOnClick布林值false如果啟用,通知會在點擊通知時關閉
swipeToClose布林值true如果啟用,通知可以用滑動手勢關閉
cssClassstring要新增的額外 css 類別
render函式自訂函式用於呈現通知。必須回傳通知 html
on物件

包含事件處理常式的物件。例如

var notification = app.notification.create({
  title: 'John Doe',
  text: 'Hello, how are you?',
  on: {
    opened: function () {
      console.log('Notification opened')
    }
  }
})

請注意,所有下列參數都可以在 notification 屬性下的全域應用程式參數中使用,以設定所有通知的預設值。例如

var app = new Framework7({
  notification: {
    title: 'My App',
    closeTimeout: 3000,
  }
});

通知方法和屬性

因此,要建立通知,我們必須呼叫

var notification = app.notification.create({ /* parameters */ })

然後我們有它的初始化執行個體(例如上面範例中的 notification 變數),包含有用的方法和屬性

屬性
notification.app連結到全域應用程式執行個體
notification.el通知 HTML 元件
notification.$el包含通知 HTML 元件的 Dom7 執行個體
notification.params通知參數
方法
notification.open()開啟通知
notification.close()關閉通知
notification.on(event, handler)新增事件處理常式
notification.once(event, handler)新增事件處理常式,在觸發後會移除
notification.off(event, handler)移除事件處理常式
notification.off(event)移除指定事件的所有處理常式
notification.emit(event, ...args)在執行個體上觸發事件

通知事件

通知會在通知元件上觸發下列 DOM 事件,以及應用程式和通知執行個體上的事件

DOM 事件

事件目標說明
notification:open通知元素<div class="notification">當通知開始其開啟動畫時觸發事件
notification:opened通知元素<div class="notification">當通知完成其開啟動畫後觸發事件
notification:close通知元素<div class="notification">當通知開始其關閉動畫時觸發事件
notification:closed通知元素<div class="notification">當通知完成其關閉動畫後觸發事件

應用程式和通知實例事件

通知實例在自身實例和應用程式實例上發出事件。應用程式實例事件具有相同的名稱,並以notification為前綴。

事件參數目標說明
clicknotificationnotification當使用者按一下通知元素時觸發事件。作為參數,事件處理常式接收通知實例
notificationClicknotificationapp
opennotificationnotification當通知開始其開啟動畫時觸發事件。作為參數,事件處理常式接收通知實例
notificationOpennotificationapp
openednotificationnotification當通知完成其開啟動畫後觸發事件。作為參數,事件處理常式接收通知實例
notificationOpenednotificationapp
closenotificationnotification當通知開始其關閉動畫時觸發事件。作為參數,事件處理常式接收通知實例
notificationClosenotificationapp
closednotificationnotification當通知完成其關閉動畫後觸發事件。作為參數,事件處理常式接收通知實例
notificationClosednotificationapp
beforeDestroynotificationnotification當通知實例即將被銷毀之前觸發事件。作為參數,事件處理常式接收通知實例
notificationBeforeDestroynotificationapp

CSS 變數

以下是相關CSS 變數(CSS 自訂屬性)的清單。

:root {
  --f7-notification-max-width: 568px;
  --f7-notification-subtitle-text-transform: none;
  --f7-notification-subtitle-line-height: 1.35;
  --f7-notification-text-text-transform: none;
  --f7-notification-text-font-weight: 400;
}
.ios {
  --f7-notification-margin: 8px;
  --f7-notification-padding-horizontal: 10px;
  --f7-notification-padding-vertical: 10px;
  --f7-notification-border-radius: 12px;
  --f7-notification-box-shadow: 0px 5px 25px -10px rgba(0, 0, 0, 0.7);
  --f7-notification-icon-size: 20px;
  --f7-notification-title-font-size: 13px;
  --f7-notification-title-text-transform: uppercase;
  --f7-notification-title-line-height: 1.4;
  --f7-notification-title-font-weight: 400;
  --f7-notification-title-letter-spacing: 0.02em;
  --f7-notification-title-right-font-size: 13px;
  --f7-notification-subtitle-font-size: 15px;
  --f7-notification-subtitle-font-weight: 600;
  --f7-notification-text-font-size: 15px;
  --f7-notification-text-line-height: 1.2;
  --f7-notification-bg-color: rgba(250, 250, 250, 0.95);
  --f7-notification-bg-color-rgb: 255, 255, 255;
  --f7-notification-title-color: #000;
  --f7-notification-title-right-color: rgba(0, 0, 0, 0.45);
  --f7-notification-subtitle-color: #000;
  --f7-notification-text-color: #000;
}
.ios .dark,
.ios.dark {
  --f7-notification-bg-color: rgba(30, 30, 30, 0.95);
  --f7-notification-bg-color-rgb: 30, 30, 30;
  --f7-notification-title-color: #fff;
  --f7-notification-text-color: #fff;
  --f7-notification-subtitle-color: #fff;
  --f7-notification-title-right-color: rgba(255, 255, 255, 0.55);
}
.md {
  --f7-notification-margin: 16px;
  --f7-notification-padding-vertical: 20px;
  --f7-notification-padding-horizontal: 16px;
  --f7-notification-border-radius: 16px;
  --f7-notification-box-shadow: none;
  --f7-notification-icon-size: 24px;
  --f7-notification-title-font-size: 16px;
  --f7-notification-title-text-transform: none;
  --f7-notification-title-line-height: 1.35;
  --f7-notification-title-font-weight: 500;
  --f7-notification-title-right-font-size: 12px;
  --f7-notification-subtitle-font-size: 14px;
  --f7-notification-subtitle-font-weight: 400;
  --f7-notification-text-font-size: 14px;
  --f7-notification-text-line-height: 1.35;
}
.md,
.md .dark,
.md [class*='color-'] {
  --f7-notification-title-color: var(--f7-md-on-surface);
  --f7-notification-bg-color: var(--f7-md-surface-5);
  --f7-notification-text-color: var(--f7-md-on-surface-variant);
  --f7-notification-title-right-color: var(--f7-md-on-surface-variant);
  --f7-notification-subtitle-color: var(--f7-md-on-surface);
}

範例

notifications.html
<template>
  <div class="page">
    <div class="navbar">
      <div class="navbar-bg"></div>
      <div class="navbar-inner sliding">
        <div class="title">Notifications</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block block-strong-ios block-outline-ios">
        <p>Framework7 comes with simple Notifications component that allows you to show some useful messages to user and
          request basic actions.</p>
        <p><a class="button button-fill" @click=${showNotificationFull}>Full layout notification</a></p>
        <p><a class="button button-fill" @click=${showNotificationWithButton}>With close button</a></p>
        <p><a class="button button-fill" @click=${showNotificationCloseOnClick}>Click to close</a></p>
        <p><a class="button button-fill" @click=${showNotificationCallbackOnClose}>Callback on close</a></p>
      </div>
    </div>
  </div>
</template>
<script>
  export default (props, { $f7, $el, $on }) => {
    let notificationFull;
    let notificationWithButton;
    let notificationCloseOnClick;
    let notificationCallbackOnClose;

    const showNotificationFull = () => {
      // Create notification
      if (!notificationFull) {
        notificationFull = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          titleRightText: 'now',
          subtitle: 'This is a subtitle',
          text: 'This is a simple notification message',
          closeTimeout: 3000,
        });
      }
      // Open it
      notificationFull.open();
    }
    const showNotificationWithButton = () => {
      // Create notification
      if (!notificationWithButton) {
        notificationWithButton = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          subtitle: 'Notification with close button',
          text: 'Click (x) button to close me',
          closeButton: true,
        });
      }
      // Open it
      notificationWithButton.open();
    }
    const showNotificationCloseOnClick = () => {
      // Create notification
      if (!notificationCloseOnClick) {
        notificationCloseOnClick = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          titleRightText: 'now',
          subtitle: 'Notification with close on click',
          text: 'Click me to close',
          closeOnClick: true,
        });
      }
      // Open it
      notificationCloseOnClick.open();
    }
    const showNotificationCallbackOnClose = () => {
      // Create notification
      if (!notificationCallbackOnClose) {
        notificationCallbackOnClose = $f7.notification.create({
          icon: '<i class="icon icon-f7"></i>',
          title: 'Framework7',
          titleRightText: 'now',
          subtitle: 'Notification with close on click',
          text: 'Click me to close',
          closeOnClick: true,
          on: {
            close: function () {
              $f7.dialog.alert('Notification closed');
            },
          },
        });
      }
      // Open it
      notificationCallbackOnClose.open();
    }

    $on('pageBeforeRemove', () => {
      $f7.notification.close();
      if (notificationFull) notificationFull.destroy();
      if (notificationWithButton) notificationWithButton.destroy();
      if (notificationCloseOnClick) notificationCloseOnClick.destroy();
      if (notificationCallbackOnClose) notificationCallbackOnClose.destroy();
    });

    return $render;
  };
</script>