How to make a background blur effect that gradually disappears when closing a pop-up, and similarly a smooth exit animation for said pop-up?

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

How to make a background blur effect that gradually disappears when closing a pop-up, and similarly a smooth exit animation for said pop-up?

问题

以下是您要翻译的代码部分:

<div id="backgroundimage"></div>
<header>
    <h1>Title</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    <nav class="navbar">
        <ul>
            <li><a href="#"> Link one </a></li>
            <li><a href="#"> Link two </a></li>
            <li><a href="#"> Link three </a></li>
            <li><a href="#"> Link four </a></li>
            <li><a href="#" onclick="openPopup()" value="open"/> Open pop-up </a></li>
        </ul>
    </nav>
</header>

<div id="popupBox">
    <div class="popupContent">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus aliquam consequat viverra.  
        Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. 
        Donec eleifend nisi et justo rutrum sagittis. 
        Integer velit dolor, finibus a turpis sed, pulvinar feugiat nulla.  </p>
    <button class="closebutton" onclick="closePopup()" value="close"/> Close </button>
    </div>
</div>

<script>
    function openPopup() {
        document.getElementById("popupBox").style.display = "block";
    }

    function closePopup() {
        document.getElementById("popupBox").style.display = "none";
    }

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function (event) {
        var modal = document.getElementById('popupBox');
        if (event.target == modal) {
            closePopup();
        } 
    }
</script>
#popupBox {
  display: none; 
  z-index: 1; 
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin:0;
  width: 100%;
  height: 100%;
  overflow: auto;
  animation: fadepopup 0.6s ease 0s 1 normal forwards;
  backdrop-filter: blur(5px);
}             

.popupContent {
  margin: auto;
  background-color: #fff;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 50px;
  outline: 0;
  width: 700px;
  height: 400px;
} 

@keyframes fadepopup {
    0% {opacity: 0;}
    100% {opacity: 1;}
}

请注意,我已经删除了您的原始文本中的英文部分,只保留了HTML和CSS代码。

英文:

(Premise: I've been learning how to code for two months by myself, I'm a beginner so I need very straight-forward, "fool-proof" answers, possibly with clear examples.
Also forgive me if the question is unclear, English is not my native language)

So by far I managed to create a pop-up box that can be closed either by clicking on a button inside it or by clicking outside the box, creating a blur effect on the background when the box is open.

I want to make it so that when closing the pop-up the background gradually loses the blur, but I haven't been able to achieve that kind of effect: when I close it, the background abruptly returns to its original state, it's not smooth at all.
I'd also like to apply the same smooth transition principle to the entry and exit of the box: so far the fade-in applies only when I open the box, but when I close it, it returns to its original state without fading.

This is the HTML with Javascript so far:

&lt;div id=&quot;backgroundimage&quot;&gt;&lt;/div&gt;
                &lt;header&gt;
                    &lt;h1&gt;Title&lt;/h1&gt;
                    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/p&gt;
                    &lt;nav class=&quot;navbar&quot;&gt;
                        &lt;ul&gt; 
                            &lt;li&gt; &lt;a href=&quot;#&quot;&gt; Link one &lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt; &lt;a href=&quot;#&quot;&gt; Link two &lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt; &lt;a href=&quot;#&quot;&gt; Link three &lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt; &lt;a href=&quot;#&quot;&gt; Link four &lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt; &lt;a href=&quot;#&quot; onclick=&quot;openPopup()&quot; value=&quot;open&quot;/&gt; Open pop-up &lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/nav&gt;
                &lt;/header&gt;

&lt;div id=&quot;popupBox&quot;&gt;
                    &lt;div class=&quot;popupContent&quot;&gt;
                    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus aliquam consequat viverra.  
                        Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. 
                        Donec eleifend nisi et justo rutrum sagittis. 
                        Integer velit dolor, finibus a turpis sed, pulvinar feugiat nulla.  &lt;/p&gt;
                    &lt;button class=&quot;closebutton&quot; onclick=&quot;closePopup()&quot; value=&quot;close&quot;/&gt; Close &lt;/button&gt;
                    &lt;/div&gt;
                &lt;/div&gt;

&lt;script&gt;
                    function openPopup() {
                        document.getElementById(&quot;popupBox&quot;).style.display = &quot;block&quot;;
                    }

                    function closePopup() {
                        document.getElementById(&quot;popupBox&quot;).style.display = &quot;none&quot;;
                    }

                    // When the user clicks anywhere outside of the modal, close it
                    window.onclick = function (event) {
                        var modal = document.getElementById(&#39;popupBox&#39;);
                       if (event.target == modal) {
                            closePopup();
                        } 
                    }
    &lt;/script&gt;

This is the CSS

#popupBox {
  display: none; 
  z-index: 1; 
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin:0;
  width: 100%;
  height: 100%;
  overflow: auto;
  animation: fadepopup 0.6s ease 0s 1 normal forwards;
  backdrop-filter: blur(5px);
}             

.popupContent {
  margin: auto;
  background-color: #fff;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 50px;
  outline: 0;
  width: 700px;
  height: 400px;
} 


@keyframes fadepopup {
	0% {opacity: 0;}

	100% {opacity: 1;}
}

I'm sure I'm doing something wrong somewhere, but I can't figure out how to achieve all the above (it's already a miracle to me that I was able to get the opening and closing of the box to work by both pressing the a button and clicking out of the box itself).

Thanks in advance to anyone who'll try to help with this.

答案1

得分: 0

在样式中,添加一个更多的关键帧

@keyframes fadeout {
	0% {
		opacity: 1;
	}
	100% {
		opacity: 0;
	}
}

在脚本中,对两个函数进行一些更改。

openPopup()

function openPopup() {
	const box = document.getElementById('popupBox');
	box.style.animationName = 'fadepopup';
	document.getElementById('popupBox').style.display = 'block';
}

closePopup()

function closePopup() {
	const box = document.getElementById('popupBox');
	box.style.animationName = 'fadeout';
	setTimeout(() => {
		box.style.display = 'none';
	}, 600);
}

600等于0.6秒。

英文:

In the style, add one more keyframe

@keyframes fadeout {
	0% {
		opacity: 1;
	}
    100% {
		opacity: 0;
	}
}

In the script, do a little change for two functions.

openPopup()

function openPopup() {
	const box = document.getElementById(&#39;popupBox&#39;)
	box.style.animationName = &#39;fadepopup&#39;;
	document.getElementById(&#39;popupBox&#39;).style.display = &#39;block&#39;
}

closePopup()

function closePopup() {
	const box = document.getElementById(&#39;popupBox&#39;)
	box.style.animationName = &#39;fadeout&#39;;
	setTimeout(() =&gt; {
		box.style.display = &#39;none&#39;
	}, 600)
}

600 is equal to 0.6s

huangapple
  • 本文由 发表于 2023年2月27日 17:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578672.html
匿名

发表评论

匿名网友

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

确定