Toast
Toast 會透過螢幕上的訊息提供有關操作的簡要回饋。
Toast App 方法
讓我們來看看相關的 App 方法,以使用 Toast
app.toast.create(parameters)- 建立 Toast 實例
- parameters - object。包含 Toast 參數的物件
方法傳回已建立的 Toast 實例
app.toast.destroy(el)- 銷毀 Toast 實例
- el - HTMLElement 或 string (使用 CSS 選擇器) 或 object。要銷毀的 Toast 元素或 Toast 實例。
app.toast.get(el)- 透過 HTML 元素取得 Toast 實例
- el - HTMLElement 或 string (使用 CSS 選擇器)。Toast 元素。
方法傳回 Toast 實例
app.toast.open(el, animate)- 開啟 Toast
- el - HTMLElement 或 string (使用 CSS 選擇器)。要開啟的 Toast 元素。
- animate - boolean。使用動畫開啟 Toast。
方法傳回 Toast 實例
app.toast.close(el, animate)- 關閉 Toast
- el - HTMLElement 或 string (使用 CSS 選擇器)。要關閉的 Toast 元素。
- animate - boolean。使用動畫關閉 Toast。
方法傳回 Toast 實例
app.toast.show(parameters)- 建立 Toast 實例並立即顯示
- parameters - object。包含 Toast 參數的物件
方法傳回已建立的 Toast 實例
Toast 參數
現在讓我們來看看建立 Toast 所需的可用參數清單
參數 | 類型 | 預設值 | 說明 |
---|---|---|---|
el | HTMLElement | Toast 元素。如果你已在 HTML 中有 Toast 元素,而且想使用此元素建立新實例,這會很有用 | |
圖示 | 字串 | Toast 圖示 HTML 佈局,例如 <i class="f7-icons">house</i> 或圖片 <img src="path/to/icon.png" /> | |
文字 | 字串 | Toast 內部文字 | |
位置 | 字串 | 底部 | Toast 位置。可以是 底部 、中央 或 頂部 |
水平位置 | 字串 | 左 | Toast 在寬螢幕上的水平對齊。僅對頂部和底部 Toast 有影響。可以是 左 、中央 或 右 |
關閉按鈕 | 布林值 | 否 | 新增 Toast 關閉按鈕 |
關閉按鈕顏色 | 字串 | 預設顏色主題之一 | |
關閉按鈕文字 | 字串 | 確定 | 關閉按鈕文字 |
關閉逾時 | 數字 | 逾時延遲 (毫秒) 以自動關閉 Toast | |
CSS 類別 | 字串 | 要新增的附加 CSS 類別 | |
關閉時銷毀 | 布林值 | 否 | 關閉時銷毀 Toast 執行個體 |
呈現 | 函式 | 自訂函式以呈現 Toast。必須傳回 Toast HTML | |
容器元素 | HTMLElement 字串 | 允許將 Toast 安裝到自訂元素,而不是應用程式根元素 | |
開啟 | 物件 | 包含事件處理常式的物件。例如
|
請注意,所有下列參數都可以在 toast
屬性下的全域應用程式參數中使用,以設定所有 Toast 的預設值。例如
var app = new Framework7({
toast: {
closeTimeout: 3000,
closeButton: true,
}
});
Toast 方法與屬性
因此,要建立 Toast,我們必須呼叫
var toast = app.toast.create({ /* parameters */ })
之後,我們會擁有其初始化的執行個體 (例如上面範例中的 toast
變數),其中包含有用的方法和屬性
屬性 | |
---|---|
toast.app | 連結到全域應用程式執行個體 |
toast.el | Toast HTML 元素 |
toast.$el | 包含 Toast HTML 元素的 Dom7 執行個體 |
toast.params | Toast 參數 |
方法 | |
toast.open() | 開啟 Toast |
toast.close() | 關閉 Toast |
toast.on(事件, 處理常式) | 新增事件處理常式 |
toast.once(事件, 處理常式) | 新增事件處理常式,該常式在觸發後會被移除 |
toast.off(事件, 處理常式) | 移除事件處理常式 |
toast.off(事件) | 移除指定事件的所有處理常式 |
toast.emit(event, ...args) | 在實例上觸發事件 |
Toast 事件
Toast 會在 toast 元素上觸發下列 DOM 事件,以及在 app 和 toast 實例上觸發事件
DOM 事件
事件 | 目標 | 說明 |
---|---|---|
toast:open | Toast 元素<div class="toast"> | 當 Toast 開始其開啟動畫時,將觸發事件 |
toast:opened | Toast 元素<div class="toast"> | 當 Toast 完成其開啟動畫後,將觸發事件 |
toast:close | Toast 元素<div class="toast"> | 當 Toast 開始其關閉動畫時,將觸發事件 |
toast:closed | Toast 元素<div class="toast"> | 當 Toast 完成其關閉動畫後,將觸發事件 |
App 和 Toast 實例事件
Toast 實例在自身實例和 app 實例上發射事件。App 實例事件具有相同的名稱,並以 toast
為前綴。
事件 | 引數 | 目標 | 說明 |
---|---|---|---|
closeButtonClick | toast | toast | 當使用者按一下 Toast 關閉按鈕時,將觸發事件。事件處理常式會接收 toast 實例作為引數 |
toastCloseButtonClick | toast | app | |
open | toast | toast | 當 Toast 開始其開啟動畫時,將觸發事件。事件處理常式會接收 toast 實例作為引數 |
toastOpen | toast | app | |
opened | toast | toast | 當 Toast 完成其開啟動畫後,將觸發事件。事件處理常式會接收 toast 實例作為引數 |
toastOpened | toast | app | |
close | toast | toast | 當 Toast 開始其關閉動畫時,將觸發事件。事件處理常式會接收 toast 實例作為引數 |
toastClose | toast | app | |
closed | toast | toast | 當 Toast 完成其關閉動畫後,將觸發事件。事件處理常式會接收 toast 實例作為引數 |
toastClosed | toast | app | |
beforeDestroy | toast | toast | 當 Toast 實例即將被銷毀時,將觸發事件。事件處理常式會接收 toast 實例作為引數 |
toastBeforeDestroy | toast | app |
CSS 變數
以下是相關 CSS 變數 (CSS 自訂屬性) 清單。
:root {
--f7-toast-font-size: 14px;
--f7-toast-icon-size: 48px;
--f7-toast-max-width: 568px;
}
.ios {
--f7-toast-text-color: #fff;
--f7-toast-bg-color: rgba(0, 0, 0, 0.75);
--f7-toast-bg-color-rgb: 0, 0, 0;
--f7-toast-padding-horizontal: 16px;
--f7-toast-padding-vertical: 12px;
--f7-toast-border-radius: 8px;
--f7-toast-button-min-width: 64px;
}
.md {
--f7-toast-padding-horizontal: 24px;
--f7-toast-padding-vertical: 14px;
--f7-toast-border-radius: 16px;
--f7-toast-button-min-width: 64px;
}
.md,
.md .dark,
.md [class*='color-'] {
--f7-toast-text-color: var(--f7-md-on-surface);
--f7-toast-bg-color: var(--f7-md-surface-5);
}
範例
toast.html
<template>
<div class="page">
<div class="navbar">
<div class="navbar-bg"></div>
<div class="navbar-inner sliding">
<div class="title">Toast</div>
</div>
</div>
<div class="page-content">
<div class="block block-strong-ios block-outline-ios">
<p>Toasts provide brief feedback about an operation through a message on the screen.</p>
<p>
<a class="button button-fill" @click=${showToastBottom}>Toast on Bottom</a>
</p>
<p>
<a class="button button-fill" @click=${showToastTop}>Toast on Top</a>
</p>
<p>
<a class="button button-fill" @click=${showToastCenter}>Toast on Center</a>
</p>
<p>
<a class="button button-fill" @click=${showToastIcon}>Toast with icon</a>
</p>
<p>
<a class="button button-fill" @click=${showToastLargeMessage}>Toast with large message</a>
</p>
<p>
<a class="button button-fill" @click=${showToastWithButton}>Toast with close button</a>
</p>
<p>
<a class="button button-fill" @click=${showToastWithCustomButton}>Toast with custom button</a>
</p>
<p>
<a class="button button-fill" @click=${showToastWithCallback}>Toast with callback on close</a>
</p>
</div>
</div>
</div>
</template>
<script>
export default (props, { $f7, $theme, $on }) => {
let toastBottom;
let toastTop;
let toastCenter;
let toastIcon;
let toastLargeMessage;
let toastWithButton;
let toastWithCustomButton;
let toastWithCallback;
const showToastBottom = () => {
// Create toast
if (!toastBottom) {
toastBottom = $f7.toast.create({
text: 'This is default bottom positioned toast',
closeTimeout: 2000,
});
}
// Open it
toastBottom.open();
}
const showToastTop = () => {
// Create toast
if (!toastTop) {
toastTop = $f7.toast.create({
text: 'Top positioned toast',
position: 'top',
closeTimeout: 2000,
});
}
// Open it
toastTop.open();
}
const showToastCenter = () => {
// Create toast
if (!toastCenter) {
toastCenter = $f7.toast.create({
text: 'I\'m on center',
position: 'center',
closeTimeout: 2000,
});
}
// Open it
toastCenter.open();
}
const showToastIcon = () => {
// Create toast
if (!toastIcon) {
toastIcon = $f7.toast.create({
icon: $theme.ios ? '<i class="f7-icons">star_fill</i>' : '<i class="material-icons">star</i>',
text: 'I\'m with icon',
position: 'center',
closeTimeout: 2000,
});
}
// Open it
toastIcon.open();
}
const showToastLargeMessage = () => {
// Create toast
if (!toastLargeMessage) {
toastLargeMessage = $f7.toast.create({
text: 'This toast contains a lot of text. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil, quae, ab. Delectus amet optio facere autem sapiente quisquam beatae culpa dolore.',
closeTimeout: 2000,
});
}
// Open it
toastLargeMessage.open();
}
const showToastWithButton = () => {
// Create toast
if (!toastWithButton) {
toastWithButton = $f7.toast.create({
text: 'Toast with additional close button',
closeButton: true,
});
}
// Open it
toastWithButton.open();
}
const showToastWithCustomButton = () => {
// Create toast
if (!toastWithCustomButton) {
toastWithCustomButton = $f7.toast.create({
text: 'Custom close button',
closeButton: true,
closeButtonText: 'Close Me',
closeButtonColor: 'red',
});
}
// Open it
toastWithCustomButton.open();
}
const showToastWithCallback = () => {
// Create toast
if (!toastWithCallback) {
toastWithCallback = $f7.toast.create({
text: 'Callback on close',
closeButton: true,
on: {
close: function () {
$f7.dialog.alert('Toast closed');
},
}
});
}
// Open it
toastWithCallback.open();
}
$on('pageBeforeOut', () => {
$f7.toast.close();
})
$on('pageBeforeRemove', () => {
// Destroy toasts when page removed
if (toastBottom) toastBottom.destroy();
if (toastTop) toastTop.destroy();
if (toastCenter) toastCenter.destroy();
if (toastIcon) toastIcon.destroy();
if (toastLargeMessage) toastLargeMessage.destroy();
if (toastWithButton) toastWithButton.destroy();
if (toastWithCustomButton) toastWithCustomButton.destroy();
if (toastWithCallback) toastWithCallback.destroy();
})
return $render;
}
</script>