当前位置:首页 > JavaScript

js 实现popup

2026-02-02 04:23:56JavaScript

实现基本的 JavaScript Popup

使用 window.open() 方法创建最简单的弹窗:

const popup = window.open('', 'PopupWindow', 'width=300,height=200');
popup.document.write('<h1>Hello Popup!</h1>');

自定义弹窗样式

通过 DOM 操作创建带样式的弹窗:

const popup = document.createElement('div');
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.style.padding = '20px';
popup.style.backgroundColor = 'white';
popup.style.boxShadow = '0 0 10px rgba(0,0,0,0.3)';
popup.innerHTML = '<button id="closeBtn">X</button><p>Custom Popup Content</p>';
document.body.appendChild(popup);

document.getElementById('closeBtn').addEventListener('click', () => {
  document.body.removeChild(popup);
});

带遮罩层的弹窗

实现背景遮罩效果:

const overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.width = '100%';
overlay.style.height = '100%';
overlay.style.backgroundColor = 'rgba(0,0,0,0.5)';
overlay.style.zIndex = '1000';

const popup = document.createElement('div');
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.style.zIndex = '1001';
popup.innerHTML = '<div style="background:white;padding:20px;">Popup Content</div>';

document.body.appendChild(overlay);
document.body.appendChild(popup);

overlay.addEventListener('click', () => {
  document.body.removeChild(overlay);
  document.body.removeChild(popup);
});

使用 CSS 动画的弹窗

添加弹出动画效果:

.popup {
  animation: fadeIn 0.3s ease-out;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translate(-50%, -40%); }
  to { opacity: 1; transform: translate(-50%, -50%); }
}
const popup = document.createElement('div');
popup.className = 'popup';
popup.style.position = 'fixed';
popup.style.top = '50%';
popup.style.left = '50%';
popup.style.transform = 'translate(-50%, -50%)';
popup.innerHTML = 'Animated Popup';
document.body.appendChild(popup);

响应式弹窗

根据屏幕尺寸调整弹窗大小:

function createResponsivePopup() {
  const width = window.innerWidth > 768 ? '50%' : '90%';

  const popup = document.createElement('div');
  popup.style.width = width;
  popup.style.maxWidth = '600px';
  popup.style.margin = '0 auto';
  popup.innerHTML = 'Responsive Popup Content';

  document.body.appendChild(popup);
}

使用第三方库

使用 jQuery UI 实现弹窗:

$('<div>').dialog({
  title: 'jQuery UI Popup',
  width: 350,
  buttons: {
    OK: function() { $(this).dialog('close'); }
  }
});

使用 SweetAlert2 创建美观弹窗:

Swal.fire({
  title: 'SweetAlert2 Popup',
  text: 'This is a beautiful popup',
  icon: 'success',
  confirmButtonText: 'OK'
});

js 实现popup

标签: jspopup
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,用于简化 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的设计宗旨是“Write Less, Do Mor…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现图片预览

js实现图片预览

使用FileReader API实现图片预览 通过FileReader对象读取用户选择的图片文件并显示预览: const input = document.getElementById('imag…

js实现验证

js实现验证

验证表单输入 使用JavaScript验证表单输入是常见的需求。可以通过监听表单提交事件,检查输入字段是否符合要求。 document.getElementById('myForm').addEve…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…