实现消息提示组件
在浏览器页面中,通用的消息提示组件一般可以分为静态局部提示和动态全局提示,用于反馈用户需要关注的信息,使用频率较高。
实现
实现消息提示组件,动态全局提示,主要使用原生JavaScript
实现,实现的代码基本都作了注释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| <!DOCTYPE html> <html> <head> <title>Message</title> <style type="text/css"> @keyframes enter { 0% { transform: translateY(-50px); opacity: 0; } 100% { transform: translateY(0); opacity: 1; } } .msg-unit{ font-size: 13px; position: fixed; left: calc(50% - 150px); width: 300px; padding: 10px; border-radius: 3px; animation: enter 0.3s; transition: all .5s; display: flex; align-items: center; } .msg-hide{ opacity: 0; transform: translateY(-50px); } .msg-unit > .msg-icon{ font-size: 15px; margin: 0 7px; } </style> <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css"> </head> <body> <button onclick="msg('这是一条info', 'info')">Info</button> <button onclick="msg('这是一条success', 'success')">Success</button> <button onclick="msg('这是一条warning', 'warning')">Warning</button> <button onclick="msg('这是一条error', 'error')">Error</button> </body> <script type="text/javascript"> (function(win, doc){ const body = doc.body; const msgList = []; const baseTop = 15; const multiplyTop = 46; const msgTypeMap = { "info": {background: "#EBEEF5", border: "#EBEEF5", color: "#909399", icon: "fa fa-info-circle"}, "success": {background: "#f0f9eb", border: "#e1f3d8", color: "#67C23A", icon: " fa fa-check-circle-o"}, "warning": {background: "#fdf6ec", border: "#faecd8", color: "#E6A23C", icon: " fa fa-exclamation-circle"}, "error": {background: "#fef0f0", border: "#fde2e2", color: "#F56C6C", icon: "fa fa-times-circle-o"}, } const create = (msg, type) => { const unit = doc.createElement("div"); unit.className = "msg-unit"; const typeInfo = msgTypeMap[type] === void 0 ? msgTypeMap["info"] : msgTypeMap[type]; unit.style.background = typeInfo.background; unit.style.color = typeInfo.color; unit.style["border"] = `1px solid ${typeInfo.border}`; const i = doc.createElement("i"); i.className = `${typeInfo.icon} msg-icon`; const span = doc.createElement("span"); span.innerText = msg; unit.appendChild(i); unit.appendChild(span); unit.style.top = msgList.length * multiplyTop + baseTop + "px"; return unit; } const computedTop = () => msgList.forEach((v, index) => v.style.top = index * multiplyTop + baseTop + "px"); const show = (msg, type) => { const unit = create(msg, type); msgList.push(unit); body.appendChild(unit); setTimeout(() => { msgList.shift(unit); computedTop(); const finish = () => { body.removeChild(unit); unit.removeEventListener("transitionend", finish); } unit.addEventListener("transitionend", finish); unit.classList.add("msg-hide"); }, 3000); } win.msg = (msg, type = "info") => show(msg, type); })(window, document); </script> </html>
|
参考
1 2 3
| https://element.eleme.cn/#/zh-CN/component/message http://kmanong.top/kmn/qxw/form/article?id=62470&cate=52 https://jancat.github.io/post/2020/design-a-pair-of-general-message-prompt-components-alert-and-toast/
|