Tips for removing a background image on the current page

These codes consist of two parts - the first part operates only on the #logo page.

$('#logo').on('click', function() {
// Retrieve home information
$.get('ajax.php', {page: 'home'}, function(data) {
    result = $.parseJSON(data);     
    // Reset background
    $('#content-area').backstretch(result.background);      
    // Reset navigation
    $('.current_page_item_green').removeClass('current_page_item_green');
    $('.current_page_item').removeClass('current_page_item');
    $('.nav-link').each(function() {
        $(this).removeClass('green');
    });     
    // Fade out the footer
    $('#footer-row').fadeIn();      
    // Reset copy
    $('#subnav').html('');
    $('#home-copy').html(result.copy);

    // Reset sizes and colors
    $('#home-logo').animate({height: 200}, 0);
    $('#home-copy').animate({height: 200, backgroundColor: '#004329', color: 'white', paddingTop: 0}, 0);
});});

The second part handles the left pages.

$(document).on('click', '.nav-link-ajax', function() { handleAjax($(this));});function handleAjax(eBtn) {
// Get the page we want
getPage = eBtn.attr('href');        
// Make AJAX call
$.get('ajax.php', {page: getPage}, function(data) {
    result = $.parseJSON(data);     
    // Fill in new page
    $('#subnav').html(result.subnav);
    $('#home-copy').html(result.copy);

    // Calculate document height and adjust content boxes accordingly
    docHeight = [$(document).height()-($(document).height()*.15)]-200;

    // Change height of content boxes
    $('#home-logo').animate({height: docHeight}, 0);
    $('#home-copy').animate({height: docHeight, backgroundColor: 'white', color: 'gray', paddingTop: 0}, 0);

    // Fade out the footer
    $('#footer-row').fadeOut();

    // Change background
    $('#content-area').backstretch(result.background);

    // Clear old nav
    $('.current_page_item_green').removeClass('current_page_item_green');
    $('.current_page_item').removeClass('current_page_item');

    // Update navigation
    if (result.nav_color == 'green') {
        // Add green
        $('.nav-link').each(function() {
            $(this).addClass('green');
        });
        $(result.current_page_item).addClass('current_page_item_green');
    } else {
        $('.nav-link').each(function() {
            $(this).removeClass('green');
        });
        $(result.current_page_item).addClass('current_page_item');
    }
});}

Upon returning to the logo page after visiting other webpages, there are extra spaces on the bottom and right side. It appears that the background size of the logo page is following the background of the previously visited webpage. How can I resolve this issue? Thank you.

Answer №1

It seems like the issue lies within the backstretch plugin. One major problem is that every time it's called, a new div element is added to the DOM. This is just one of the many issues with the plugin; it sets position to absolute instead of fixed, doesn't consider parent position settings, and more.

My recommendation is to completely remove it. Not just the plugin itself, but also anywhere in your code where it's being used. You can easily search for all instances of .backstretch( using your IDE (like Notepad++ or Komodo Edit) and delete those lines. Since backstretch dynamically adds the background div, there shouldn't be any need to remove HTML lines.

Just so you know, I found it referenced 4 times in main.js and once in the plugins file:
Lines [126, 152, 185, 237] in main.js
Plugin mentioned in lines 24 to 28 of plugins.js

Once you've removed it, check to ensure there are no backgrounds on any page. Double-check everything and inspect the console for any errors. If everything looks good, let's move forward. An easy way to create a background is by using a div with position fixed. Insert an img element inside and adjust the source file as needed. Here's a simple example:

HTML

<body>
    <div id="background" style="position:fixed;top:0;right:0;bottom:0;left:0;z-index:-1;">
        <img alt="no need for alt text" src="img/layout/bg_home.png" height="100%" width="100%">
    </div>
    <div ....

Then in your JS, replace the old line (which should be deleted now) of $('#content-area').backstretch(result.background); with something like this:

Replace the initial setup on line 126 of main.js with:

$('#background img').attr("src", "img/layout/bg_home.png");

For lines [152, 185, 237]

$('#background img').attr("src", result.background);

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

Interact with users on a website by using PHP to process form data

Here is the code snippet I am struggling with: <form action="profiles.php" method="POST" name="SearchSimple" id="SearchSimple" > <input name="search" id="s" style="width: 150px;" type="text"> <a style="display: inline-block; widt ...

`Send data to a C# controller function by utilizing <button> and jquery`

My bootstrap modal contains multiple buttons for downloading files in different formats. I can successfully trigger the controller method by setting the onclick function like this: onclick="location.href='@Url.Action("DownloadAsJPG", "Home")'" ...

