// 警告框 alert("这是一个简单的弹窗");
// 确认框 let result = confirm("你确定要执行此操作吗?");
// 输入框 let username = prompt("请输入您的用户名");
alert("操作成功!");
.modal { opacity: 0; transition: opacity 0.3s ease-in-out; }
.modal.show { opacity: 1; }
document.addEventListener('click', function(event) { if (event.target === modalOverlay) {
closeModal();
} });
.modal { position: fixed; z-index: 1000; }
/ 问题可能出在这里 / .header { position: relative; z-index: 1001; / 比弹窗还高 / }
class DraggableModal { constructor(modal) {
this.modal = modal;
this.isDragging = false;
this.offset = { x: 0, y: 0 };
this.init();
}
init() {
const header = this.modal.querySelector('.modal-header');
header.style.cursor = 'move';
header.addEventListener('mousedown', this.startDrag.bind(this));
document.addEventListener('mousemove', this.drag.bind(this));
document.addEventListener('mouseup', this.stopDrag.bind(this));
}
startDrag(e) {
this.isDragging = true;
const rect = this.modal.getBoundingClientRect();
this.offset.x = e.clientX - rect.left;
this.offset.y = e.clientY - rect.top;
this.modal.style.transition = 'none'; // 拖拽时禁用动画
}
drag(e) {
if (!this.isDragging) return;
const x = e.clientX - this.offset.x;
const y = e.clientY - this.offset.y;
this.modal.style.left = `${x}px`;
this.modal.style.top = `${y}px`;
}
stopDrag() {
this.isDragging = false;
this.modal.style.transition = ''; // 恢复动画
} }