55 * @param {String} message - 消息内容
66 * @param {String} title - 可选的标题
77 * @param {Boolean} dismissible - 是否可关闭,默认false
8+ * @param {Number} autoClose - 自动关闭时间(毫秒),0表示不自动关闭,默认0
89 * @param {String} className - 额外的CSS类,可选
910 */
1011
1112const alertType = typeof type !== ' undefined' ? type : ' info' ;
1213const alertMessage = typeof message !== ' undefined' ? message : ' ' ;
1314const alertTitle = typeof title !== ' undefined' ? title : ' ' ;
1415const isDismissible = typeof dismissible !== ' undefined' ? dismissible : false ;
16+ const autoCloseTime = typeof autoClose !== ' undefined' ? parseInt (autoClose) : 0 ;
1517const extraClass = typeof className !== ' undefined' ? className : ' ' ;
1618
17- // 根据类型设置样式
19+ const alertId = ' alert-' + Math .random ().toString (36 ).substr (2 , 9 );
20+
1821let alertClass = ' ' ;
1922let iconClass = ' ' ;
2023
@@ -40,20 +43,67 @@ switch(alertType) {
4043%>
4144
4245<% if (alertMessage) { % >
43- < div class = " border rounded-lg p-2 my-2 <%= alertClass %> <%= extraClass %>" < % if (isDismissible) { % > id= " alert-<%= Math.random().toString(36).substr(2, 9) %>" < % } % >>
46+ < div class = " border rounded-lg p-2 my-2 <%= alertClass %> <%= extraClass %> transition-all duration-300"
47+ id= " <%= alertId %>"
48+ < % if (autoCloseTime > 0 ) { % > data- auto- close= " <%= autoCloseTime %>" < % } % >>
4449 < div class = " flex" >
4550 < i class = " <%= iconClass %> mr-3 mt-1" >< / i>
4651 < div class = " flex-1" >
4752 < % if (alertTitle) { % >
4853 < div class = " font-medium mb-1" >< %= alertTitle % >< / div>
4954 < % } % >
5055 < div class = " text-sm" >< %= alertMessage % >< / div>
56+
57+ < % if (autoCloseTime > 0 ) { % >
58+ < div class = " mt-2 h-1 bg-gray-200 rounded-full overflow-hidden" >
59+ < div class = " h-full bg-current opacity-30 rounded-full transition-all ease-linear"
60+ id= " <%= alertId %>-progress"
61+ style= " width: 100%; transition-duration: <%= autoCloseTime %>ms; transform-origin: left;" >< / div>
62+ < / div>
63+ < % } % >
5164 < / div>
52- < % if (isDismissible) { % >
53- < button type= " button" class = " ml-3 text-gray-400 hover:text-gray-600" onclick= " this.parentElement.parentElement.style.display='none'" >
65+
66+ < % if (isDismissible || autoCloseTime > 0 ) { % >
67+ < button type= " button"
68+ class = " ml-3 text-gray-400 hover:text-gray-600 transition-colors duration-200"
69+ onclick= " dismissAlert('<%= alertId %>')" >
5470 < i class = " fas fa-times" >< / i>
5571 < / button>
5672 < % } % >
5773 < / div>
5874 < / div>
75+
76+ < script>
77+ (function initializeAlert () {
78+ const alertElement = document .getElementById (' <%= alertId %>' );
79+ const autoCloseTime = parseInt (alertElement .dataset .autoClose ) || 0 ;
80+
81+ if (autoCloseTime > 0 ) {
82+ const progressBar = document .getElementById (' <%= alertId %>-progress' );
83+
84+ if (progressBar) {
85+ progressBar .offsetWidth ;
86+ progressBar .style .width = ' 0%' ;
87+ }
88+
89+ setTimeout (() => {
90+ dismissAlert (' <%= alertId %>' );
91+ }, autoCloseTime);
92+ }
93+ })();
94+
95+ function dismissAlert (alertId ) {
96+ const alertElement = document .getElementById (alertId);
97+ if (! alertElement) return ;
98+
99+ alertElement .style .opacity = ' 0' ;
100+ alertElement .style .transform = ' translateY(-10px)' ;
101+
102+ setTimeout (() => {
103+ if (alertElement .parentNode ) {
104+ alertElement .parentNode .removeChild (alertElement);
105+ }
106+ }, 300 );
107+ }
108+ < / script>
59109< % } %>
0 commit comments