我正在尝试用HTML / Javascript构建一个模态框,我发现了这个样本(rif。https://www.w3schools.com/howto/howto_css_modals.asp),这对我来说非常好......
我已经用这种方式修改了它......
<html>
<head>
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 70%;
}
</style>
</head>
<body>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
</script>
</body>
</html>
现在我想避免保留在模态框中的“x”(我知道..这样我就无法关闭模态框但这只是一个示例,在我的原始代码中我会以另一种方式关闭模态框....现在我只想避免在框内显示“x”...)。
我知道我使用的是JQuery,但目前我不想使用它并只使用HTML / Javascript
建议/示例?
答案 0 :(得分:1)
在modal-content
外单击以关闭模式。
同时删除了x
<html>
<head>
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 70%;
}
</style>
</head>
<body>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<p>Some text in the Modal..</p>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
modal.addEventListener('click', function (e) {
var target = e.target || e.srcElement;
if (target.getAttribute('id') === 'myModal') {
modal.style.display = 'none';
}
});
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
</script>
</body>
</html>
&#13;
答案 1 :(得分:1)
这是我的榜样,希望能帮到你。
<html>
<head>
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
#modal-content {
position: absolute;
top:20px;
left:15%; /* 15% left | 70% center | 15% right*/
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 70%;
display: none;
z-index:1 ; /* Sit on top */
}
</style>
</head>
<body>
<h2>Modal Example</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
</div>
<!-- Modal content -->
<div id="modal-content">
<p>Some text in the Modal..</p>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var modalContent = document.getElementById('modal-content');
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
modalContent.style.display = "block";
}
var x = document.getElementById("myModal");
x.onclick = function () {
modal.style.display = "none";
modalContent.style.display = "none";
}
</script>
</body>
</html>