Button for Selecting Colors

I love the color picker button on Google Keep, where a variety of colors pop up for selection. I'd like to add something similar to my website. What steps should I take to implement this feature? ...

Extracting information from a database (HTML, JavaScript)

I am currently working on a table that receives data from the server using ajax: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +d ...

Embed a script into an iframe from a separate domain

While experimenting, I attempted to inject a script inside an iframe element using the following method. However, since the child and parent elements do not belong to the same domain (and I am aware that XSS is prevented in modern browsers), I was wonder ...

Executing jQuery code after a data update in Angular can be achieved by utilizing

Currently, I am working with Angular 1.4.7 on a site that was not set up by me. As a result, I am uncertain about the specific information needed in this scenario. We are displaying search filters, but their enabling or disabling is controlled by data fetc ...

Is there a way for me to insert PHP code into a file that has been generated by PHP?

I have encountered an issue where PHP files seem to convert all PHP code into emptiness. Despite creating a PHP file, adding PHP and HTML code to it, and then closing it, the code does not appear as expected in the file. I attempted a solution which is det ...

Server vs Client-Side: Loading Dynamic HTML

Currently, I am working on a project that involves making AJAX calls to load hundreds of records from the database to be displayed on a slider. Specifically, the data I am retrieving includes the 'Image Path' for all the images, as well as other ...

Ways to verify the presence of an element in a list

I found this interesting JS code snippet: ;(function ($) { $('.filter-opts .opt').click(function(){ var selectedName = $(this).html(); $('.append').append('<li>' + selectedName + '</li> ...

Rgd: Double-Sided Horizontal Slide Control

While browsing the internet, I came across several examples of horizontal sliders with adjustable values. For example, if I set the maximum value as 100 and move the slider 10 sectors to the left, each sector would decrease by 10. What I am trying to ach ...

Stopping a div from causing the parent table's width to increase

I've encountered an interesting challenge with a project I'm working on. I need to inject markup into pages that I don't have control over, specifically within tables. The issue arises when my injected div causes the parent table to expand u ...

Is it possible to rearrange column order in Bootstrap 4x according to different viewport sizes?

One of the great things about Bootstrap is its ability to rearrange columns regardless of the HTML structure. However, I am looking to have a different order of elements when the viewport size is smaller (e.g., Mobile). The current HTML structure looks li ...

Collapse input horizontally using the display property set to flex

At the top of a mobile page, I have implemented two search boxes: https://i.sstatic.net/gCfOG.png When a user clicks inside one of the boxes, the desired behavior is as follows: The selected box expands The other box collapses horizontally A close ico ...

Using ASP.NET MVC, pass a list of values separated by commas to an action method

Hey there, I'm facing an issue with an ajax call where I am trying to retrieve values from an html select multiple tag. The problem arises when I attempt to pass these values into my controller as I keep getting a null reference error in my controller ...

Can an additional height be added to the equalizer to increase its height?

Is it feasible to append an additional 35px to the height determined by Foundation Equalizer? For instance, if Equalizer computes a height of 350px, I would like to increase it by 35px. This means that the resultant style should be height: 385px; instead ...

Issue with Firefox position: absolute not producing the desired outcome

I am working on a webpage where I want to put a label over an image. I was able to achieve this using position: absolute for the label and float: left for the img tag. This method worked perfectly in most browsers, except for Firefox (both version 3 and 4) ...

Leveraging JQuery to retrieve the string value from an onclick() event

Curious if there's a more efficient approach to tackle this issue, decided to seek input from the SO community... There's a third-party web page over which I have no control in terms of how it's displayed, but they do allow me to integrate ...

Identify Bootstrap modal to modify destination of keypress input

I am working on a piece of code that detects keypress events and focuses input towards a searchbar with the id "search". $( document ).keypress(function() { $("#search").focus(); In addition, I have a bootstrap modal dialog containing input fields ...

Troubleshooting z-index problem with background image and parent tag of image

I seem to be facing an issue with the z-index property. Here is the code snippet in question: <div id="sidebarCont"> <div id="avatarCont" class="mask"> <img id="" class="" src="img.jpg" alt=""/> </div> And here is the correspo ...

Is it possible to dynamically add attributes to XHTML tags using Javascript? For instance, adding a title attribute to an anchor tag like <a title="text">

Is it possible to modify XHTML source code through JavaScript? Can attributes be added to any XHTML tag dynamically, such as adding a title attribute to an anchor tag (<a title="text">)? ...