JS部分
// 初始化一个弹出框
// 生成dom元素
// 插到页面中
// 显示
(function(window, document) {
let Msg = function(options) {
this._init(options);
}
Msg.prototype._init = function({ content = '' }) {
this.content = content;
this._createElement();
this._show(this._el);
}
Msg.prototype._createElement = function() {
let wrap = document.createElement('div');
wrap.className = 'msg__wrap';
wrap.innerHTML = '\
<div class="msg-header">\
<span>确认删除</span>\
<span class="msg-header-close-button">×</span>\
</div>\
<div class="msg-body">\
<div class="msg-body-icon">\
<div class="msg-body-icon-info"></div>\
</div>\
<div class="msg-body-content"></div>\
</div>\
<div class="msg-footer">\
<button class="msg-footer-btn msg-footer-cancel-button">算了吧</button>\
<button class="msg-footer-btn msg-footer-confirm-button">好的</button>\
</div>';
this._el = wrap;
console.log("TCL: Msg.prototype._createElement -> this._el", this._el)
}
Msg.prototype._show = function(el) {
document.body.append(el);
setTimeout(function() {
el.style.transform = 'translate(-50%, -50%) scale(1, 1)';
});
}
window.$Msg = Msg;
})(window, document);
不断通过原型往上添加函数,注意_init()的参数是html里穿的
HTML部分
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./msg.css">
<style>
#pop {
border: 0 none;
color: #fff;
outline: none;
padding: 10px 20px;
font-size: 18px;
background: #2594f0;
cursor: pointer;
}
#pop:active {
background: blue;
}
</style>
</head>
<body>
<button id="pop">弹个框</button>
<!--
<div class="msg__wrap">
<div class="msg-header">
<span>确认删除</span>
<span class="msg-header-close-button">×</span>
</div>
<div class="msg-body">
<div class="msg-body-icon">
<div class="msg-body-icon-wrong"></div>
</div>
<div class="msg-body-content">是否删除</div>
</div>
<div class="msg-footer">
<button class="msg-footer-btn msg-footer-cancel-button">算了吧</button>
<button class="msg-footer-btn msg-footer-confirm-button">好的</button>
</div>
</div>
-->
<script src="./msg.js"></script>
<script type="text/javascript">
new $Msg({
content: '贤者模式马上就过去了。。真的要清空吗?<span style="color: red;">~</span>',
});
</script>
</body>
只看最后就行,通过new方法找到暴露出的$msg,不用参数也行,因为前面的解构赋值代表着如果时underfine就是空
来源:CSDN
作者:锋芒神话霜
链接:https://blog.csdn.net/qq_34471916/article/details/104156976