• Vue通知提醒框(Notification)


    Vue3通知提醒框(Notification)

    可自定义设置以下属性:

    • 自动关闭的延时时长(duration),单位ms,默认4500ms
    • 消息从顶部弹出时,距离顶部的位置(top),单位px,默认24px
    • 消息从底部弹出时,距离底部的位置(bottom),单位px,默认24px
    • 消息弹出位置(placement),可选:左上topLeft,右上topRight(默认),左下bottomLeft,右下bottomRight

    调用时可选以下五个方法对应五种不同样式:

    • this.$refs.notification.open(notification) // 默认使用
    • this.$refs.notification.info(notification) // info调用
    • this.$refs.notification.success(notification) // success调用
    • this.$refs.notification.error(notification) // error调用
    • this.$refs.notification.warn(notification) // warn调用

    五种样式效果如下图:

    open()调用:

    info()调用:

     success()调用:

    error()调用:

    warn()调用:

    ①创建通知提醒框组件Notification:

    1. <script>
    2. export default {
    3. name: 'Notification',
    4. props: {
    5. duration: { // 自动关闭的延时时长,单位ms,默认4500ms;设置null时,不自动关闭
    6. type: Number,
    7. default: 4500
    8. },
    9. top: { // 消息从顶部弹出时,距离顶部的位置,单位像素px
    10. type: Number,
    11. default: 24
    12. },
    13. bottom: { // 消息从底部弹出时,距离底部的位置,单位像素
    14. type: Number,
    15. default: 24
    16. },
    17. placement: { // 消息弹出位置,可选topLeft,topRight,bottomLeft,bottomRight
    18. type: String,
    19. default: 'topRight'
    20. }
    21. },
    22. data () {
    23. return {
    24. colorStyle: { // 颜色主题对象
    25. info: '#1890FF',
    26. success: '#52c41a',
    27. error: '#f5222d',
    28. warn: '#faad14'
    29. },
    30. mode: 'info', // 调用方法对应的样式主题
    31. resetTimer: null,
    32. hideIndex: [],
    33. hideTimers: [],
    34. notificationData: []
    35. }
    36. },
    37. computed: {
    38. clear () { // 所有提示是否已经全部变为false
    39. return this.hideIndex.length === this.notificationData.length
    40. }
    41. },
    42. watch: {
    43. clear (to, from) { // 所有提示都消失后重置
    44. if (!from && to) {
    45. this.resetTimer = setTimeout(() => {
    46. this.hideIndex.splice(0)
    47. this.notificationData.splice(0)
    48. }, 500)
    49. }
    50. }
    51. },
    52. methods: {
    53. onEnter (index) {
    54. clearTimeout(this.hideTimers[index])
    55. this.$set(this.hideTimers, index, null)
    56. },
    57. onLeave (index) {
    58. if (this.duration) {
    59. this.$set(this.hideTimers, index, setTimeout(() => {
    60. this.onHideNotification(index)
    61. }, this.duration))
    62. }
    63. },
    64. show (notification) {
    65. clearTimeout(this.resetTimer)
    66. this.notificationData.push(notification)
    67. this.hideTimers.push(null)
    68. const index = this.notificationData.length - 1
    69. if (this.duration) {
    70. this.$set(this.hideTimers, index, setTimeout(() => {
    71. this.onHideNotification(index)
    72. }, this.duration))
    73. }
    74. },
    75. open (notification) {
    76. this.mode = 'open'
    77. this.show(notification)
    78. },
    79. info (notification) {
    80. this.mode = 'info'
    81. this.show(notification)
    82. },
    83. success (notification) {
    84. this.mode = 'success'
    85. this.show(notification)
    86. },
    87. error (notification) {
    88. this.mode = 'error'
    89. this.show(notification)
    90. },
    91. warn (notification) {
    92. this.mode = 'warn'
    93. this.show(notification)
    94. },
    95. onHideNotification (index) {
    96. this.hideIndex.push(index)
    97. this.$emit('close')
    98. }
    99. }
    100. }
    101. script>
    102. <style lang="less" scoped>
    103. // 渐变过渡效果
    104. .fade-enter-active, .fade-leave-active {
    105. transition: opacity .3s;
    106. }
    107. .fade-enter, .fade-leave-to {
    108. opacity: 0;
    109. }
    110. // 滑动渐变过渡效果
    111. .slide-fade-enter-active, .slide-fade-leave-active {
    112. transition: all .3s ease;
    113. }
    114. .slide-fade-enter, .slide-fade-leave-to {
    115. transform: translateX(408px);
    116. -ms-transform: translateX(408px); /* IE 9 */
    117. -webkit-transform: translateX(408px); /* Safari and Chrome */
    118. opacity: 0;
    119. }
    120. .topRight {
    121. margin-right: 24px;
    122. right: 0;
    123. }
    124. .topLeft {
    125. margin-left: 24px;
    126. left: 0;
    127. }
    128. .bottomRight {
    129. margin-right: 24px;
    130. right: 0;
    131. }
    132. .bottomLeft {
    133. margin-left: 24px;
    134. left: 0;
    135. }
    136. .m-notification-wrap {
    137. position: fixed;
    138. z-index: 999; // 突出显示该层级
    139. width: 384px;
    140. color: rgba(0,0,0,.65);
    141. font-size: 14px;
    142. .m-notification {
    143. margin-bottom: 16px;
    144. padding: 16px 24px;
    145. border-radius: 4px;
    146. box-shadow: 0 4px 12px rgba(0, 0, 0, 15%);
    147. line-height: 1.5;
    148. background: #fff;
    149. transition: all .3s;
    150. position: relative;
    151. .u-status-svg {
    152. width: 24px;
    153. height: 24px;
    154. display: inline-block;
    155. position: absolute;
    156. margin-left: 4px;
    157. }
    158. .u-title {
    159. padding-right: 24px;
    160. display: inline-block;
    161. margin-bottom: 8px;
    162. color: rgba(0,0,0,.85);
    163. font-size: 16px;
    164. line-height: 24px;
    165. }
    166. .u-description {
    167. font-size: 14px;
    168. }
    169. .mb4 {
    170. margin-bottom: 4px;
    171. }
    172. .ml48 {
    173. margin-left: 48px;
    174. }
    175. .u-close {
    176. display: inline-block;
    177. position: absolute;
    178. top: 21px;
    179. right: 24px;
    180. width: 14px;
    181. height: 14px;
    182. fill: rgba(0,0,0,.45);
    183. cursor: pointer;
    184. transition: fill .3s ease;
    185. &:hover {
    186. fill: rgba(0,0,0,.75);
    187. }
    188. }
    189. }
    190. }
    191. style>

    ②在要使用的页面引入:

    1. <Notification
    2. ref="notification"
    3. placement="topRight"
    4. :duration="null"
    5. :top="30"
    6. @close="onClose" />
    7. import Notification from '@/components/Notification'
    8. components: {
    9. Notification
    10. }
    11. onShowNotification () {
    12. const notification = {
    13. title: 'Notification Title',
    14. description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.'
    15. }
    16. this.$refs.notification.open(notification) // 默认使用
    17. // this.$refs.notification.info(notification) // info调用
    18. // this.$refs.notification.success(notification) // success调用
    19. // this.$refs.notification.error(notification) // error调用
    20. // this.$refs.notification.warn(notification) // warning调用
    21. },
    22. onClose () { // 点击默认关闭按钮时触发的回调函数
    23. console.log('关闭notification')
    24. }
  • 相关阅读:
    springcloud+nacos+feign+gateway构建微服务
    为什么远程访问软件是建筑师的必备品
    Pycharm中配置Celery启动
    C++之构造函数列表使用默认值(一百九十一)
    NNDL 实验四 线性分类
    掌握区块链浏览器的使用,应该是每一个币圈人的必修课。
    2001-2021年省、上市公司五年规划产业政策整理代码+匹配结果
    蔬菜水果生鲜配送团购商城小程序的作用是什么
    zookeeper详解
    Nvidia Jetson/Orin +FPGA+AI大算力边缘计算盒子:加油站安全智能检测系统
  • 原文地址:https://blog.csdn.net/Dandrose/article/details/126830202