通知
使用通知组件可以显示类似推送(或本地)系统通知的必要消息。
通知 App 方法
让我们看看与通知相关的 App 方法来处理通知:
app.notification.create(参数)- 创建通知实例
- 参数 - 对象. 带有通知参数的对象
该方法返回创建的通知实例
app.notification.destroy(el)- 销毁通知实例
- el - HTMLElement或字符串(使用 CSS 选择器) 或对象. 要销毁的通知元素或通知实例。
app.notification.get(el)- 通过 HTML 元素获取通知实例
- el - HTMLElement或字符串(使用 CSS 选择器)。通知元素。
该方法返回通知实例
app.notification.open(el, animate)- 打开通知
- el - HTMLElement或字符串(使用 CSS 选择器)。要打开的通知元素。
- animate - 布尔值. 带动画打开通知。
该方法返回通知实例
app.notification.close(el, animate)- 关闭通知
- el - HTMLElement或字符串(使用 CSS 选择器)。要关闭的通知元素。
- animate - 布尔值. 带动画关闭通知。
该方法返回通知实例
通知参数
现在让我们看看创建通知所需的可用参数列表:
参数 | 类型 | 默认 | 描述 |
---|---|---|---|
el | HTMLElement 字符串 | 通知元素。如果你已经在 HTML 中有通知元素,并想使用这个元素创建新的实例,这会很有用。 | |
icon | 字符串 | 通知图标 HTML 布局,例如:<i class="f7-icons">house</i> 或图像<img src="path/to/icon.png" /> | |
title | 字符串 | 通知标题 | |
titleRightText | 字符串 | 标题右侧的附加文本 | |
subtitle | 字符串 | 通知副标题 | |
text | 字符串 | 通知内部文本 | |
closeButton | 布尔值 | false | 添加通知关闭按钮 |
closeTimeout | 数字 | 自动关闭通知的延迟时间(以毫秒为单位) | |
closeOnClick | 布尔值 | false | 如果启用,点击通知时会关闭通知 |
swipeToClose | 布尔值 | true | 如果启用,可以通过滑动手势关闭通知 |
cssClass | 字符串 | 要添加的附加 CSS 类 | |
render | 函数 | 自定义函数来渲染通知。必须返回通知的 HTML。 | |
on | 对象 | 带有事件处理程序的对象。例如:
|
注意,所有以下参数都可以在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(事件上触发, 处理程序) | 添加事件处理程序 |
notification.once(事件上触发, 处理程序) | 添加在触发后将被移除的事件处理程序 |
notification.off(事件上触发, 处理程序) | 移除事件处理程序 |
notification.off(事件上触发) | 移除指定事件的所有处理程序 |
notification.emit(事件上触发, ...args) | 在实例上触发事件 |
通知事件
通知将在通知元素和 app 及通知实例上触发以下 DOM 事件:
DOM 事件
事件 | Target | 描述 |
---|---|---|
notification:open | 通知元素<div class="notification"> | 当通知开始其打开动画时,将触发此事件 |
notification:opened | 通知元素<div class="notification"> | 当通知完成其打开动画后,将触发此事件 |
notification:close | 通知元素<div class="notification"> | 当通知开始其关闭动画时,将触发此事件 |
notification:closed | 通知元素<div class="notification"> | 当通知完成其关闭动画后,将触发此事件 |
app 和通知实例事件
通知实例在自身实例和 app 实例上都会发出事件。App 实例事件名称前缀为notification
.
事件 | 参数 | Target | 描述 |
---|---|---|---|
click | notification | notification | 当用户点击通知元素时,将触发此事件。作为参数,事件处理程序接收通知实例 |
notificationClick | notification | app | |
open为前缀 | notification | notification | 当通知开始其打开动画时,将触发此事件。作为参数,事件处理程序接收通知实例 |
notificationOpen | notification | app | |
opened | notification | notification | 当通知完成其打开动画后,将触发此事件。作为参数,事件处理程序接收通知实例 |
notificationOpened | notification | app | |
close | notification | notification | 当通知开始其关闭动画时,将触发此事件。作为参数,事件处理程序接收通知实例 |
notificationClose | notification | app | |
closed | notification | notification | 当通知完成其关闭动画后,将触发此事件。作为参数,事件处理程序接收通知实例 |
notificationClosed | notification | app | |
beforeDestroy | notification | notification | 在通知实例将被销毁之前,将触发此事件。作为参数,事件处理程序接收通知实例 |
notificationBeforeDestroy | notification | app |
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>