Transitioning from one CSS id to another during page loading

Wondering how to fade in one CSS id on page load and then smoothly transition to another after a few seconds? Here are the ids:

#background {
    background: url("images/am_photography_bg.jpg") no-repeat center -25px fixed;
    background-size: 100%;
    height: 100vh;
}

#background-blurred {
    background: url("images/am_photography_bg_blurred.jpg") no-repeat center -25px fixed;
    background-size: 100%;
    height:100vh;
    opacity:.5;
}

This is the HTML structure:

<html>
<head>
</head>
<body>
<section>
    <div id="background"></div>
</section>
</html>

Looking for suggestions. I attempted to incorporate jQuery with this code:

$("#background").delay(1500).animate({
    background: url("images/am_photography_bg_blurred.jpg") no-repeat center -25px fixed,
    opacity: .5,
}, 1000, function() {
    // Animation complete.
});

Combining everything like this:

$(document).ready(function() {

    $('body').css('display', 'none')
    $('body').fadeIn(1500);

    $("#background").delay(1500).animate({
        background: url("images/am_photography_bg_blurred.jpg") no-repeat center -25px fixed,
        opacity: .5,
    }, 1000, function() {
        // Animation complete.
    });

    $("h1").delay(2000).animate({
        top: -30,
        opacity: 1,
    }, 700, function() {
        // Animation complete.
    });

    $('.link').click(function() {
        event.preventDefault();
        newLocation = this.href;
        $('body').fadeOut(500, newpage);
    });
});

function newpage() {
    window.location = newLocation;
}

However, encountering some issues.

Answer №1

Is this what you had in mind?

  $('#background').addClass('background');

setTimeout(function() {
  $('#background').addClass('background-blured');
}, 2000);
  .background {
background: blue url("images/am_photography_bg.jpg") no-repeat center -25px fixed;
background-size: 100%;
height: 100vh;
    }

    .background-blured {
    background: red url("images/am_photography_bg_blured.jpg") no-repeat center -25px fixed;
    background-size: 100%;
height: 100vh;
    opacity: .5;
    }

#background {
   transition: all 2s ease-in-out;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

<section>
<div id="background" class="background"></div>
</section>

Answer №2

Feel free to utilize the following code snippet:

$(document).ready(function(){
    $("#background").fadeIn();
    setTimeout(changeBackground() , 3000); //set a time after which changeBackground() will be executed.
});

function changeBackground()
{
    $("#background").fadeOut();
    $("#background-blurred").fadeIn();
}

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

Incorporating a MUI5 theme into a custom emotion-themed application

I'm currently working on an application that incorporates multiple themes. One of these is a Material UI theme specifically designed for MUI v4, while the other is an emotion theme used by non-MUI components. In my attempt to transition to MUI v5, I ...

I need to use JavaScript to create HTML to PDF and then upload the PDF file to a SharePoint Document Library

Our current project requires us to convert HTML to PDF and save it in a SharePoint Document Library. While we have successfully converted the HTML to PDF using Kendo plugin, we are facing challenges with storing it in SharePoint. It's worth noting th ...

Avoiding the default action and using a false return value do not produce the

Despite trying preventDefault, return false, and stopImmediatePropagation, the page continues to redirect back to the first selection instead of redirecting to the textarea after inputting all required fields and clicking on the button. The issue persists ...

Center an image vertically and horizontally within a div element inside a container, even if the image sizes and aspect ratio differ, while also including padding

I am looking to vertically align images with varying ratios, where the image may be larger or smaller than the content and also include padding; I have tried different solutions found here, but none seem to cover all my requirements; I specifically need i ...

Ways to accurately determine the size of an array

