英文:
My pop up modals are showing the same info when clicked, and the blur is covering the pop up boxes
问题
我一直在搜索,但找不到答案,所以我希望有人可以帮助。我正在创建多个弹出框,每个弹出框将显示不同的信息,当弹出时,背后的屏幕将模糊。我的问题是,所有弹出框都只显示一个信息元素,忽略了其他元素。与此问题一起的是,屏幕上的模糊也覆盖了弹出框,我尝试了z-index和其他一些方法,但仍然感到困惑。如果需要,我将分享我的代码片段和完整项目的存储库。我没有包括第二个弹出框的CSS,因为设置相同。 (我只处理前两个,直到我解决问题)
https://github.com/CheyenneTech/Digital-Resume
<main class="grid-container" id="blur">
<article class="grid-item grid-item-3 tech-skills">
<button class="pop-up-box-btn" href="#" onclick="toggle()">⁺</button>
<h1>技术技能</h1>
</article>
<div class="skill-popup" id="skills-pop-up-box">
<h2>技能</h2>
<p>更多内容</p>
<a href="#" onclick="toggle()">关闭</a>
</div>
<article class="grid-item grid-item-4 professional-experience">
<button class="pop-up-box-btn" href="#" onclick="toggle()">⁺</button>
<h1>专业经验</h1>
</article>
<div class="experience-popup" id="experience-pop-up-box">
<h2>经验</h2>
<p>更多内容</p>
<a href="#" onclick="toggle()">关闭</a>
</div>
</main>
.grid-container#blur.active {
filter: blur(20px);
z-index: 1;
}
/* 弹出框样式 */
#skills-pop-up-box {
position: fixed;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
width: 600px;
padding: 50px;
box-shadow: 0px 3px 6px 0.5px rgba(62, 66, 66, 0.286);
background: #fff;
visibility: hidden;
opacity: 0;
transition: 0.5s;
z-index: 100;
}
#skills-pop-up-box.active {
visibility: visible;
opacity: 1;
transition: 0.5s;
top: 50%;
z-index: 100;
}
function toggle() {
let blur = document.getElementById('blur');
blur.classList.toggle('active');
let skillPopUp = document.getElementById('skills-pop-up-box');
skillPopUp.classList.toggle('active');
let experiencePopUp = document.getElementById('experience-pop-up-box');
experiencePopUp.classList.toggle('active');
}
英文:
I've been searching and can't figure out the answer, so I am hoping someone can help. I am creating multiple pop up boxes that will display different info and when it pops up the screen behind will blur. My issue is that all of the pop up boxes are showing one element of information and forgetting the rest. Along with that problem is the blur of the screen is also covering the pop up box, I've tried z-index and a couple other things but I am still lost. I'll share a snippit of my code and my repo for the full picture if need be. I left out the CSS for the 2nd pop up box because it is the same setup. (I'm working with just the 2 until I figure out the problem)
https://github.com/CheyenneTech/Digital-Resume
<main class="grid-container" id="blur">
<article class="grid-item grid-item-3 tech-skills">
<button class="pop-up-box-btn" href="#" onclick="toggle()">⁺</button>
<h1>Technical Skills</h1>
</article>
<div class="skill-popup" id="skills-pop-up-box">
<h2>skills</h2>
<p>some more stuff</p>
<a href="#" onclick="toggle()">Close</a>
</div>
<article class="grid-item grid-item-4 professional-experience">
<button class="pop-up-box-btn" href="#" onclick="toggle()">⁺</button>
<h1>Professional Experience</h1>
</article>
<div class="experience-popup" id="experience-pop-up-box">
<h2>experience</h2>
<p>some more stuff</p>
<a href="#" onclick="toggle()">Close</a>
</div>
.grid-container#blur.active {
filter: blur(20px);
z-index: 1;
}
/* pop up boxes */
#skills-pop-up-box {
position: fixed;
top: 40%;
left: 50%;
transform: translate(-50%,-50%);
width: 600px;
padding: 50px;
box-shadow: 0px 3px 6px 0.5px rgba(62, 66, 66, 0.286);
background: #fff;
visibility: hidden;
opacity: 0;
transition: 0.5s;
z-index: 100;
}
#skills-pop-up-box.active {
visibility: visible;
opacity: 1;
transition: 0.5s;
top: 50%;
z-index: 100;
}
function toggle(){
let blur = document.getElementById('blur');
blur.classList.toggle('active');
let skillPopUp = document.getElementById('skills-pop-up-box');
skillPopUp.classList.toggle('active');
let experiencePopUp = document.getElementById('experience-pop-up-box');
experiencePopUp.classList.toggle('active');
}
答案1
得分: 0
关于第一个问题,所有弹出框显示相同内容的原因是因为您将相同的函数 toggle
添加到所有按钮上,这是正确的,但当此函数运行并达到 toggle
函数时,它将执行以下相同的操作(请阅读注释)
let blur = document.getElementById('blur');
blur.classList.toggle('active');
let skillPopUp = document.getElementById('skills-pop-up-box');
skillPopUp.classList.toggle('active'); // 首先这将变为 active
let experiencePopUp = document.getElementById('experience-pop-up-box');
experiencePopUp.classList.toggle('active'); // 最后,每次单击任何内容时都会运行此代码,因此每次都会显示经验弹出框
因此,解决方法是:
- 在您的HTML中,您可以将id传递给toggle函数,如下所示:
<article class="grid-item grid-item-3 tech-skills">
<button class="pop-up-box-btn" href="#" onclick="toggle('skills-pop-up-box')">⁺</button>
<h1>Technical Skills</h1>
</article>
<div class="skill-popup" id="skills-pop-up-box">
<h2>skills</h2>
<p>some more stuff</p>
<a href="#" onclick="toggle('skills-pop-up-box')">Close</a>
</div>
您应该对所有其他卡片执行相同的操作。
- 在您的JavaScript中,使用传递的id来添加/删除active类,如下所示:
function toggle(id){
const blur = document.getElementById('blur');
blur.classList.toggle('active');
const currentPopUp = document.getElementById(id);
currentPopUp.classList.toggle('active');
}
对于第二个问题,我首先会删除这段CSS代码:
.grid-container#blur.active {
/* */
}
然后在HTML中,我会在主标签前的body
内添加一个新的div作为遮罩层,如下所示:
<body>
<div class="overlay"></div>
<main class="grid-container" id="blur">
接着在JavaScript中,通过添加最后两行来修改toggle函数:
function toggle(id){
const blur = document.getElementById('blur');
blur.classList.toggle('active');
const currentPopUp = document.getElementById(id);
currentPopUp.classList.toggle('active');
const overlay = document.querySelector('.overlay');
overlay.classList.toggle('active');
}
最后,在CSS中添加以下代码:
.overlay {
background-color: black; /* 任何颜色 */
opacity: 0.4; /* 任何百分比,注意您也可以使用十六进制或rgba颜色 */
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
display: none;
}
.overlay.active {
display: block;
}
英文:
For the first question about all pop up boxes showing the same content that's because you add event to all of your buttons with the same function toggle
that's good but when this runs and reaches the toggle
function it will do the same things as follows (read the comments)
let blur = document.getElementById('blur');
blur.classList.toggle('active');
let skillPopUp = document.getElementById('skills-pop-up-box');
skillPopUp.classList.toggle('active'); // firstly this will be active
let experiencePopUp = document.getElementById('experience-pop-up-box');
experiencePopUp.classList.toggle('active'); // lastly every time whatever you click this will run so every time the experience popup is the one that will be shown
So the solution is
- In your html you can pass the id to the toggle function as the following
<article class="grid-item grid-item-3 tech-skills">
<button class="pop-up-box-btn" href="#" onclick="toggle('skills-pop-up-box')">⁺</button>
<h1>Technical Skills</h1>
</article>
<div class="skill-popup" id="skills-pop-up-box">
<h2>skills</h2>
<p>some more stuff</p>
<a href="#" onclick="toggle('skills-pop-up-box')">Close</a>
</div>
You should do the same for all other cards. <br/>
2. In your javascript, use the passed id to add/remove the active class to it
function toggle(id){
const blur = document.getElementById('blur');
blur.classList.toggle('active');
const currentPopUp = document.getElementById(id);
currentPopUp.classList.toggle('active');
}
For the second question
I would firstly remove this css code section
.grid-container#blur.active {
/* */
}
and in the html I would add a new div for an overlay just firstly inside the body before the main tag as follows:
<body>
<div class="overlay"></div>
<main class="grid-container" id="blur">
and in javascript modify the toggle function by adding the last 2 lines:
function toggle(id){
const blur = document.getElementById('blur');
blur.classList.toggle('active');
const currentPopUp = document.getElementById(id);
currentPopUp.classList.toggle('active');
const overlay = document.querySelector('.overlay');
overlay.classList.toggle('active');
}
and in css add these lines
.overlay {
background-color: black; /* whatever color */
opacity: 40%; /* whatever percent, Note you can use hexa or rgba color instead */
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
display: none;
}
.overlay.active {
display: block;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论