Transitioning smoothly between images (using key frames and specified durations)

I have implemented a cross-fading image animation in CSS that transitions between two images. I am looking for guidance on how to adjust the duration of each image display to approximately 10 seconds instead of an instant transition. The total animation length is set to 3 seconds, and I want each image to remain visible for 10 seconds before switching to the next one. It seems like I need to modify the code related to opacity and percentage values, but I'm struggling with the syntax. Any assistance would be greatly appreciated. Below is the snippet of my current code. You can also find a fiddle here if it helps.

CSS Code:

/*INDEX PAGE CSS*/
 #index_banner {
    height:360px;
    position:relative;
    margin:0 auto;
}
#index_banner img {
    position:absolute;
    -webkit-animation: cf4FadeInOut 3s;
    animation: cf4FadeInOut 3s;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
}
@-webkit-keyframes cf4FadeInOut {
    0% {
        opacity:1;
    }
    50% {
        opacity:0;
    }
    100% {
        opacity:1;
    }
}
@keyframes cf4FadeInOut {
    0% {
        opacity:1;
    }
    50% {
        opacity:0;
    }
    100% {
        opacity:1;
    }
}
#index_banner img:nth-child(odd) {
    -webkit-animation-delay: 4s;
    animation-delay: 4s;
}
#welcome_text {
    padding-top: 20px;
    text-align: center;
    line-height: 2em;
}
#trailer_title {
    text-align: center;
}
#trailer_video {
    padding-top: 10px;
    text-align: center;
    padding-bottom:20px;
}

HTML Code:

<div id="index_banner">
    <img class="bottom" src="images/SPAWN IMAGE.png" alt="INDEX BANNER">
    <img class="top" src="images/SURVIVAL IMAGE - GAMEMODES.png" alt="INDEX BANNER 2">
</div>

Answer №1

Check out the updated version with the desired result here

To achieve this effect, I implemented the following steps:

1) Set position:absolute; for images.

2) Increased the animation time to 15s.

3) Added

animation-timing-function:ease-in-out;
for smoother animation transitions.

4) Applied animation-delay:8s to #index_banner img:nth-child(odd).

5) Adjusted the keyframes as shown below:

@keyframes cf4FadeInOut {

   0% {
        opacity:0;
    }
    30% {
        opacity:0; 
    }
    40%{
        opacity:1; 
    }
    75%{
        opacity:1; 
    }
    85%{
        opacity:1; 
    }

    100% {
        opacity:0;
    }

}

Explanation of Keyframes:

The keyframes are responsible for fading in the image after 4.5 seconds (30% of 15 seconds), keeping it visible for a few seconds, and then gradually fading it out.

Why is INDEX BANNER 1 displayed at the beginning?

This is due to the animation-delay set for it, causing it to start fading out after 8 seconds from an initial opacity of 0.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Background: Cover causing image to be cut off

I'm having trouble trying to display the top part of a background image under my current navigation bar. I've been unable to identify what mistake I may be making here. Here is my HTML code: <section class="jumbotron"> <div class=" ...

Update the appearance based on the dimensions of the parent container with Bootstrap

Here's the situation I'm facing on my webpage: <div id="myDiv"> <p>A</p> <p>B</p> </div> If 'myDiv' is narrower than width X, display A & B vertically. Otherwise, display A and B horizo ...

Adjust the color of a linked tab with a dropdown list on WordPress navigation menu using CSS

I'm currently working on a WordPress website utilizing the Twenty-Twelve theme. One of the main menu tabs is labeled 'Sales Items'. When hovering over this tab, it displays various categories of sales items in a dropdown list. However, I am ...

Choose an option from the dropdown menu, then eliminate other choices that have the same value as the one selected

Our system involves using dropdown menus to select an airport and its terminal, followed by another dropdown menu to choose a specific service within that airport and terminal. For example, selecting Airport 1 and the International Terminal would then prom ...

Creating a link button using JavaScript

I have integrated a Setmore code on my website to facilitate appointment booking, which triggers a popup similar to what you would see on a hotel reservation site. <script id="setmore_script" type="text/javascript" src="https://my.setmore.com/js/iframe ...

