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");

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

相关推荐
matlab_xiaowang11 分钟前
Redux 入门:JavaScript 可预测状态管理库
开发语言·javascript·其他·ecmascript
幼儿园技术家20 分钟前
前端如何设计权限系统(RBAC / ABAC)?
前端
前端摸鱼匠2 小时前
Vue 3 的v-bind合并行为:讲解v-bind与普通属性合并的规则
前端·javascript·vue.js·前端框架·ecmascript
REDcker2 小时前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
W.A委员会3 小时前
CSS中的单位
css·css3·html5
donecoding4 小时前
一个 sudo 引发的血案:npm 全局包权限错乱彻底修复
前端·node.js·前端工程化
风骏时光牛马4 小时前
Raku正则匹配与数据批量处理实操案例
前端
nbwenren4 小时前
2026实测:Gemini 3 镜像站视觉能力实践——拍照原型图,一键生成 HTML+CSS 代码
前端·css·html
Lee川4 小时前
Prisma 实战指南:像搭积木一样设计古诗词数据库
前端·数据库·后端
Linsk4 小时前
Java和JavaScript的关系真是雷峰和雷峰塔的关系吗?
java·javascript·oracle