Stop jQuery from resetting the background image when using fadeOut()

Currently, I am using fadeIn and fadeOut functions with jQuery to create a fading effect. However, when the final fade out occurs, the entire CSS table fades out and the background image of the CSS body comes into view. Despite setting the background-position to center-center, jQuery resets the image back to a position of left-bottom after the animation is completed. My goal is to maintain the background image in the center-center position.

To address this issue, I attempted to encapsulate everything within a <figure> wrapper instead of directly in the body element. Unfortunately, this resulted in the screen turning completely white once the animation finished.

Below is the HTML and jQuery code:

<html>
<head lang="en">
    <link type="text/css" rel="stylesheet" href="site.css" media="screen">
    <script type="text/javascript" src="jquery.js"></script>
    <meta charset="UTF-8">
    <script>
        $(document).ready(function() {
            $(".firstFade").fadeIn(5000).delay(10000).fadeOut(7000);
            $(".secondFade").delay(6000).fadeIn(5000).delay(3000).fadeOut(1000);
            $("#tableCell5Text").delay(14500).fadeOut(1000);
            $(".container").delay(15000).fadeOut(7000);
        });
    </script>
</head>
<body>
    <figure id="pathImg">
        <div class="container" id="table">
            <div class="container containerRow" id="tableRow">
                <div class="container containerCell" id="tableCell">
                    <img src="images/img1.jpg" alt="" width="150" height="150" class="hidden firstFade" id="img1">
                </div>
            </div>
            <div class="container containerRow" id="tableRow2">
                <div class="container containerCell" id="tableCell2">
                    <img src="images/img2.png" alt="" height="85" width="77" class="hidden firstFade" id="img2">
                </div>
            </div>
            <div class="container containerRow" id="tableRow3">
                <div class="container containerCell" id="tableCell3">
                    <p class="hidden firstFade" id="tableCell3Text">some text</p>
                </div>
            </div>
            <div class="container containerRow" id="tableRow4">
                <div class="container containerCell" id="tableCell4">
                    <h2 class="hidden secondFade" id="tableCell4Text">some text</h2>
                </div>
            </div>
            <div class="container containerRow" id="containerRow5">
                <div class="container containerCell" id="containerCell5">
                    <h1 id="tableCell5Text">some text</h1>
                </div>
            </div>
    </div>
    </figure>
</body>

CSS styling:

* {
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
}

#pathImg {
  margin: 0;
  padding: 0;
  background-image: url(images/sunnySkies.jpg);
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}

.hidden {
  display: none;
}

#table {
  display: table;
  height: 100vh;
  width: 100vw;
  text-align: center;
  background-color: #ffffff;
}

.containerRow {
  display: table-row;
}

.containerCell {
  display: table-cell;
}

#tableCell {
  height: 20vh;
}

#tableCell2 {
  height:   15vh;
  vertical-align: middle;
}

#tableCell3 {
  height: 10vh;
  vertical-align: top;
}

#tableCell4 {
  height: 5vh;
  vertical-align: top;
}

#tableCell3Text {
  font-family: 'Tangerine', cursive;
  font-size: 300%;
  color: #444444;
}

#tableCell4Text {
  font-family: arial;
  color: #444444;
}

#tableCell5Text {
  font-family: arial;
  color: #444444;
}

#img1 {
  margin: 5vh 0 0 0;
  padding: 5px;
  border: 2px solid #444444;
  border-radius: 5px;
  background-color: gray;
}

Answer №1

From my experience, everything seems to be functioning properly even without the figure element. However, if you are utilizing the figure element, it appears that the issue arises from fading out the .container class, which sets its display property to none thereby removing all height. To prevent the collapse of elements on the page, it is essential to keep something visible. This absence of height in the body eventually results in a blank screen. The solution lies in obtaining the height of the tallest image and setting the body's height accordingly before the fadeout operation.

Make the following modifications to your jQuery code:

 $(".firstFade").fadeIn(5000).delay(10000).fadeOut(7000);
 $(".secondFade").delay(6000).fadeIn(5000).delay(3000).fadeOut(1000);
 $("#tableCell5Text").delay(14500).fadeOut(1000);

 var $height = $("#table").height();
 $("body").height($height);
 $(".container").delay(15000).fadeOut(7000);

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 hovering, the <a> element does not fully encompass the background color

