点击按钮弹出模态框实现

点击按钮弹出模态框的实现:

html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
	<title>模态框</title>
	<link rel="stylesheet" type="text/css" href="modalBox.css">
</head>
<body>
    <!-- 触发按钮 -->
    <button id="triggerBtn">模态框</button>
    <!-- 模态框 -->
    <div id="myModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2>头部</h2>
                <span id="closeBtn" class="close">×</span>
            </div>
            <div class="modal-body">
                <p>这是一个模态框!</p>
                <p>喜欢就点个赞吧!</p>
            </div>
            <div class="modal-footer">
                <h3>尾部</h3>
            </div>
        </div>
    </div>
<script type="text/javascript" src="modalBox.js"></script>
</body>
</html>
js:

(function() {
	/*建立模态框对象*/
	var modalBox = {};
	/*获取模态框*/
	modalBox.modal = document.getElementById("myModal");
    /*获得trigger按钮*/
	modalBox.triggerBtn = document.getElementById("triggerBtn");
    /*获得关闭按钮*/
	modalBox.closeBtn = document.getElementById("closeBtn");
	/*模态框显示*/
	modalBox.show = function() {
		console.log(this.modal);
		this.modal.style.display = "block";
	}
	/*模态框关闭*/
	modalBox.close = function() {
		this.modal.style.display = "none";
	}
	/*当用户点击模态框内容之外的区域,模态框也会关闭*/
	modalBox.outsideClick = function() {
		var modal = this.modal;
		window.onclick = function(event) {
            if(event.target == modal) {
            	modal.style.display = "none";
            }
		}
	}
    /*模态框初始化*/
	modalBox.init = function() {
		var that = this;
		this.triggerBtn.onclick = function() {
            that.show();
		}
		this.closeBtn.onclick = function() {
			that.close();
		}
		this.outsideClick();
	}
	modalBox.init();

})();
css:

/*模态框*/
.modal {
    display: none; /* 默认隐藏 */
    position: fixed; /* 根据浏览器定位 */
    z-index: 1; /* 放在顶部 */
    left: 0;
    top: 0;
    width: 100%; /* 全宽 */
    height: 100%; /* 全高 */
    overflow: auto; /* 允许滚动 */
    background-color: rgba(0,0,0,0.4); /* 背景色 */
}
/*模态框内容*/
.modal-content {
    display: flex; /*采用flexbox布局*/
    flex-direction: column; /*垂直排列*/
    position: relative;
    background-color: #fefefe;
    margin: 15% auto; /*距顶部15% 水平居中*/
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
    animation: topDown 0.4s; /*自定义动画,从模态框内容上到下出现*/
}
@keyframes topDown {
    from {top: -300px; opacity: 0}
    to {top: 0; opacity: 1}
}
/*模态框头部*/
.modal-header {
    display: flex; /*采用flexbox布局*/
    flex-direction: row; /*水平布局*/
    align-items: center; /*内容垂直居中*/
    justify-content: space-between; 
}
/*关闭X 样式*/
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}
.close:hover {
    color: black;
    text-decoration: none;
    cursor: pointer;
}







  • 23
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在JSP中点击按钮弹出模态框,可以使用Bootstrap框架中的Modal组件来实现。 首先,在JSP页面中引入Bootstrap的CSS和JS文件: ```html <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script> ``` 然后,在页面中添加一个按钮: ```html <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">打开模态框</button> ``` 其中,`data-toggle="modal"`表示点击按钮弹出模态框,`data-target="#exampleModal"`表示模态框的ID为exampleModal。 最后,在页面中添加模态框的HTML代码: ```html <div class="modal" tabindex="-1" role="dialog" id="exampleModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">模态框标题</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <p>模态框内容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary">保存</button> </div> </div> </div> </div> ``` 在上述代码中,`id="exampleModal"`与按钮中的`data-target="#exampleModal"`对应,`class="modal"`表示这是一个模态框,`role="dialog"`表示角色为对话框。 点击按钮后,模态框就会弹出点击关闭按钮或者点击模态框外部区域都可以关闭模态框
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值