gpt4 book ai didi

javascript - body 背景模糊功能不适用于多种模式

转载 作者:行者123 更新时间:2023-11-27 22:53:18 26 4
gpt4 key购买 nike

介绍 - 我创建了一个模态窗口,其中多个模态窗口可以在彼此之上打开并一个接一个地关闭最近我添加了模糊功能,其中如果一个模态窗口被打开, body 背景变得模糊,如果打开第二个窗口, body 和第一个窗口都会变得模糊。关闭时遵循相同的概念......关闭第二个窗口使第一个窗口正常,关闭第一个窗口使主体背景正常。

对于这个特定的问题,我创建了 3 个模态 - 第一个有一个按钮来触发它,第二个可以从第一个模态的主体打开,第三个模态有一个按钮

介绍中提到的模糊功能仅适用于第一和第二模态,不适用于第三模态 - 在第三模态中,关闭模态窗口不会像发生的那样使主体背景不模糊与前 2 个模态

[第三个模态只是一个解释这个问题的最小例子,我理想情况下希望它能与 n 个模态一起工作]

希望有人能给出一个可行的解决方案

我在片段中包括了整个 Html、css 和 JavaScript 代码,虽然只是为了非常具体和方便你们,我觉得这是 JavaScript 中的一部分,需要一些更改..

          if (index > 0) {
var parentModal = spans[index - 1].parentElement.parentElement;
parentModal.classList.remove("open");
if (parentModal.getElementsByClassName('open').length > 0) {
parentModal.getElementsByClassName('open')[0].classList.remove("open");
}
} else {
document.body.classList.remove("open");
}

完整代码从这里开始:(Js、Html 和 Css)

let open_modals = [];

$(function() {

// Get the button that opens the modal
// read all the control of any type which has class as modal-button
var btn = document.querySelectorAll(".modal-button");

// All page modals
var modals = document.querySelectorAll('.modal');

// Get the <span> element that closes the modal
var spans = document.getElementsByClassName("close");

// When the user clicks the button, open the modal
for (var i = 0; i < btn.length; i++) {
btn[i].onclick = function(e) {
e.preventDefault();
modal = document.querySelector(e.target.getAttribute("href"));
modal.style.display = "block";
open_modals.push(modal.id);
document.body.style.overflow = "hidden";

if (this.parentElement.parentElement.nodeName == 'BODY') {
document.body.classList.add("open");
} else {
this.parentElement.parentElement.parentElement.classList.add("open");
}
}
}

function checkRenableScroll() {
if (!open_modals.length) {
document.body.style.overflow = "scroll";
}
}

// When the user clicks on <span> (x), close the modal
for (var i = 0; i < spans.length; i++) {
spans[i].onclick = function() {
for (var index in modals) {
if (typeof modals[index].style !== 'undefined' && modals[index].id == open_modals[open_modals.length - 1]) {
modals[index].classList.add("modal-content-active");
var item = modals[index];

if (index > 0) {
var parentModal = spans[index - 1].parentElement.parentElement;
parentModal.classList.remove("open");
if (parentModal.getElementsByClassName('open').length > 0) {
parentModal.getElementsByClassName('open')[0].classList.remove("open");
}
} else {
document.body.classList.remove("open");
}

setTimeout(function() {
item.classList.remove("modal-content-active");
item.style.display = "none";
open_modals.pop();
checkRenableScroll();

}, 400);
}
}
}
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target.classList.contains('modal')) {
for (var index in modals) {
if (typeof modals[index].style !== 'undefined' && modals[index].id == open_modals[open_modals.length - 1]) {
modals[index].classList.add("modal-content-active");
var item = modals[index];

if (index > 0) {
var parentModal = spans[index - 1].parentElement.parentElement;
parentModal.classList.remove("open");
if (parentModal.getElementsByClassName('open').length > 0) {
parentModal.getElementsByClassName('open')[0].classList.remove("open");
}
} else {
document.body.classList.remove("open");
}

setTimeout(function() {
item.classList.remove("modal-content-active");
item.style.display = "none";
open_modals.pop();
checkRenableScroll();

}, 400);
}
}
}
}
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>
<section id="first">

<!-- Trigger/Open The Modal -->
<a href="#myModal1" class="modal-button">• First Modal</a><br>

<p>Second Modal can be triggered from body of first modal window<br>

<a href="#myModal3" class="modal-button">• Third Modal</a>

</section>

<!-- The Modal -->
<div id="myModal1" class="modal">

<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="headertext">
Modal window 1
</div>
</div>
<div class="modal-body">
<img class="pic" src="https://drive.google.com/thumbnail?id=108ZLeoIfNkKODfRbLuPWpmXRl0gH9qkD">
<div class="bodytext">
<ul><li>You are currently viewing first Modal window
<span class="bold"><a href="#myModal2" class="modal-button">Click here to open second modal window</a></span></li></ul>
</div>
</div>
</div>
</div>

<!-- The Modal -->
<div id="myModal2" class="modal">

<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="headertext">
Modal window 2
</div>
</div>
<div class="modal-body">
<img class="pic" src="https://drive.google.com/thumbnail?id=108ZLeoIfNkKODfRbLuPWpmXRl0gH9qkD">
<div class="bodytext">
You are currently viewing Modal window 2
</div>
</div>
</div>
</div>

<!-- The Modal -->
<div id="myModal3" class="modal">

<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="headertext">
Modal window 3
</div>
</div>
<div class="modal-body">
<img class="pic" src="https://drive.google.com/thumbnail?id=108ZLeoIfNkKODfRbLuPWpmXRl0gH9qkD">
<div class="bodytext">
You are currently viewing Modal window 3
</div>
</div>
</div>
</div>

</body>
</html>
@import url('https://fonts.googleapis.com/css?family=Quicksand&display=swap');

/* The Modal (background) */

.modal {
box-sizing: border-box;
font-family: 'Quicksand', sans-serif;
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 3.125rem;
/* 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 {
color: white;
position: relative;
background-color: #171B20;
margin: auto;
padding: 0;
border: 0.0625rem solid #888;
width: 90%;
box-shadow: 0 0.25rem 0.5rem 0 rgba(0, 0, 0, 0.2), 0 0.375rem 1.25rem 0 rgba(0, 0, 0, 0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s;
}


/* Add Animation */

@-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;
}
}

