-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.js
More file actions
158 lines (138 loc) · 4.63 KB
/
Copy pathmessage.js
File metadata and controls
158 lines (138 loc) · 4.63 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// message.js - 独立的消息提示组件
class SimpleMessage {
constructor() {
this.container = null;
this.init();
}
init() {
// 创建消息容器
this.container = document.createElement('div');
this.container.className = 'simple-message-container';
this.container.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
display: flex;
flex-direction: column;
align-items: flex-end;
`;
document.body.appendChild(this.container);
}
show(options) {
const {message, type = 'info', duration = 3000} = options;
// 创建消息元素
const messageEl = document.createElement('div');
messageEl.className = `simple-message simple-message-${type}`;
messageEl.style.cssText = `
min-width: 200px;
max-width: 400px;
padding: 12px 16px;
margin-bottom: 10px;
border-radius: 8px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
background-color: #fff;
border: 1px solid;
display: flex;
align-items: center;
animation: fadeIn 0.3s;
transition: all 0.3s;
`;
// 设置不同消息类型的样式
const typeStyles = {
success: {
borderColor: '#f0f9eb', backgroundColor: '#f0f9eb', color: '#67c23a'
}, error: {
borderColor: '#fef0f0', backgroundColor: '#fef0f0', color: '#f56c6c'
}, warning: {
borderColor: '#fdf6ec', backgroundColor: '#fdf6ec', color: '#e6a23c'
}, info: {
borderColor: '#f4f4f5', backgroundColor: '#f4f4f5', color: '#909399'
}
};
Object.assign(messageEl.style, typeStyles[type] || typeStyles.info);
// 添加图标
const iconMap = {
success: '✓', error: '✗', warning: '!', info: 'i'
};
const iconEl = document.createElement('span');
iconEl.textContent = iconMap[type] || 'i';
iconEl.style.cssText = `
margin-right: 10px;
font-weight: bold;
font-size: 16px;
`;
// 添加消息文本
const textEl = document.createElement('span');
textEl.textContent = message;
textEl.style.cssText = `
flex: 1;
font-size: 14px;
line-height: 1.5;
`;
// 添加关闭按钮
const closeEl = document.createElement('span');
closeEl.textContent = '×';
closeEl.style.cssText = `
margin-left: 10px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
`;
closeEl.onclick = () => this.closeMessage(messageEl);
messageEl.appendChild(iconEl);
messageEl.appendChild(textEl);
messageEl.appendChild(closeEl);
this.container.appendChild(messageEl);
// 自动关闭
if (duration > 0) {
setTimeout(() => this.closeMessage(messageEl), duration);
}
return messageEl;
}
closeMessage(messageEl) {
messageEl.style.opacity = '0';
messageEl.style.transform = 'translateX(100%)';
setTimeout(() => {
if (messageEl.parentNode) {
messageEl.parentNode.removeChild(messageEl);
}
}, 300);
}
success(message, duration = 3000) {
return this.show({message, type: 'success', duration});
}
error(message, duration = 3000) {
return this.show({message, type: 'error', duration});
}
warning(message, duration = 3000) {
return this.show({message, type: 'warning', duration});
}
info(message, duration = 3000) {
return this.show({message, type: 'info', duration});
}
}
// 全局消息实例
window.SimpleMessage = new SimpleMessage();
// 添加到全局对象
window.Message = {
success: (message, duration) => window.SimpleMessage.success(message, duration),
error: (message, duration) => window.SimpleMessage.error(message, duration),
warning: (message, duration) => window.SimpleMessage.warning(message, duration),
info: (message, duration) => window.SimpleMessage.info(message, duration)
};
// 添加动画样式
const style = document.createElement('style');
style.textContent = `
@keyframes fadeIn {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
`;
document.head.appendChild(style);