弹出模态框在点击时显示相同的信息,模糊效果遮盖了弹出框。

huangapple go评论86阅读模式
英文:

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

&lt;main class=&quot;grid-container&quot; id=&quot;blur&quot;&gt;




&lt;article class=&quot;grid-item grid-item-3 tech-skills&quot;&gt;
        &lt;button class=&quot;pop-up-box-btn&quot; href=&quot;#&quot; onclick=&quot;toggle()&quot;&gt;⁺&lt;/button&gt;
        &lt;h1&gt;Technical Skills&lt;/h1&gt;
&lt;/article&gt;

&lt;div class=&quot;skill-popup&quot; id=&quot;skills-pop-up-box&quot;&gt;
    &lt;h2&gt;skills&lt;/h2&gt;
    &lt;p&gt;some more stuff&lt;/p&gt;
    &lt;a href=&quot;#&quot; onclick=&quot;toggle()&quot;&gt;Close&lt;/a&gt;
&lt;/div&gt;

&lt;article class=&quot;grid-item grid-item-4 professional-experience&quot;&gt;
    &lt;button class=&quot;pop-up-box-btn&quot; href=&quot;#&quot; onclick=&quot;toggle()&quot;&gt;⁺&lt;/button&gt;
    &lt;h1&gt;Professional Experience&lt;/h1&gt;
&lt;/article&gt;
&lt;div class=&quot;experience-popup&quot; id=&quot;experience-pop-up-box&quot;&gt;
    &lt;h2&gt;experience&lt;/h2&gt;
    &lt;p&gt;some more stuff&lt;/p&gt;
    &lt;a href=&quot;#&quot; onclick=&quot;toggle()&quot;&gt;Close&lt;/a&gt;
&lt;/div&gt;


.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(&#39;blur&#39;);
    blur.classList.toggle(&#39;active&#39;);
    let skillPopUp = document.getElementById(&#39;skills-pop-up-box&#39;);
    skillPopUp.classList.toggle(&#39;active&#39;);
    let experiencePopUp = document.getElementById(&#39;experience-pop-up-box&#39;);
    experiencePopUp.classList.toggle(&#39;active&#39;);

}

答案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'); // 最后,每次单击任何内容时都会运行此代码,因此每次都会显示经验弹出框

因此,解决方法是:

  1. 在您的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>

您应该对所有其他卡片执行相同的操作。

  1. 在您的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(&#39;blur&#39;);
    blur.classList.toggle(&#39;active&#39;);
    let skillPopUp = document.getElementById(&#39;skills-pop-up-box&#39;);
    skillPopUp.classList.toggle(&#39;active&#39;); // firstly this will be active
    let experiencePopUp = document.getElementById(&#39;experience-pop-up-box&#39;);
    experiencePopUp.classList.toggle(&#39;active&#39;); // 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

  1. In your html you can pass the id to the toggle function as the following
&lt;article class=&quot;grid-item grid-item-3 tech-skills&quot;&gt;
            &lt;button class=&quot;pop-up-box-btn&quot; href=&quot;#&quot; onclick=&quot;toggle(&#39;skills-pop-up-box&#39;)&quot;&gt;⁺&lt;/button&gt;
            &lt;h1&gt;Technical Skills&lt;/h1&gt;
    &lt;/article&gt;

    &lt;div class=&quot;skill-popup&quot; id=&quot;skills-pop-up-box&quot;&gt;
        &lt;h2&gt;skills&lt;/h2&gt;
        &lt;p&gt;some more stuff&lt;/p&gt;
        &lt;a href=&quot;#&quot; onclick=&quot;toggle(&#39;skills-pop-up-box&#39;)&quot;&gt;Close&lt;/a&gt;
    &lt;/div&gt;

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(&#39;blur&#39;);
    blur.classList.toggle(&#39;active&#39;);

    const currentPopUp = document.getElementById(id);
    currentPopUp.classList.toggle(&#39;active&#39;);
}

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:

&lt;body&gt;
    &lt;div class=&quot;overlay&quot;&gt;&lt;/div&gt;
    &lt;main class=&quot;grid-container&quot; id=&quot;blur&quot;&gt;

and in javascript modify the toggle function by adding the last 2 lines:

function toggle(id){
    const blur = document.getElementById(&#39;blur&#39;);
    blur.classList.toggle(&#39;active&#39;);

    const currentPopUp = document.getElementById(id);
    currentPopUp.classList.toggle(&#39;active&#39;);

    const overlay = document.querySelector(&#39;.overlay&#39;);
    overlay.classList.toggle(&#39;active&#39;);
}

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

huangapple
  • 本文由 发表于 2023年5月25日 10:23:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定