@-webkit-keyframes animateBottom {
from {
top: 0px;
opacity: 1;
}
to {
top: 500px;
opacity: 0;
}
}

@keyframes animateBottom {
from {
top: 0px;
opacity: 1;
}
to {
top: 300px;
opacity: 0;
}
}

.modal-content-active {
-webkit-animation-name: animateBottom;
-webkit-animation-duration: 0.4s;
animation-name: animateBottom;
animation-duration: 0.4s;
}


/* The Close Button */

.close {
color: #F0B823;
float: right;
font-size: 2.6rem;
font-weight: bold;
position: absolute;
right: 0.25rem;
top: -0.25rem;
}

.close:hover,
.close:focus {
color: #fff;
text-decoration: none;
cursor: pointer;
}

.modal-header {
padding: 0.125rem 1rem;
background-color: #171B20;
color: #F0B823;
}

.modal-body {}

.modal-button {
font-family: 'Quicksand', sans-serif;
background-color: #171B20;
border: none;
color: white;
padding: 0.248em 0.496em;
text-align: left;
text-decoration: none;
display: inline-block;
font-size: 1.750rem;
margin: 0.124em 0.062em;
-webkit-transition-duration: 0;
/* Safari */
transition-duration: 0;
cursor: pointer;
width: auto;
}

.modal-button:hover {
background-color: #171B20;
color: #F0B823;
}

.pic {
margin: auto;
display: block;
height: auto;
width: 50vh;
}

.headertext {
font-family: 'Quicksand', sans-serif;
display: block;
text-align: center;
font-size: 2rem;
}

.bodytext {
font-size: 1.125rem;
font-family: 'Quicksand', sans-serif;
display: block;
padding: 0.625em 0.9375em;
line-height: 2rem;
}

p {
display: block;
margin: 0;
padding: 0;
}

.open>* {
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
}

.modal {
-webkit-filter: blur(0px);
-moz-filter: blur(0px);
-o-filter: blur(0px);
-ms-filter: blur(0px);
filter: blur(0px);
}

.modal .open {
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
}

.bold {
display: inline-block;
font-weight: 900;
}

最佳答案

一个 Jquery 实现可以去除 20 行以下不需要的代码(从 100 行减少到 17 行)并且不那么复杂。

$(function () {
const openModals = [];
$('.modal-button').click(e => {
e.preventDefault();
$(e.target).closest('.modal').add('body').addClass('open');
openModals.push($($(e.target).attr('href')).show());
});
$(window).add('.close').click(e => {
e.stopPropagation();
if ($(e.target).is('.modal, .close')) {
const closing = openModals.pop().addClass('modal-content-active');
setTimeout(() => {closing.hide().removeClass('modal-content-active')}, 400);
if (openModals.length > 0) {
openModals[openModals.length - 1].removeClass('open');
} else $('body').removeClass('open');
}
});
});

