javascript - 如何在对底层 Div 产生模糊效果后获得模糊效果

标签 javascript html css

我已经为着陆页制作了模糊效果。然而,它也不太像预期的那样工作,我不确定为什么。

它很好地模糊了,问题在于当我希望底层 Div 出现时,我希望它“淡入”,因为它只是在淡出完成后出现。

这是一个片段:

splash = document.getElementById('intro');
content = document.getElementById('content');

	function enterSite (element) {
		opac = 1;
		fps = 1000/30;
		function decrease() {
			opac -= 0.02;
			if (opac <= 0.1){
				splash.style.display = 'none';
				return true;
			}
		splash.style.opacity = opac;
		setTimeout(decrease,fps);
		}
		function increase() {
			opac += 0.02;
			if (opac >= 0.1){
				content.style.display = 'block';
				return false;
			}
		content.style.opacity = opac;
		setTimeout(increase,fps);
		}
		decrease(), increase();
	}
html, body {
    overflow: hidden;
}
main {
    width: 100%;
    height: 100vh;
    position: relative;
}
#intro {
    background-image: url(Images/splash.jpg);   
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    text-align: center;
    height: 100vh;
}
#splash {
    margin: auto;
    width: 40%;
    background-color: rgba(56,56,56,0.4);
    border-radius: 50px 50px;
}
#splash-p {
    width: 70%;
    font-size: 1.2em;
    line-height: 1.5em;
    margin: auto;
    text-align: center;
    padding-top: 10px;
    color: #fff;
}
.btn {
    width: 35%;
    margin: auto;
    margin-top: 10px;
    margin-bottom: 10px;
}

/* Main Content Page */

article {
    position: absolute;
    height: 100vh;
    background-color: red;
}
<main>
		<div id="intro">
			<div id="splash">

			<p id="splash-p">Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing.</p> 

			<input type="image" src="Images/Button.png" class="btn" onclick="enterSite()">

			</div>
		</div>

		<article id="content">
			
			Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,Just a random piece of online text that means nothing,

		</article>

	</main>

最佳答案

我认为您可以通过将内容分解得更多一些而不是嵌套那么多函数和调用来获益。此外,使用 setInterval() 而不是重复 setTimeout() 可能会有所帮助。

splash = document.getElementById('intro');
content = document.getElementById('content');
var fadeOut = null;
var fadeIn = null;
opac = 1;
fps = 1000/30;

function increase() {
    content.style.opacity = opac;
    opac += 0.02;

    if (opac >= 1){ //Opacity is 100%
      window.clearInterval(fadeIn); //Stop fade-in
    }

}

function decrease() {
    splash.style.opacity = opac;
    opac -= 0.02;

    if (opac < 0.1){ //If object is almost gone
        splash.style.display = 'none'; //Hide it completely
        window.clearInterval(fadeOut); //Stop fade-out

        content.style.display = 'block'; //Set up new content
        content.style.opacity = 0;
        fadeIn = setInterval(increase, fps); //Begin fade-in

    }
}

function enterSite () {
    fadeOut = setInterval(decrease, fps); //Start the fadeout
}

JSFiddle

关于javascript - 如何在对底层 Div 产生模糊效果后获得模糊效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39882308/

相关文章:

javascript - jQuery 解析字符串中的 unicode 字符

javascript - Hammer.js:如何检测任意数量/多个手指的捏合

javascript - 将 Javascript 转换为 jQuery ajax?

html - 粘性页脚 - 试图理解代码

html - 有没有办法减慢悬停效果?

javascript - 当类名相同时单击事件不起作用

python - 如何使用python和selenium IDE获取网页上的所有链接

javascript - 如何计算字符串中单词的频率

javascript - jquery创建没有结束标签的元素

HTML,flex,连续两个 div,强制一个 div 的大小更小