JavaScript and CSS work together to create a seamless transition of background images (part two)

Thanks to the assistance of (Jan) in my previous inquiry, I successfully tackled the issue of automatic background modification.

You can view my previous question here: Automatic background change with JavaScript and CSS

However, after changing the background, there is a noticeable color transition/jump. My preference is for the color alteration to occur smoothly and without any abrupt changes.

var i = 0;
function changeBackgroundColor() {
  var background = document.getElementById("background");

  if (i % 2 === 0) {
    background.classList.remove("background-body");
  } else {
    background.classList.add("background-body");
  }
  i = i + 1;
}
setInterval(changeBackgroundColor, 3 * 1000);
html,body{
  width: 100%;
  overflow-x: hidden !important;
  height: 100%;
  overflow-y: hidden !important;
}

#background{
  width: 100%;
  height: 100%;
  background: #39c787;
  background-image: -webkit-gradient(linear, 15% 0%, 75% 84%, from(#ca83ddc4), to(#7874e3f3), color-stop(70%, #dfa450ab));
  transition: all 5s ease;
  -webkit-transition: all 5s ease;
  -moz-transition: all 5s ease;
  -o-transition: all 5s ease;
  -ms-transition: all 5s ease;
}

.background-body{
  background: #e0922d !important;
  background-image: -webkit-gradient(linear, 15% 0%, 75% 84%, from(#9283ddc4), to(#22ff12d1), color-stop(70%, #c550dfab)) !important;
  transition: all 5s ease;
  -webkit-transition: all 5s ease;
  -moz-transition: all 5s ease;
  -o-transition: all 5s ease;
  -ms-transition: all 5s ease;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body id="background"></body>

Answer №1

To create a unique design effect, you can utilize an absolutely-positioned pseudo-element with a distinct background and then apply a transition to alter its opacity.

function modifyBackground() {
  document.body.classList.toggle("custom")
  setTimeout(modifyBackground, 4000)
}
modifyBackground()
html, body {
  width: 100%;
  height: 100%;
}

body {
  position: relative;
  margin: 0;
  background-image: linear-gradient(135deg, rgba(131,58,180,1) 0%, rgba(253,29,29,1) 50%, rgba(252,176,69,1) 100%);
}

body::before {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  content: '';
  background-image: linear-gradient(135deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
  opacity: 0;
  transition: opacity 2s ease-in-out;
}

body.custom::before {
  opacity: 1;
}

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

When displaying a collection of components, clicking a button will always select the most recent element in the array

Can you explain why this code won't work in a React environment? Every time the button is clicked, it picks up the value "name" from the last element in the array. In this example, the dialog will always display the name "John2". import React from "r ...

The Grunt connect-modrewrite plugin is experiencing malfunctions

I utilized generator-angular to create my project. I am using HTML5 History. ($locationProvider.html5Mode(true).hashPrefix('!'); For URL rewrite, I am using connect-modrewrite I followed all the steps outlined in the tutorial. Below is my Gru ...

Transitioning from bootstrap 3 to 5 results in an increase in all sizes across the board

After transitioning from bootstrap 3 to 5, I noticed that all the elements on every page of my app became enlarged. The only component using bootstrap in this image is the dropdown menu with three vertical dots. https://i.sstatic.net/Ma27h.png https://i.s ...

Obtain the href attribute's value from an HTML document using Javascript

I'm facing an issue here. Currently, I am utilizing Grid.js (http://tympanus.net/codrops/2013/03/19/thumbnail-grid-with-expanding-preview/) for my project. Now, my goal is to incorporate a Lightbox with multiple thumbnails inside the dropout provide ...

Using custom class instances or objects within Angular controllers is a simple process

Using three.js, I have created a class called threeDimView that houses my scene, camera, and other elements. In my JavaScript code, I instantiate a global object of this class named threeDimView_. threeDimView_ = new threeDimView(); Now, I wish to displa ...

What is the best way to prevent the mat-options from overlapping with a fixed mat-toolbar header when scrolling?

Looking for help with Angular as a beginner. I want to create a layout with a fixed toolbar/header at the top of all components, where everything else will be rendered underneath it. However, when using mat-autocomplete and scrolling to the bottom without ...

Angular does not automatically add a class

My Angular function looks like this: $scope.show = function(el){ if($scope.steps[el] == true){ $scope.steps[el] = false; } else{ $scope.steps = []; $scope.steps[el] = true; } } When I trigger it by clicking on thi ...

What is the best way to include keys in my JSON data, rather than just values?

I've encountered an issue with the PrimeVue datatable I created, as it is only showing empty rows. After investigating, I believe the problem lies in my JSON data structure, where the fields do not have keys. What modifications should be made to stan ...

Error: Symfony encountered an uncaught type mismatch

Seeking assistance with correcting an Uncaught TypeError related to the tinyMCE editor malfunctioning. https://i.sstatic.net/vM8D5.png I have attempted potential solutions such as: - updating the tinymce installation by replacing the older reference wi ...

Accessing Row Data from a Material Table using a Button Click, Not through Row Selection

My React component features a material table view, shown here: https://i.stack.imgur.com/OUVOD.png Whenever the delete icon is clicked in the table, I want to access the rowdata associated with that particular row. However, all I am able to retrieve is ...

The Discord OAuth2 bot fails to assign roles to authenticated users

While working on OAuth2 login for my website, I encountered an issue. After a user successfully logs in through OAuth2, the bot should assign a role to them on my Discord server. However, when I tested the login process myself, the bot returned an error me ...

I have been dynamically inserting div elements into the DOM using JavaScript, using the appendChild method to add them and later removing them with removeChild. Strangely, when trying to append a div

There seems to be an issue in the code snippet below. The function create() works fine the first time, but encounters a problem on the second run at the line boxWrapper.appendChild(box);. It appears that the removeChild method is causing some disruption in ...

What is preventing me from using Selenium/Javascript to control Firefox on Ubuntu 22.04, when I am able to do so with Python?

My python script effectively controls Firefox using Selenium: from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get("https://dev.to") driver.find_element(By.CLASS_NAME, "crayons ...

The onclick handler fails to function following the injection of html using jquery AJAX

After using ajax to inject HTML into my page, the onclick handler on a specific part of that injected HTML does not seem to be functioning... Specifically, I am referring to this image... < img class="close_row" src="img/close.gif"/> In jQuery, I ...

Tips for selecting a JSON data node on-the-fly using jQuery

This is an example of my ajax function: $.ajax({ type: "GET", dataType: "json", async: false, url: "/wp-content/comment_data.php", data: 'songid=' + $array, success: function(data){ oTable.find('td').eac ...

Customizing Big Cartel's Neat theme: A guide to eliminating the blur effect on featured product images upon site activation

My website is built on Big Cartel using the Neat theme. Every time the homepage loads, the Featured products images appear blurry for about 4 seconds, especially if you're not using Chrome browser. Is there a way to remove this blur effect? Thank yo ...

The drop down menu is not activating upon selection change

When it comes to selecting the duration of stay, I am attempting to display a dropdown menu for the number of guests using jQuery. <form class="form-inline" method="get" action="#" th:action="#" id="search"> <div class="input-group input-gr ...

Webpack: transform code in web workers within the public directory

Currently, I am utilizing Next.js for my project which incorporates webpack 5 to compile TypeScript code. Within my public folder, there are several web worker scripts located at path "/workers/**/*.worker.js". I am curious if it is possible to write the ...

Replace HTML elements with AJAX and JavaScript

Working with MySQL data in pyramid presents a challenge as I need to dynamically change an HTML if statement based on the results from JS ajax calls. The main page receives data from views.py and passes it to the .mak script. The key views here are the ma ...

Variables from a node.js module

I am having trouble extracting a variable from a node.js module. My goal is to create a module that interacts with an authentication system, and it currently only returns a token. I require this token in the main.js file so that I can call other modules an ...