Building an HTML table using arrays and JSON information

I am looking to generate an HTML table... Here is the code snippet: $result = curl_exec($ch); curl_close($ch); $json = json_decode($result); foreach ($json->data->reservations as $element) { print_r($element); }// Upon running this, I received ...

How do I dynamically incorporate Tinymce 4.x into a textarea?

I am encountering a small issue when trying to dynamically add tinymce to a textarea after initialization. tinymce.init({ selector: "textarea", theme: "modern", height: 100, plugins: [ "advlist autolink image lists charmap print p ...

Create a responsive DIV element within a website that is not originally designed to be responsive

I am looking to optimize the positioning of an ad div on my non-responsive website. The current setup has the ad displayed at 120x600 pixels, always floating to the right of the screen. While this works well for desktop or large devices, the display become ...

The HTML canvas drawImage method overlays images when the source is modified

Trying to implement a scroll animation on my website, I came across a guide for creating an "Apple-like animation" using image sequences. Even though I'm new to Angular, I attempted to adapt the code to work with Angular. However, instead of animatin ...

How to transfer a property value from a pop-up window to its parent window using JavaScript

In the parent window, when a button is clicked, the page content is stored in an object. Simultaneously, a pop-up window with a radio button and a save button opens. Once a radio button is clicked and saved, the goal is to send the radio button value back ...

Is there a way to ensure a Javascript alert appears just one time and never again?

I struggle with Javascript, but I have a specific task that needs to be completed. I am participating in a school competition and need an alert to appear only once throughout the entire project, not just once per browsing session. The alert will inform ...

Interacting with HTML elements in Java programming

I am facing a challenging situation that requires some brainstorming. I would greatly appreciate any advice or guidance in the right direction. My goal is to incorporate a Google Map into my Java Swing project, with the map being specified as a URL wit ...

Dynamically loading CSS files in an Angular 17 application during runtime

I have a specific need where numerous CSS files are stored on a public CDN server. My Angular 17 application requires the ability to extract a value from a query parameter and then use that value to locate the corresponding CSS file on the CDN server in o ...

Issues with HTML5 audio playback

My website features an HTML5 audio player which suddenly stopped working in Chrome. Strangely enough, it works perfectly fine on all other browsers. <audio controls> <source src="http://www.mywebsite.com/path/2015-09-27.mp3" type="audio/mpeg ...

How can I center an image next to text on two lines, that is compatible with IE7?

I am trying to align an image (logo) with text in two lines - the website title and a brief description. Here is my attempt using HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional ...

Tips for enhancing the color saturations of cells that overlap in an HTML table

My goal is to enhance the color of overlapping cells in my HTML tables. For instance, when I click on cell 2, the .nextAll(':lt(5)') method will change the class of the next 4 cells. https://i.stack.imgur.com/mwv8x.png Next, clicking on cell 3 ...

Box-sizing:border-box causing CSS padding problem in Firefox

Something strange is happening in Firefox, it seems like the debug console is not telling the truth or there's something I'm not seeing. Here's the CSS for the body tag: html, body { width: 100%; } body { padding: 5% 10% 0 10%; ...

Load the div first before delaying the load of the next element using jQuery

Hey there! I'm currently working on a gallery with left-floated divs, where each picture serves as the background for the div. What I'd like to achieve is having each picture load with a slight delay; meaning the first picture loads, then waits f ...

Is there a way to adjust the label elevation for a Material UI TextField component?

I am trying to enhance the appearance of a textfield label, but I am facing some challenges with my code. textStyle: { backgroundColor: '#1c1a1a', border: 0, borderRadius: 0, width: 400, height: 66, display: 'flex&a ...

The Bootstrap nav-tab functions perfectly on a local server, but unfortunately does not work when hosted remotely

UPDATE: Issue resolved so I have removed the Github link. It turns out that Github pages require a secure https connection for all linked scripts. Always remember to check the console! I encountered an unusual bug where the Bootstrap nav-tab functionality ...