实现消息提示组件

实现消息提示组件

在浏览器页面中,通用的消息提示组件一般可以分为静态局部提示和动态全局提示,用于反馈用户需要关注的信息,使用频率较高。

实现

实现消息提示组件,动态全局提示,主要使用原生JavaScript实现,实现的代码基本都作了注释。

<!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; // 避免XSS 
            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); // 加入body
            setTimeout(() => { // 定时器
                msgList.shift(unit); // 出队
                computedTop(); // 重新计算高度
                const finish = () => {
                    body.removeChild(unit); // 移出body
                    unit.removeEventListener("transitionend", finish); // 移除监听器
                }
                unit.addEventListener("transitionend", finish); // 添加监听器
                unit.classList.add("msg-hide"); // 触发移除过渡动画
            }, 3000); // 3秒延时触发
        }
        win.msg = (msg, type = "info") => show(msg, type); // 加入window对象
    })(window, document);
</script>
</html>

每日一题

https://github.com/WindrunnerMax/EveryDay

参考

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/
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
组件名称:ymPrompt消息提示组件 2.0 演示地址:http://bbs.okajax.com/demo/31/ =============================================================================== 组件说明:在web开发中,对于浏览器默认的消息提示框(如alert,confirm等)外观无法控制,为了实现更好的界面效果,于是模拟系统的消息提示实现了该组件。在外观上可以通过css进行完全的控制。 =============================================================================== 组件功能介绍: 1、调用方式简单,直接使用ymPrompt.alert()的方式调用,传入相应的参数即可。 2、兼容IE6.0+、FF1.5+、Opear9+(在Opear下显示有一个小缺陷),兼容HTML4/XHTML1.0页面渲染模式。 3、完全的CSS皮肤定制功能,所有外观包括显示图标/关闭按钮等都可以通过css设置。目前提供了五种皮肤,Vista、QQ、dmm-green、ExtBlue和BlackColl。有兴趣可以修改样式表文件定义新的皮肤。 4、提供四种消息类型。分别为消息提示、成功信息、错误信息、询问信息。 5、弹出消息框时屏蔽页面其它元素的操作,自动隐藏页面select及iframe下select(无限级select隐藏),并遮罩iframe等元素。 6、页面select控件状态还原时保持原状态,即如果原来select就是隐藏的,则消息框弹出并关闭后该select仍保持隐藏状态,而只显示原本显示的select。 7、消息组件弹出时屏蔽除Tab和回车外的几乎所有键盘操作(包括F5),“消息确认框”支持“tab键/左右方向键”切换确定/取消焦点。 8、可以页面加载的任意时间调用,如页面初始化时调用,页面加载完成后调用。 9、支持通过JSON和传统的参数传入方式。如ymprompt.alert('消息内容',300,200,'消息标题',handler)和ymprompt.alert({message:'消息内容',width:300,height:200,title:'消息标题',handler:handler}),这两种写法是等价的。 10、只需要一个回调函数,可捕获确定、取消及关键按钮的点击事件。函数改变了1.0版需要对确定和取消按钮分别写回调函数及不支持关闭事件的缺陷, 11、完全的封装,对外只暴露一个ymPrompt变量,有效防止与使用者程序变量的冲突。 12、程序(js)与样式(css)的完全分离,取消了1.0中自动加载css的功能,用户可根据需要加载对应css样式表单,降低了对样式配置的复杂度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mFcoder187974305

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值