使用示例

  1. message.warning({
  2. notice: '服务器连接异常',
  3. reload: true,
  4. duration: 0,
  5. onClick:() => {
  6. alert(2)
  7. },
  8. })

js 文件 index.js

  1. import './style.css'
  2. /**
  3. * 加载中
  4. * @param {*} props
  5. */
  6. function loading(props) {
  7. initMsg({
  8. ...props,
  9. icon: '<svg viewBox="0 0 1024 1024" focusable="false" class="anticon-spin" data-icon="loading" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 0 0-94.3-139.9 437.71 437.71 0 0 0-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"></path></svg>'
  10. })
  11. }
  12. /**
  13. * 成功
  14. * @param {*} props
  15. */
  16. function success(props) {
  17. initMsg({
  18. ...props,
  19. icon: '<svg viewBox="64 64 896 896" focusable="false" class="" data-icon="check-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm193.5 301.7l-210.6 292a31.8 31.8 0 0 1-51.7 0L318.5 484.9c-3.8-5.3 0-12.7 6.5-12.7h46.9c10.2 0 19.9 4.9 25.9 13.3l71.2 98.8 157.2-218c6-8.3 15.6-13.3 25.9-13.3H699c6.5 0 10.3 7.4 6.5 12.7z"></path></svg>'
  20. })
  21. }
  22. /**
  23. * 提示
  24. * @param {*} props
  25. */
  26. function info(props) {
  27. initMsg({
  28. ...props,
  29. icon: '<svg viewBox="64 64 896 896" focusable="false" class="" data-icon="info-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm32 664c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V456c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272zm-32-344a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"></path></svg>'
  30. })
  31. }
  32. /**
  33. * 警告
  34. * @param {*} props
  35. */
  36. function warning(props) {
  37. initMsg({
  38. ...props,
  39. icon: '<svg viewBox="64 64 896 896" focusable="false" class="" data-icon="exclamation-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm-32 232c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V296zm32 440a48.01 48.01 0 0 1 0-96 48.01 48.01 0 0 1 0 96z"></path></svg>'
  40. })
  41. }
  42. /**
  43. * 错误
  44. * @param {*} props
  45. */
  46. function error(props) {
  47. initMsg({
  48. ...props,
  49. icon: '<svg viewBox="64 64 896 896" focusable="false" class="" data-icon="close-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm165.4 618.2l-66-.3L512 563.4l-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 359a8.32 8.32 0 0 1-1.9-5.2c0-4.4 3.6-8 8-8l66.1.3L512 464.6l99.3-118.4 66-.3c4.4 0 8 3.5 8 8 0 1.9-.7 3.7-1.9 5.2L553.5 514l130 155c1.2 1.5 1.9 3.3 1.9 5.2 0 4.4-3.6 8-8 8z"></path></svg>'
  50. })
  51. }
  52. /**
  53. * DOM结构组装
  54. * @param {*} props
  55. */
  56. function initMsg(props) {
  57. // 自动关闭定时参数, 不设置默认三秒
  58. const defaultDuration = props.hasOwnProperty('duration') ? parseInt(props.duration) : 3000
  59. // 如果存在消息弹窗则先删除
  60. closeMsg()
  61. // 创建msg内容
  62. const msgContent = document.createElement('div')
  63. msgContent.className = 'msg_content'
  64. msgContent.innerHTML = props.icon // 添加图标
  65. msgContent.innerHTML += `<span class='notice'>${props.notice}<span>`
  66. // 添加短线重连按钮
  67. const msgBtn = document.createElement('span')
  68. msgBtn.className = 'reload_btn'
  69. msgBtn.innerHTML = '重连'
  70. msgBtn.onclick = props.onClick
  71. props.reload && msgContent.appendChild(msgBtn)
  72. // 创建外部容器
  73. const msgBox = document.createElement('div')
  74. msgBox.className = 'sock_msg_box'
  75. msgBox.id = 'sockMsg'
  76. msgBox.appendChild(msgContent)
  77. // 添加 msg 内容到页面
  78. document.body.appendChild(msgBox)
  79. // 存在定时参数且不为 0 则定时关闭
  80. defaultDuration && setInterval(closeMsg,defaultDuration)
  81. }
  82. /**
  83. * 删除消息提示
  84. */
  85. function closeMsg() {
  86. const meaageDom = document.getElementById('sockMsg')
  87. meaageDom && document.body.removeChild(meaageDom)
  88. }
  89. export default {
  90. loading,
  91. success,
  92. info,
  93. warning,
  94. error
  95. }

样式文件 style.css

  1. .sock_msg_box {
  2. min-width: 160px;
  3. padding: 10px;
  4. position: fixed;
  5. top: 20px;
  6. left: 0;
  7. right: 0;
  8. margin: 0 auto;
  9. text-align: center;
  10. }
  11. .sock_msg_box .msg_content {
  12. display: inline-flex;
  13. align-items: center;
  14. padding: 10px 16px;
  15. background: #fff;
  16. border-radius: 4px;
  17. -webkit-box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  18. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  19. pointer-events: all;
  20. }
  21. .sock_msg_box .msg_content .anticon-spin {
  22. -webkit-animation: loadingCircle 1s infinite linear;
  23. animation: loadingCircle 1s infinite linear;
  24. color: #1890ff;
  25. }
  26. .sock_msg_box .msg_content [data-icon=check-circle] {
  27. color: #52c41a;
  28. }
  29. .sock_msg_box .msg_content [data-icon=info-circle] {
  30. color: #1890ff;
  31. }
  32. .sock_msg_box .msg_content [data-icon=exclamation-circle] {
  33. color: #faad14;
  34. }
  35. .sock_msg_box .msg_content [data-icon=close-circle] {
  36. color: #f5222d;
  37. }
  38. .sock_msg_box .msg_content .notice {
  39. margin-left: 10px;
  40. }
  41. .sock_msg_box .msg_content .reload_btn {
  42. color: #f5222d;
  43. margin-left: 10px;
  44. cursor: pointer;
  45. }
  46. .sock_msg_box .msg_content .reload_btn:hover {
  47. position:relative;
  48. font-size: 12px;
  49. padding: 0.5px;
  50. }