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

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

相关推荐
willingtolove9 分钟前
使用chrome修改请求参数重新发送请求
前端·chrome
-曾牛17 分钟前
CSRF跨站请求伪造:原理、利用与防御全解析
前端·网络·web安全·网络安全·渗透测试·csrf·原理解析
卓码软件测评31 分钟前
第三方软件检测机构:【利用测试工具Postman测试沙箱:在Tests标签中编写健壮的质量检查逻辑测试脚本】
javascript·node.js·postman
魂祈梦43 分钟前
前端下载多个文件/浏览器批量下载文件
前端·浏览器
谎言西西里1 小时前
彻底搞懂 JavaScript 的 this:从陷阱到解决方案
javascript
小明记账簿_微信小程序1 小时前
手写一个webpack插件(plugin)
前端
我命由我123451 小时前
微信小程序 - scroll-view 的一些要点(scroll-view 需要设置滚动方向、scroll-view 需要设置高度)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
BD_Marathon1 小时前
【JavaWeb】CSS浮动
前端·css
1024肥宅1 小时前
手写 Promise:深入理解 JavaScript 异步编程的核心
前端·javascript·promise
铅笔侠_小龙虾1 小时前
Vue 学习目录
前端·vue.js·学习