程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Vue通知提醒框(Notification)

发布于2022-12-03 15:16     阅读(1198)     评论(0)     点赞(14)     收藏(1)


项目相关依赖版本信息

可自定义设置以下属性:

  • 自动关闭的延时时长(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.warning(notification) // warning调用

五种样式效果如下图:

open()调用:

info()调用:

 success()调用:

error()调用:

warning()调用:

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

  1. <template>
  2. <div :class="['m-notification-wrap', placement]" :style="`top: ${placement.includes('top') ? top : ''}px; bottom: ${placement.includes('bottom') ? bottom : ''}px;`">
  3. <transition name="slide-fade" v-for="(data, index) in notificationData" :key="index">
  4. <div class="m-notification" v-if="showNotification[index]">
  5. <div class="m-notification-content">
  6. <svg class="u-status-svg" v-if="mode==='info'" :fill="colorStyle[mode]" viewBox="64 64 896 896" data-icon="info-circle" aria-hidden="true" focusable="false"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z"></path><path d="M464 336a48 48 0 1 0 96 0 48 48 0 1 0-96 0zm72 112h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V456c0-4.4-3.6-8-8-8z"></path></svg>
  7. <svg class="u-status-svg" v-if="mode==='success'" :fill="colorStyle[mode]" viewBox="64 64 896 896" data-icon="check-circle" aria-hidden="true" focusable="false"><path d="M699 353h-46.9c-10.2 0-19.9 4.9-25.9 13.3L469 584.3l-71.2-98.8c-6-8.3-15.6-13.3-25.9-13.3H325c-6.5 0-10.3 7.4-6.5 12.7l124.6 172.8a31.8 31.8 0 0 0 51.7 0l210.6-292c3.9-5.3.1-12.7-6.4-12.7z"></path><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z"></path></svg>
  8. <svg class="u-status-svg" v-if="mode==='warning'" :fill="colorStyle[mode]" viewBox="64 64 896 896" data-icon="exclamation-circle" aria-hidden="true" focusable="false"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z"></path><path d="M464 688a48 48 0 1 0 96 0 48 48 0 1 0-96 0zm24-112h48c4.4 0 8-3.6 8-8V296c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8z"></path></svg>
  9. <svg class="u-status-svg" v-if="mode==='error'" :fill="colorStyle[mode]" viewBox="64 64 896 896" data-icon="close-circle" aria-hidden="true" focusable="false"><path d="M685.4 354.8c0-4.4-3.6-8-8-8l-66 .3L512 465.6l-99.3-118.4-66.1-.3c-4.4 0-8 3.5-8 8 0 1.9.7 3.7 1.9 5.2l130.1 155L340.5 670a8.32 8.32 0 0 0-1.9 5.2c0 4.4 3.6 8 8 8l66.1-.3L512 564.4l99.3 118.4 66 .3c4.4 0 8-3.5 8-8 0-1.9-.7-3.7-1.9-5.2L553.5 515l130.1-155c1.2-1.4 1.8-3.3 1.8-5.2z"></path><path d="M512 65C264.6 65 64 265.6 64 513s200.6 448 448 448 448-200.6 448-448S759.4 65 512 65zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z"></path></svg>
  10. <div :class="['u-title', {'mb4': mode!=='open', 'ml48': mode!=='open'}]">{{ data.title || '--' }}</div>
  11. <p :class="['u-description', {'ml48': mode!=='open'}]">{{ data.description || '--' }}</p>
  12. </div>
  13. <svg class="u-close" @click="onHideNotification(index)" viewBox="64 64 896 896" data-icon="close" aria-hidden="true" focusable="false"><path d="M563.8 512l262.5-312.9c4.4-5.2.7-13.1-6.1-13.1h-79.8c-4.7 0-9.2 2.1-12.3 5.7L511.6 449.8 295.1 191.7c-3-3.6-7.5-5.7-12.3-5.7H203c-6.8 0-10.5 7.9-6.1 13.1L459.4 512 196.9 824.9A7.95 7.95 0 0 0 203 838h79.8c4.7 0 9.2-2.1 12.3-5.7l216.5-258.1 216.5 258.1c3 3.6 7.5 5.7 12.3 5.7h79.8c6.8 0 10.5-7.9 6.1-13.1L563.8 512z"></path></svg>
  14. </div>
  15. </transition>
  16. </div>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'Notification',
  21. props: {
  22. duration: { // 自动关闭的延时时长,单位ms,默认4500ms;设置null时,不自动关闭
  23. type: Number,
  24. default: 4500
  25. },
  26. top: { // 消息从顶部弹出时,距离顶部的位置,单位像素px
  27. type: Number,
  28. default: 24
  29. },
  30. bottom: { // 消息从底部弹出时,距离底部的位置,单位像素
  31. type: Number,
  32. default: 24
  33. },
  34. placement: { // 消息弹出位置,可选topLeft,topRight,bottomLeft,bottomRight
  35. type: String,
  36. default: 'topRight'
  37. }
  38. },
  39. data () {
  40. return {
  41. colorStyle: { // 颜色主题对象
  42. info: '#1890FF',
  43. success: '#52c41a',
  44. error: '#f5222d',
  45. warning: '#faad14'
  46. },
  47. mode: 'info', // 调用方法对应的样式主题
  48. resetTimer: null,
  49. showNotification: [],
  50. hideTimers: [],
  51. notificationData: []
  52. }
  53. },
  54. computed: {
  55. clear () { // 所有提示是否已经全部变为false
  56. return this.showNotification.every(item => !item)
  57. }
  58. },
  59. watch: {
  60. clear (to, from) { // 所有提示都消失后重置
  61. if (!from && to) {
  62. this.resetTimer = setTimeout(() => {
  63. this.notificationData.splice(0)
  64. this.showNotification.splice(0)
  65. }, 500)
  66. }
  67. }
  68. },
  69. methods: {
  70. onEnter (index) {
  71. clearTimeout(this.hideTimers[index])
  72. },
  73. onLeave (index) {
  74. if (this.duration) {
  75. this.onHideNotification(index)
  76. }
  77. },
  78. show (notification) {
  79. clearTimeout(this.resetTimer)
  80. this.notificationData.push(notification)
  81. const index = this.notificationData.length - 1
  82. console.log('index:', index)
  83. this.$nextTick(() => { // 待异步更新队列之后显示提示框,否则过渡效果会异常
  84. this.$set(this.showNotification, index, true)
  85. if (this.duration) {
  86. this.onHideNotification(index)
  87. }
  88. })
  89. },
  90. open (notification) {
  91. this.mode = 'open'
  92. this.show(notification)
  93. },
  94. info (notification) {
  95. this.mode = 'info'
  96. this.show(notification)
  97. },
  98. success (notification) {
  99. this.mode = 'success'
  100. this.show(notification)
  101. },
  102. error (notification) {
  103. this.mode = 'error'
  104. this.show(notification)
  105. },
  106. warning (notification) {
  107. this.mode = 'warning'
  108. this.show(notification)
  109. },
  110. onHideNotification (index) {
  111. if (this.duration) {
  112. this.hideTimers[index] = setTimeout(() => {
  113. this.$set(this.showNotification, index, false)
  114. this.$emit('close')
  115. }, this.duration)
  116. } else {
  117. this.$set(this.showNotification, index, false)
  118. this.$emit('close')
  119. }
  120. }
  121. }
  122. }
  123. </script>
  124. <style lang="less" scoped>
  125. // 渐变过渡效果
  126. .fade-enter-active, .fade-leave-active {
  127. transition: opacity .3s;
  128. }
  129. .fade-enter, .fade-leave-to {
  130. opacity: 0;
  131. }
  132. // 滑动渐变过渡效果
  133. .slide-fade-enter-active, .slide-fade-leave-active {
  134. transition: all .5s ease;
  135. }
  136. .slide-fade-enter, .slide-fade-leave-to {
  137. transform: translateX(408px);
  138. -ms-transform: translateX(408px); /* IE 9 */
  139. -webkit-transform: translateX(408px); /* Safari and Chrome */
  140. opacity: 0;
  141. }
  142. .topRight {
  143. margin-right: 24px;
  144. right: 0;
  145. }
  146. .topLeft {
  147. margin-left: 24px;
  148. left: 0;
  149. }
  150. .bottomRight {
  151. margin-right: 24px;
  152. right: 0;
  153. }
  154. .bottomLeft {
  155. margin-left: 24px;
  156. left: 0;
  157. }
  158. .m-notification-wrap {
  159. position: fixed;
  160. z-index: 999; // 突出显示该层级
  161. width: 384px;
  162. color: rgba(0,0,0,.65);
  163. font-size: 14px;
  164. .m-notification {
  165. position: relative;
  166. margin-bottom: 16px;
  167. padding: 16px 24px;
  168. border-radius: 4px;
  169. box-shadow: 0 4px 12px rgba(0, 0, 0, 15%);
  170. line-height: 1.5;
  171. background: #fff;
  172. .m-notification-content {
  173. .u-status-svg {
  174. width: 24px;
  175. height: 24px;
  176. display: inline-block;
  177. position: absolute;
  178. margin-left: 4px;
  179. }
  180. .u-title {
  181. padding-right: 24px;
  182. display: inline-block;
  183. margin-bottom: 8px;
  184. color: rgba(0,0,0,.85);
  185. font-size: 16px;
  186. line-height: 24px;
  187. }
  188. .u-description {
  189. font-size: 14px;
  190. }
  191. .mb4 {
  192. margin-bottom: 4px;
  193. }
  194. .ml48 {
  195. margin-left: 48px;
  196. }
  197. }
  198. .u-close {
  199. display: inline-block;
  200. position: absolute;
  201. top: 16px;
  202. right: 22px;
  203. width: 14px;
  204. height: 14px;
  205. fill: rgba(0,0,0,.45);
  206. cursor: pointer;
  207. transition: fill .3s ease;
  208. &:hover {
  209. fill: rgba(0,0,0,.75);
  210. }
  211. }
  212. }
  213. }
  214. </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.warning(notification) // warning调用
  21. },
  22. onClose () { // 点击默认关闭按钮时触发的回调函数
  23. console.log('关闭notification')
  24. }

原文链接:https://blog.csdn.net/Dandrose/article/details/126830202




所属网站分类: 技术文章 > 博客

作者:我喜欢css

链接:http://www.qianduanheidong.com/blog/article/459382/caa906ec4768a4af619e/

来源:前端黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)