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

What could be causing the state to not update as anticipated?

I am currently in the process of developing a TicTacToe game and have a requirement to maintain the current player in state under the name currentPlayer. The idea is that after one player makes a move, I need to update currentPlayer to represent the opposi ...

Is it possible to design a collapsible element that gives a sneak peek of its content before fully expanding?

In the title, I mentioned that I require the collapsible to display its content partially before expanding and then fully after expanding. To illustrate this concept, here are two drawings for reference: https://i.sstatic.net/f4CHh.png https://i.sstatic. ...

add information to a JavaScript "JSON array"

When working in JS (specifically node/js but applicable to general JS), one common issue arises. Upon receiving JSON data from the server, there is often a need to modify it before presenting it on the view. How should this be approached? Creating additi ...

Establish the lowest possible height for Angular Material Expansion Panel Content

Is there a way to specify a minimum height for the content of an Angular Material expansion panel when it is open? I have searched for examples on setting the expandedHeight and collapsedHeight for the header, but not for the content itself. The Angular Do ...

In my programming world, 'i' is a mysterious being - always undefined, except when it decides

Currently, I am utilizing Vue, Vuedraggable, and Vuetify in my project. I have encountered an issue where I am unable to use 'let' to define the index for my loop as it always returns undefined. Strangely, using 'var' instead of ' ...

Storing JSON strings in PHP differs from storing them in JavaScript

Using JavaScript, I can save a cookie using JSON.stringify(), which saves the cookie directly like this: '[{"n":"50fb0d0cc1277d182f000002","q":2},{"n":"50fb0d09c1277d182f000001","q":1},{"n":"50fb0d06c1277d182f000000","q":1}] Now, I am sending this t ...

Tips for creating a margin on an HTML button's background color

My bootstrap button has dimensions of 46 x 96px and a border radius of 22px. When hovered, I would like the background color to only extend up to 40 x 90px, without filling the entire button. I understand this can be achieved using a box-shadow, but since ...

The alignment of my Navbar Brand is off in my Navigation Bar and won't align to

<body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs ...

Searching for a method to execute actions prior to the "enter" event in ng-animate, similar to the beforeAddClass function

//trying to replicate the top animation with the enter-leave animation below app.animation('.answer-animation', function(){ return { beforeAddClass: function(element, className, done){ if (className == 'answer') { ...

Tips for customizing css for image tags

Here is the CSS code I used for images: img { border: none; max-width: 100%; height: auto !important } To override specific image styles, I added this at the bottom: .locals-list>img { height: 35px; } Below is the HTML code I am work ...

The jquery selector fails to retrieve all elements

On the upcoming web page, I am attempting to use Jquery to select all <li> elements. Specifically, I want to target all the products contained within <ul class=search-result-gridview-items">. You can find the products here: I have made attempt ...

Ways to address the problem of returning assignments

I encountered an issue while working on my code Error message: Arrow function should not return assignment no-return-assign The problematic code snippet is as follows: await DB.Place.find( match, // key to filter (err, doc) => (listOfObje ...

Unable to retrieve HTML code with PHP's file_get_contents function

I have been trying to retrieve the HTML content of a webpage using the code below: $url = "http://mysmallwebpage.com/"; $html = file_get_contents($url); Unfortunately, it seems that the file_get_contents function is unable to open URLs in certain formats ...

I am having trouble receiving responses when using mentionsInput in my Laravel project

Why am I not receiving any response while using the mentionsInput feature in my Laravel project? The data is correctly coming in JSON format, which I have verified. $(document).ready(function() { var token = $("#csrf-token").val(); $("textarea.menti ...

Listening for a client's socket emit through Express Routes

I have successfully implemented the functionality to emit and receive messages using socket.io between the server and client with the code in server.js. const express = require('express') const app = express() const port = 4000 var http = require ...

Acquiring data from a website using Python post user engagement

Hey everyone! One of my friends has a lot of typing to do for her IT classes in school. She needs to learn how to type quickly on the keyboard. Being quite lazy, she asked me if I knew of any way she could type her texts on without actually doing any wor ...

Initialization of directive failed to occur properly

Utilizing the angular-ui-bootstrap tabs directive, I tried to create tabs. However, upon logging each controller and link function, the initialization order appears incorrect. Expected Order outer - controller Inner - Controller Inner - Link Inner - C ...

The voracious nature of the `+` and `*` operators

There is a variable, const input = "B123213"; When using the following regex pattern, const reg = /\d+/; and executing String match function, console.log(input.match(reg)); The output returned is 123213, illustrating that the expression is gree ...

The concatenation function in JavaScript does not seem to be functioning properly with JSON

My attempt to use .concat() in order to combine two objects is resulting in tiles.concat is not a function The following code (in an angular app and written in coffeescript): $scope.tiles = new UI(); $scope.tiles.loadUITiles(); console.log($sco ...

What is the best way to transfer variables to a different page through a pop-up window?

I'm working with a function that converts the Id of the clicked element into a variable, then opens a new window with a different page. How can I access or utilize this variable on the newly opened page? var idToWrite = []; $(function(){ $(".szl ...