js模态对话框使用方法详解
作者:Sunshine_Lisa 发布时间:2024-04-19 10:46:22
标签:js,模态对话框
模态框(Modal Dialogue Box)也可叫做模态对话框,或者对话框,当一个模态框被打开时,用户可以与该对话框进行交互,点击关闭按钮可关闭该模态框!
功能实现:
1. 页面上有一个按钮,用于打开模态框,模态框默认隐藏;
2. 用户点击按钮,可打开模态框;用户点击模态框中的关闭或者点击页面其他地方可关闭该模态框
✦ 点击页面其他地方,关闭模态框,可用window.onclick事件
✦ 给关闭按钮绑定点击事件,按钮被点击,模态框Modal添加样式display:none;
✦ 给button按钮绑定点击事件,当按钮被点击时,模态框Modal添加样式display:block;
✦ 先获取页面上的button按钮,关闭按钮,以及模态框Modal
代码实现:
<html>
<head>
<style>
/* 弹窗 (background) */
.modal {
display: none; /* 默认隐藏 */
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
/* 弹窗内容 */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 35%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* 添加动画 */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* 关闭按钮 */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5587A2;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5587A2;
text-align: right;
color: white;
}
</style>
</head>
<body>
<!-- 打开弹窗按钮 -->
<button id="myBtn">打开弹窗</button>
<!-- 弹窗 -->
<div id="myModal" class="modal">
<!-- 弹窗内容 -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2>弹窗头部</h2>
</div>
<div class="modal-body">
<p>弹窗内容...</p>
<p>弹窗内容...</p>
</div>
<div class="modal-footer">
<h3>弹窗底部</h3>
</div>
</div>
</div>
<script>
// 获取弹窗
var modal = document.getElementById('myModal');
// 打开弹窗的按钮对象
var btn = document.getElementById("myBtn");
// 获取 <span> 元素,用于关闭弹窗 that closes the modal
var span = document.getElementsByClassName("close")[0];
// 点击按钮打开弹窗
btn.onclick = function() {
modal.style.display = "block";
}
// 点击 <span> (x), 关闭弹窗
span.onclick = function() {
modal.style.display = "none";
}
// 在用户点击其他地方时,关闭弹窗
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>


猜你喜欢
- 本篇文章介绍如何使用xlrd来读取Excel表格中的内容,xlrd是第三方库,所以在使用前我们需要安装xlrd。另外我们一般会使用xlwt来
- 有一个群友在群里问个如何快速搭建一个搜索引擎,在搜索之后我看到了这个代码所在Git:https://github.com/asciimoo/
- 几个方式(本文不作介绍),要将Session保存到SQL Server中,需要有以下几个步骤:1.首先要创建用于保存Session数据的数据
- 摘要:下拉菜单经常带来更多的可用性问题,并且常常容易被混淆。这是因为网页设计师们往往会在不同的几种情况下使用它。同时,滚动的菜单降低了网页的
- 起步在我的印象中,python的机制会自动清理已经完成任务的子进程的。通过网友的提问,还真看到了僵尸进程。import multiproce
- 本文实例讲述了python实现在控制台输入密码不显示的方法。分享给大家供大家参考。具体实现方法如下:import console;names
- 怎么用javascript进行拖拽本文译自:http://www.webreference.com/programming/javascri
- 文通过一个操作实例来说明SQL中主标识列IDENTITY的使用技巧。要求:在 sql server 2005中,建立数据表book,在表bo
- 1. SELECT INTO 语句用途:SELECT INTO 语句从一个表复制数据,然后把数据插入到另一个新表中,表结构与查询结构一致。P
- django-mdeditorGithub地址:https://github.com/pylixm/django-mdeditor 欢迎试用
- 方法在 Golang 中没有类,不过我们可以为结构体定义方法。我们看一个例子:package main import ( 
- 前言在程序中我们经常可以看到有很多的加密算法,比如说MD5 sha1等,今天我们就来了解下这下加密算法的吧,在了解之前我们需要知道一个模块嘛
- 本文实例为大家分享了python计算器的具体代码,供大家参考,具体内容如下主要用到的工具是Python中的Tkinter库比较简单直接上图形
- 本文实例为大家分享了python读取mysql数据绘制条形图的具体代码,供大家参考,具体内容如下Mysql 脚本示例:create tabl
- 前言Martin(Bob大叔)曾在《代码整洁之道》一书打趣地说:当你的代码在做 Code Review 时,审查者要是愤怒地吼道:“What
- 不知道用ASP写代码的朋友是不是和我有一样的感受,ASP中最头疼的就是调试程序的时候不方便,我想可能很多朋友都会用这样的方法&ldq
- 1.设计原则 1) 标准化和规范化 数据的标准化有助于消除数据库中的数据冗余。标准化有好几种形式,但Third Normal Form(3N
- 在基于互联网的应用中,程序经常需要自动地发送电子邮件。如:一个网站的注册系统会在用户注册时发送一封邮件来确认注册;当用户忘记登陆密码的时候,
- 一、主要目的最近在玩Python网络爬虫,然后接触到了selenium这个模块,就捉摸着搞点有意思的,顺便记录一下自己的学习过程。二、前期准
- 目录一、scrapy 分析1. 解析函数或数据入库出错,不会重试,会造成一定的数据丢失2. 运行方式,需借助命令行,不方便调试3. 入库 p