Notification 通知
在页面一角显示通知信息,常用于系统推送、操作结果等场景。
基础用法
通过 ZcNotification 函数调用,传入标题和内容。
不同类型
通过 type 属性设置通知类型,支持 info、success、warning、danger。
显示位置
通过 position 属性控制通知出现的位置。
自定义时长
通过 duration 控制通知显示时长,设置为 0 则不会自动关闭。
Notification API
Options
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
title | 通知标题 | string | — |
message | 通知正文内容 | string | — |
type | 通知类型 | info | success | warning | danger | info |
duration | 显示时间(毫秒),0 为常驻 | number | 4500 |
showClose | 是否显示关闭按钮 | boolean | true |
position | 通知显示位置 | top-right | top-left | bottom-right | bottom-left | top-right |
offset | 距离边界的偏移量(px) | number | 16 |
onClose | 关闭时的回调函数 | () => void | — |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
close | 通知关闭时触发 | — |
快捷方式
ZcNotification 提供以下快捷方法:
ZcNotification.info(options)- 信息通知ZcNotification.success(options)- 成功通知ZcNotification.warning(options)- 警告通知ZcNotification.danger(options)- 错误通知
每个快捷方法均接受字符串或配置对象(不含 type 字段)。
导出
| 名称 | 描述 |
|---|---|
ZcNotification | 通知函数,含类型快捷方法 |
ZcNotificationCloseAll | 关闭所有通知实例 |
注意事项
- SSR 兼容性:
ZcNotification依赖document对象,在 SSR 环境中调用需确保仅在客户端执行。 - 自动关闭:默认 4.5 秒后自动关闭。设置
duration: 0可实现手动关闭。 - 位置管理:支持
top-right、top-left、bottom-right、bottom-left四个方位,相同位置的通知会自动堆叠并计算偏移量。 - 鼠标悬停:鼠标移入通知区域时会暂停自动关闭计时器,移出后恢复。