My issue revolves around an array of objects. When I log the array, everything appears as expected. However, when I use the .length function, it inexplicably returns a value of 0. Check out my code snippet: async fetchTicketType(updatedTicket) { awai ...

Utilize auto-suggestion feature for populating dynamically created input fields with information sourced from the

I have searched through numerous questions on stackoverflow related to my issue, but I was unable to apply any of them to solve my problem. Therefore, I am providing the files that I have been working with. I have successfully implemented autocomplete fe ...

The material UI tabs text is missing the ellipses due to a coding error

As a newcomer to web development, I am experimenting with adding an ellipsis to the span element within the tabs. <div class="MuiTabs-scroller MuiTabs-fixed" role="tablist" style="overflow: hidden;"> <div class="MuiTabs-flexContainer"> ...

Populate the content within a div element with specified height and width by utilizing JavaScript and jQuery

I need help with filling a fixed height div with long text retrieved via ajax in json format. For example, if I have a div with a height of 500px and width of 300px, with a font size of 16px. Is there a javascript recursive method that can fill the data ...

Updating the value property of an object within a loop dynamically in React

At the moment, I am utilizing an array of objects called "mainArr" as shown below, I have implemented a loop inside a function to filter object properties, but I want to dynamically replace obj.name with obj."param" based on the parameter passed. Both nam ...

Switching the default z-index for child elements within an HTML container

According to the specification, elements are typically drawn "in tree order" for in-flow, non-positioned elements of similar block level or float status and identical z-index. This means that elements declared last in the HTML markup appear on top. But wha ...

Using the clip-path property to determine the content padding within a div

I am encountering an obstacle while attempting to insert icons into a polygon-shaped div. The problem lies with the padding, as the icons get cut off by the borders of the polygon. #container { width: 200px; height: 200px; border: 2px solid blac ...

I am encountering an issue where the msal-browser login process seems to be frozen at the callback

After successfully installing the msal-browser package, I am able to log in. However, I encounter an issue where the screen gets stuck at the callback URL with a code. The samples provided in the GitHub repository demonstrate returning an access token in ...

The font sizes appear larger when styled with HTML compared to using Photoshop

When I view my <nav> element in Chrome, the font size of each list element appears much larger than it was originally designed in Photoshop. The font is displaying 23px wider in Chrome compared to Photoshop, even though my resolution is set at 72dpi ...

NodeJS: Issue when implementing try/catch with async/await causing SyntaxError

As a newcomer to Node Js, I am struggling to understand why the code below is throwing a syntax error with catch(). I recently updated to Node JS V14. Any assistance would be greatly appreciated. async function demoPromise() { try { let message ...

DxDataGrid: Implementing a comprehensive validation system for multiple edit fields

I'm currently working with a DxDataGrid within an Angular Application. Within this particular application, I have the need to input four dates. I've implemented validation rules that work well for each individual field. However, my challenge aris ...

"Trouble with the .click event on my page

Having some trouble getting a click event to work. When we use the code below, nothing happens, but if we switch out .click for .addClass, everything runs smoothly. var deeplink = window.location.hash.substr(1); if (deeplink.length < 20) { var df ...

Placing a table within a div causes the div to expand in width beyond 100%

A situation has arisen where a table with a large number of columns (30 columns) is being affected by the white-space:nowrap style set on each th tag, causing the parent div to stretch unnaturally. How can this issue be resolved and allow for a horizonta ...

Is there a way for me to view the contents within the box?

Hey there! I have a question about how to access the content inside this mysterious box "" within the style sheet. For instance, I am curious to find out the link of the image: .facebookicon::before { content: ""; } ...

Different ways to notify a React/Next.js page that Dark Mode has been switched?

I am in the process of creating my first basic Next.js and Tailwind app. The app includes a fixed header and a sidebar with a button to toggle between dark and light modes. To achieve this, I'm utilizing next-theme along with Tailwind, which has been ...

Updating dropdown menu selection after successful form submission validation

I'm currently working on a PHP sign up/registration form that retains and resubmits user input if validation fails. Although I have successfully implemented text boxes, password inputs, and radio buttons, I am encountering some challenges with the dro ...