I am currently working on a navigation bar that changes the background color of the links when hovered. However, I encountered an issue where the dropdown menu in the navigation bar does not cover the entire area. body { margin: 0; font-family: Aria ...

Struggling to make changes to a Styled Component in a React application

In a certain file, I have a BaseComponent composed of various other components and being exported. The top level of the BaseComponent contains a wrapper responsible for managing margins. When I export it and attempt to override some styles with: //other fi ...

What is the method with the greatest specificity for applying styles: CSS or JS?

When writing code like the example below: document.querySelector('input[type=text]').addEventListener('focus', function() { document.querySelector('#deletebutton').style.display = 'none' }) input[type=text]:focu ...

Cross platform compatibility in jQuery Ajax across different browsers and operating systems

Is there a specific explanation as to why a jQuery Ajax call to the server would function correctly on Firefox for Mac but encounter issues on Firefox for PC? ...

Tips for adjusting the size of a CSS radio button while ensuring the text remains centered

Uncertain about which part to modify in this radio button code to decrease its size while keeping the text aligned with the inner button. Changes made have shifted the text to the top of the button instead. Can you provide guidance on the necessary adjustm ...

Can the placement of divs impact search engine optimization (SEO)?

My website is currently structured with div containers in the following order: Less important left side bar Less important right side bar The core center content at last I am concerned about the impact on SEO as the core content comes after the sidebars ...

Track how far visitors scroll into the inner content area using Google Tag Manager

Our website features a left-side vertical navigation menu. In order to measure scroll depth, we incorporated Tag Manager code. However, the Tag Manager code triggers on every page load due to us fixing the body scroll and implementing a custom scroll for t ...

Distinguishing Design with CSS3

Here is the code snippet I am using: <table width="562" cellspacing="0" cellpadding="0" border="0" align="center" class="genericClass"> (nested tags are here) </table> These are the related styles in my stylesheet: table.genericClass a ...

Trim the edge of a dynamic picture

Currently, I am facing a challenge with an image that requires the corner to be cut off. The image needs to be responsive in order to adjust its size according to the page dimensions. Since the image is managed by a CMS, the corner cut has to be done progr ...

"VS Code's word wrap feature is beneficial for wrapping long lines of text and code, preventing them from breaking and ensuring they are

text not aligning properly and causing unnecessary line breaks insert image here I attempted to toggle the word wrap feature, installed the Rewrap plugin, and played around with vscode settings ...

Enhance your SVG progress circle by simply selecting checkboxes

I have a unique system with 5 checkboxes that act as a To-Do list. When I click on each checkbox, the circle's diameter should progressively fill up in increments of 1/5 until all 5 checkboxes are completed. The order in which the checkboxes are click ...

What is the correct way to reference this data element in Ajax?

I am attempting to retrieve the elevation data for 732 meters from the JSON API response below: {"results":1,"data":[{"wind":{"degrees":200,"speed_kts":6,"speed_mph":7,"speed_mps":3,&quo ...

Facing an issue with an Ajax script retrieving the PHP $_GET variable

Struggling to fetch PHP $_GET variable using an Ajax script? The aim is to update a MySql table with the same script. Below is my PHP/HTML script for fetching and displaying data: session_start(); require_once ('dbconnect.php'); $query = mysql_ ...

What is the best way to retrieve JSON data using JavaScript within a loop?

I'm struggling to retrieve data from a JSON object. Here's the JSON structure I'm working with: var data = [ {"mes":{ "January":{ "Inversion":"1000","Fans":"1020"} ...

jQuery fadeIn effect happening at rapid speed

I am currently working on integrating ajax with WordPress. After the ajax call is completed, I am successfully fading out a specific div slowly. However, when trying to fade in the new data using jQuery's fadeIn() method, I noticed that regardless of ...

Learn how to set up webpack or Next.js to generate a CSS file from custom Material styles and automatically inject it into the header

One of the things I've done is define some custom material styles, like this: import {createStyles, makeStyles, Theme} from '@material-ui/core/styles'; export const useStyles = makeStyles((theme: Theme) => createStyles({ fab ...

how to show an error in a modal window when encountering an error

Using Blazor and Blazorstrap, typically when the server disconnects, an "Error" message is displayed. However, with the BsModal from Blazorstrap, it appears in the background layer, making it unresponsive. How can this be fixed? Is it possible to close the ...

Obtain the value using jQuery from an AJAX request and display it

When I write the code below, the alert output is null: var availableSlots = ""; $.get( '', { availableSlots: userName }, function(a) { availableSlots = a; }); alert(availableSlots); Putting the alert inside the get function works fine, but ...

Prevent a HTML button from being clicked multiple times before it disappears (using Django)

I am currently working on a Django project that involves events where users can join by adding their user_id and event_id to the attend model. On the event page, there is a join button form that, when clicked, adds the user to the attendees list. After cli ...

Tips for making a screen adapt to different monitor resolutions

Hello there! I've been working on a login screen using bootstrap 4, but I'm facing an issue with the layout. My concern is about adjusting the page layout so that the background image does not get cut off or distorted. Take a look at the image ...