$(function () {
const openModals = [];
$('.modal-button').click(e => {
e.preventDefault();
$(e.target).closest('.modal').add('body').addClass('open');
openModals.push($($(e.target).attr('href')).show());
});
$(window).add('.close').click(e => {
e.stopPropagation();
if ($(e.target).is('.modal, .close')) {
const closing = openModals.pop().addClass('modal-content-active');
setTimeout(() => {closing.hide().removeClass('modal-content-active')}, 400);
if (openModals.length > 0) {
openModals[openModals.length - 1].removeClass('open');
} else $('body').removeClass('open');
}
});
});
@import url('https://fonts.googleapis.com/css?family=Quicksand&display=swap');

/* The Modal (background) */

.modal {
box-sizing: border-box;
font-family: 'Quicksand', sans-serif;
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 3.125rem;
/* 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 {
color: white;
position: relative;
background-color: #171B20;
margin: auto;
padding: 0;
border: 0.0625rem solid #888;
width: 90%;
box-shadow: 0 0.25rem 0.5rem 0 rgba(0, 0, 0, 0.2), 0 0.375rem 1.25rem 0 rgba(0, 0, 0, 0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s;
}


/* Add Animation */

@-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;
}
}

@-webkit-keyframes animateBottom {
from {
top: 0px;
opacity: 1;
}
to {
top: 500px;
opacity: 0;
}
}

@keyframes animateBottom {
from {
top: 0px;
opacity: 1;
}
to {
top: 300px;
opacity: 0;
}
}

.modal-content-active {
-webkit-animation-name: animateBottom;
-webkit-animation-duration: 0.4s;
animation-name: animateBottom;
animation-duration: 0.4s;
}


/* The Close Button */

.close {
color: #F0B823;
float: right;
font-size: 2.6rem;
font-weight: bold;
position: absolute;
right: 0.25rem;
top: -0.25rem;
}

.close:hover,
.close:focus {
color: #fff;
text-decoration: none;
cursor: pointer;
}

.modal-header {
padding: 0.125rem 1rem;
background-color: #171B20;
color: #F0B823;
}

.modal-body {}

.modal-button {
font-family: 'Quicksand', sans-serif;
background-color: #171B20;
border: none;
color: white;
padding: 0.248em 0.496em;
text-align: left;
text-decoration: none;
display: inline-block;
font-size: 1.750rem;
margin: 0.124em 0.062em;
-webkit-transition-duration: 0;
/* Safari */
transition-duration: 0;
cursor: pointer;
width: auto;
}

.modal-button:hover {
background-color: #171B20;
color: #F0B823;
}

.pic {
margin: auto;
display: block;
height: auto;
width: 50vh;
}

.headertext {
font-family: 'Quicksand', sans-serif;
display: block;
text-align: center;
font-size: 2rem;
}

.bodytext {
font-size: 1.125rem;
font-family: 'Quicksand', sans-serif;
display: block;
padding: 0.625em 0.9375em;
line-height: 2rem;
}

p {
display: block;
margin: 0;
padding: 0;
}

.open {
overflow: hidden;
}

.open>* {
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
}

.modal {
-webkit-filter: blur(0px);
-moz-filter: blur(0px);
-o-filter: blur(0px);
-ms-filter: blur(0px);
filter: blur(0px);
}

.modal .open {
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
}

.bold {
display: inline-block;
font-weight: 900;
}
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
<section id="first">
<!-- Trigger/Open The Modal -->
<a href="#myModal1" class="modal-button">• First Modal</a><br>
<p>Second Modal can be triggered from body of first modal window<br>
<a href="#myModal3" class="modal-button">• Third Modal</a>
</section>

<!-- The Modal -->
<div id="myModal1" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header"><span class="close">×</span>
<div class="headertext">Modal window 1</div>
</div>
<div class="modal-body">
<img class="pic" src="https://drive.google.com/thumbnail?id=108ZLeoIfNkKODfRbLuPWpmXRl0gH9qkD">
<div class="bodytext">
<ul>
<li>You are currently viewing first Modal window <span class="bold"><a href="#myModal2" class="modal-button">Click here to open second modal window</a></span></li>
</ul>
</div>
</div>
</div>
</div>

<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header"><span class="close">×</span>
<div class="headertext">Modal window 2</div>
</div>
<div class="modal-body">
<img class="pic" src="https://drive.google.com/thumbnail?id=108ZLeoIfNkKODfRbLuPWpmXRl0gH9qkD">
<div class="bodytext">You are currently viewing Modal window 2</div>
</div>
</div>
</div>

<!-- The Modal -->
<div id="myModal3" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header"><span class="close">×</span>
<div class="headertext">Modal window 3</div>
</div>
<div class="modal-body">
<img class="pic" src="https://drive.google.com/thumbnail?id=108ZLeoIfNkKODfRbLuPWpmXRl0gH9qkD">
<div class="bodytext">You are currently viewing Modal window 3</div>
</div>
</div>
</div>
</body>

</html>

关于javascript - body 背景模糊功能不适用于多种模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57847428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com