html5侧边提示框

javascript 复制代码
// 显示通知消息
function showNotification(message, type = 'info') {
	// 创建通知元素
	const notification = document.createElement('div');
	notification.className = `notification notification-${type}`;
	notification.innerHTML = `
        <i class="fas fa-${getNotificationIcon(type)}"></i>
        <span>${message}</span>
    `;

	// 添加样式
	notification.style.cssText = `
        position: fixed;
        top: 90px;
        right: 20px;
        background: ${getNotificationColor(type)};
        color: white;
        padding: 12px 16px;
        border-radius: 6px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.3);
        z-index: 3000;
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 14px;
        max-width: 300px;
        animation: slideInRight 0.3s ease;
        transform: translateX(100%);
    `;

	document.body.appendChild(notification);

	// 显示动画
	setTimeout(() => {
		notification.style.transform = 'translateX(0)';
	}, 10);

	// 自动移除
	setTimeout(() => {
		notification.style.transform = 'translateX(100%)';
		setTimeout(() => {
			if (notification.parentNode) {
				notification.parentNode.removeChild(notification);
			}
		}, 300);
	}, 3000);
}

// 获取通知图标
function getNotificationIcon(type) {
	const icons = {
		success: 'check-circle',
		error: 'exclamation-circle',
		warning: 'exclamation-triangle',
		info: 'info-circle'
	};
	return icons[type] || 'info-circle';
}

// 获取通知颜色
function getNotificationColor(type) {
	const colors = {
		success: '#4CAF50',
		error: '#F44336',
		warning: '#FF9800',
		info: '#2196F3'
	};
	return colors[type] || '#2196F3';
}

以上是提示框的核心代码

提示框的使用如下:

javascript 复制代码
showNotification("数据提交成功!", "success");

showNotification(`数据提交失败!`, "error");

showNotification(`数据提交错误!`, "warning");

以下是警告提示框的效果:

相关推荐
一只小风华~39 分钟前
Web前端:JavaScript和CSS实现的基础登录验证功能
前端
90后的晨仔39 分钟前
Vue Router 入门指南:从零开始实现前端路由管理
前端·vue.js
LotteChar1 小时前
WebStorm vs VSCode:前端圈的「豆腐脑甜咸之争」
前端·vscode·webstorm
90后的晨仔1 小时前
零基础快速搭建 Vue 3 开发环境(附官方推荐方法)
前端·vue.js
洛_尘1 小时前
Java EE进阶2:前端 HTML+CSS+JavaScript
java·前端·java-ee
孤独的根号_1 小时前
Vite背后的技术原理🚀:为什么选择Vite作为你的前端构建工具💥
前端·vue.js·vite
吹牛不交税2 小时前
Axure RP Extension for Chrome插件安装使用
前端·chrome·axure
薛定谔的算法2 小时前
# 前端路由进化史:从白屏到丝滑体验的技术突围
前端·react.js·前端框架
拾光拾趣录3 小时前
Element Plus表格表头动态刷新难题:零闪动更新方案
前端·vue.js·element
Adolf_19933 小时前
React 中 props 的最常用用法精选+useContext
前端·javascript·react.js