wtDom代码请查看下面的链接:
https://www.jianshu.com/p/cb6b6fa1770a
import {wtDom} from '../common/utils';
/*
* @params options {Object} 必须
*/
function Alert (params) {
var defaults = {
title: '',
content: '',
btns: [],
zIndex: 100,
ios: false // 是否是ios风格
};
var options = Object.assign(defaults, params);
var Alert = wtDom.create('div');
Alert.className = 'wt-Alert';
if (options.ios) {
Alert.className = 'wt-Alert wt-Alert-ios';
}
// 如果title不为空
if (options.title !== '') {
var title = wtDom.create();
title.className = 'title';
title.innerText = options.title;
Alert.appendChild(title);
}
// 显示内容
var content = wtDom.create();
content.className = 'content';
content.innerText = options.content;
Alert.appendChild(content);
// 显示btn
var btns = wtDom.create();
btns.className = 'wt-Alert-btns';
options.btns.forEach((item, index) => {
var btn = wtDom.create();
btn.className = 'wt-Alert-btn';
btn.innerText = item.text;
btn.addEventListener('click', function () {
wtDom.del(mask);
wtDom.del(Alert);
item.handle(index);
});
btns.appendChild(btn);
});
Alert.appendChild(btns);
Alert.style.zIndex = options.zIndex + 1;
// 添加Alert
document.body.appendChild(Alert);
// 遮罩层
var mask = wtDom.create();
mask.className = 'wt-alert-mask';
mask.style.zIndex = options.zIndex;
mask.addEventListener('click', function () {
if (options.btns.length === 0 ) {
wtDom.del(mask);
wtDom.del(Alert);
}
});
// 添加遮罩层
document.body.appendChild(mask);
}
export default Alert;
css代码:
/* alert*/
.wt-alert-mask {
position: fixed;
background: rgba(0,0,0,0.3);
width: 100%;
height: 100%;
top: 0;
left: 0;
}
.wt-Alert {
width: 80%;
background: #fff;
z-index: 103;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 20px 0px #9b9b9b;
box-sizing: border-box;
padding: 0.5rem;
.title {
font-size: 0.8rem;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.content {
font-size: 0.7rem;
color: #666;
margin-top: 1rem;
}
.wt-Alert-btns {
display: flex;
color: #1BB5F1;
font-size: 0.7rem;
justify-content: flex-end;
margin-top: 1rem;
.wt-Alert-btn{
padding: 0.3rem 0.5rem;
&:active {
background: #eee;
}
}
}
&.wt-Alert-ios {
border-radius: 0.5rem;
padding: 0;
overflow: hidden;
.title{
margin: 1rem;
margin-bottom: 0;
}
.content {
margin-left: 1rem;
margin-right: 1rem;
margin-top: 1rem;
}
.wt-Alert-btns {
position: relative;
&:after {
height: 1px;
content: '';
border-top: 1px solid #ccc;
transform: scaleY(.5);
display: block;
position: absolute;
top: 0;
width: 100%;
}
.wt-Alert-btn {
flex: 1;
text-align: center;
line-height: 2.2rem;
height: 2.2rem;
box-sizing: border-box;
position: relative;
align-items: center;
display: flex;
justify-content: center;
font-size: 0.8rem;
&:last-child {
&:after {
display: none;
}
}
&:after {
height: 100%;
content: '';
border-right: 1px solid #ccc;
transform: scaleX(0.5);
display: block;
position: absolute;
top: 1px;
right: 0;
width: 1px;
}
}
}
}
}
网友评论