打开模态窗口
当前位置:首页 > JavaScript

js 实现 模态窗口

2026-02-02 17:00:55JavaScript

使用原生JavaScript实现模态窗口

创建HTML结构,包含触发按钮和模态框内容

<button id="modalBtn">打开模态窗口</button>
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>这里是模态窗口内容</p>
  </div>
</div>

添加基础CSS样式

.modal {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.4);
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}

实现JavaScript逻辑

const modal = document.getElementById("myModal");
const btn = document.getElementById("modalBtn");
const span = document.getElementsByClassName("close")[0];

btn.onclick = function() {
  modal.style.display = "block";
}

span.onclick = function() {
  modal.style.display = "none";
}

window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

使用jQuery简化实现

HTML结构同上,引入jQuery库后

$(document).ready(function(){
  $("#modalBtn").click(function(){
    $("#myModal").fadeIn();
  });

  $(".close, .modal").click(function(){
    $("#myModal").fadeOut();
  });

  $(".modal-content").click(function(e){
    e.stopPropagation();
  });
});

动态内容模态窗口实现

创建可复用的模态窗口函数

function showModal(title, content) {
  const modalHTML = `
    <div id="dynamicModal" class="modal">
      <div class="modal-content">
        <span class="close">&times;</span>
        <h3>${title}</h3>
        <div>${content}</div>
      </div>
    </div>
  `;

  document.body.insertAdjacentHTML('beforeend', modalHTML);

  const modal = document.getElementById("dynamicModal");
  const span = document.querySelector("#dynamicModal .close");

  span.onclick = function() {
    modal.remove();
  }

  window.onclick = function(event) {
    if (event.target == modal) {
      modal.remove();
    }
  }

  modal.style.display = "block";
}

// 调用示例
showModal("提示", "这是一条重要消息");

添加动画效果

在CSS中添加过渡动画

.modal-content {
  animation: modalopen 0.5s;
}

@keyframes modalopen {
  from {opacity: 0; transform: translateY(-50px)}
  to {opacity: 1; transform: translateY(0)}
}

可访问性改进

添加ARIA属性增强可访问性

<div id="myModal" class="modal" role="dialog" aria-labelledby="modalTitle" aria-hidden="true">
  <div class="modal-content">
    <h3 id="modalTitle">模态窗口标题</h3>
    <span class="close" aria-label="关闭">&times;</span>
    <p>这里是模态窗口内容</p>
  </div>
</div>

对应JavaScript更新

js 实现 模态窗口

btn.onclick = function() {
  modal.style.display = "block";
  modal.setAttribute('aria-hidden', 'false');
}

span.onclick = function() {
  modal.style.display = "none";
  modal.setAttribute('aria-hidden', 'true');
}

标签: 窗口模态
分享给朋友:

相关文章

react模态框封装之后如何引用

react模态框封装之后如何引用

封装模态框的基本结构 封装一个React模态框通常需要创建一个可复用的组件,包含状态管理、样式和交互逻辑。以下是一个基础封装示例: import React, { useState }…

react实现窗口拖拽

react实现窗口拖拽

实现窗口拖拽的基本思路 在React中实现窗口拖拽功能,核心是通过鼠标事件监听和元素位置更新。需要处理mousedown、mousemove和mouseup事件,计算鼠标移动距离并更新元素位置。 使…

js实现聊天窗口

js实现聊天窗口

实现聊天窗口的基本结构 使用HTML和CSS构建聊天窗口的界面框架。HTML部分需要包含消息显示区域和输入框,CSS用于美化界面。 <div class="chat-container">…

jquery弹出div窗口

jquery弹出div窗口

jQuery 弹出 Div 窗口的方法 使用 show() 和 hide() 方法 通过 jQuery 的 show() 和 hide() 方法可以控制 div 的显示与隐藏。默认情况下,div 设置…

java如何关闭当前窗口关闭

java如何关闭当前窗口关闭

关闭当前窗口的方法 在Java中关闭当前窗口取决于使用的GUI框架。以下是常见框架的实现方法: Swing应用程序 对于Swing应用程序,可以使用JFrame或JDialog的dispose()方…

uniapp底部窗口

uniapp底部窗口

uniapp底部窗口实现方法 在uniapp中实现底部窗口通常涉及自定义组件或使用官方提供的<uni-popup>组件。以下是几种常见方法: 使用uni-popup组件 安装uni-po…