简体   繁体   中英

How can I make my popup appear smoothly after 5 seconds?

The popup shall appear and cover the full screen after 5 seconds smoothly using the CSS3 transition property and opacity, however it appears all at once.

JSFiddle: https://jsfiddle.net/dnvk87xL/

 var element = document.getElementById("popup"); var t=setTimeout(openPopUp,5000); function openPopUp() { element = document.getElementById("popup"); element.style.display = "block"; element.style.opacity = "1"; } 
 #popup{ position: fixed; height:100%; background-color: green; display: none; opacity: 0; -webkit-transition: opacity 0.7s; transition: opacity 0.7s; } 
 <div> <div id="popup"> I'm gonna appear smoothly after 5 seconds </div> <p> Website content </p> </div> 

"Display" is not animatable CSS property. Instead, try using "Visibility".

 var element = document.getElementById("popup"); var t=setTimeout(openPopUp,5000); function openPopUp() { element = document.getElementById("popup"); element.style.visibility = "visible"; element.style.opacity = "1"; } 
 #popup{ position: fixed; height:100%; background-color: green; display: block; visibility: hidden; opacity: 0; -webkit-transition: opacity 0.7s; transition: opacity 0.7s; } 
 <div> <div id="popup"> I'm gonna appear smoothly after 5 seconds </div> <p> Website content </p> </div> 

Why don't you set display: block; from the beggining? Add a z-index to move it to the background too and change it after. Check it out:

 var element = document.getElementById("popup"); var t=setTimeout(openPopUp,5000); function openPopUp() { element = document.getElementById("popup"); element.style.opacity = "1"; element.style.zIndex = "1"; } 
 #popup{ position: fixed; height:100%; background-color: green; display: block; opacity: 0; -webkit-transition: opacity 0.7s; transition: opacity 0.7s; z-index:-1; } 
 <div> <div id="popup"> I'm gonna appear smoothly after 5 seconds </div> <p> Website content </p> </div> 

You can use the JQuery function FadeIn in your setTimeout

var timer = setTimeout(function(){
    element.FadeIn('slow');
}, 5000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM