Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

html - Fade-In after timed visibility through Javascript using CSS or JS

I have an image that disappears (via javascript) and then fades out (via CSS) on my page, and then once this happens I have a div with text that appears once the image disappears. What I am hoping to do and am having problems with is making the text that appears after 5 seconds appear with a fade in ... html/js as follows:


<script type="text/javascript">
var random_images_array = ['light.jpg', 'dark.jpg', 'photo.jpg'];
    
function getRandomImage(imgAr, path) {
    path = path || 'images/'; // default path here
    var num = Math.floor( Math.random() * imgAr.length );
    var img = imgAr[ num ];
    var imgStr = '<img src="' + path + img + '" alt = "">';
    document.write(imgStr); document.close();
}
</script>

</head>
<body>
   
<div id="welcomeImage" class="fadeout">
    <script type="text/javascript">getRandomImage(random_images_array, 'images/')</script>
</div>


<div id="introText" class="animated fadeIn">
    <p>Div with Text</p>
</div>

<script type="text/javascript">window.setTimeout("document.getElementById('welcomeImage').style.display='none';", 4000); </script>
<script type="text/javascript">
function showIt() {document.getElementById("introText").style.visibility = "visible";}
setTimeout("showIt()", 5000); </script>

</body>
</html>

CSS:

.fadeout {
    animation: fadeOut 1s forwards; 
    animation-delay: 3s;
}

@keyframes fadeOut {
    from {opacity: 1;}
    to {opacity: 0;}
}

/* One option I tried that did not work out

.fadein {
    animation: fadeIn 3s forwards;  
    animation-delay: 5s;
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity: 1;}
}
*/

/*my most current attempt at fadein through CSS */

.animated { 
    animation-duration: 3s; 
    animation-fill-mode: both; 
    animation-delay: 5;
} 

@keyframes fadeIn { 
    0% {opacity: 0;} 
    100% {opacity: 1;} 
} 
.fadeIn { 
    animation-name: fadeIn; 
}

#introText {
    width: auto; 
    padding: 100px;
    visibility: hidden;
    text-align: center;
    height: auto;
}
'''

Am i able to add a fade-in transition into the visibility script? I tried doing a fade in with CSS but could not get it to work.

I do not know the JS to add it to my script and have tried searching for it but could not find anything for my specific situation.

If anyone sees anything I could fix in my CSS to make it fade in properly (maybe a timing issue?) or know how I can include a fade-in in my script making the text visible it would be much appreciated!

Thanks!!
question from:https://stackoverflow.com/questions/65546831/fade-in-after-timed-visibility-through-javascript-using-css-or-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe instead of toggleing visibility you can toggle the display from none to block.

Take a look here:

.fadeout {
    animation: fadeOut 1s forwards; 
    animation-delay: 3s;
}

@keyframes fadeOut {
    from {opacity: 1;}
    to {opacity: 0;}
}

/* One option I tried that did not work out

.fadein {
    animation: fadeIn 3s forwards;  
    animation-delay: 5s;
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity: 1;}
}
*/

/*my most current attempt at fadein through CSS */

.animated { 
    animation-duration: 3s; 
    animation-fill-mode: both; 
} 

@keyframes fadeIn { 
    0% {opacity: 0;} 
    100% {opacity: 1;} 
} 
.fadeIn { 
    animation-name: fadeIn; 
}

#introText {
    width: auto; 
    padding: 100px;
    display: none;
    text-align: center;
    height: auto;
}
<script type="text/javascript">
var random_images_array = ['light.jpg', 'dark.jpg', 'photo.jpg'];
    
function getRandomImage(imgAr, path) {
    path = path || 'https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fr.ddmcdn.com%2Fs_f%2Fo_1%2FAPL%2Fuploads%2F2014%2F10%2Fintro-cats-at-home-324x205.jpg&f=1&nofb=1'; // default path here
    var num = Math.floor( Math.random() * imgAr.length );
    var img = imgAr[ num ];
    var imgStr = '<img src="' + path + img + '" alt = "">';
    document.write(imgStr); document.close();
}
</script>


<body>
   
<div id="welcomeImage" class="fadeout">
    <script type="text/javascript">getRandomImage(random_images_array, 'https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fr.ddmcdn.com%2Fs_f%2Fo_1%2FAPL%2Fuploads%2F2014%2F10%2Fintro-cats-at-home-324x205.jpg&f=1&nofb=1')</script>
</div>


<div id="introText" class="animated fadeIn">
    <p>Div with Text</p>
</div>

<script type="text/javascript">window.setTimeout("document.getElementById('welcomeImage').style.display='none';", 4000); </script>
<script type="text/javascript">
function showIt() {document.getElementById("introText").style.display = "block";}
setTimeout("showIt()", 5000); </script>

</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...