LazyBackground() - load background image lazily for a section identified by its ID

I’m looking to implement lazy loading for a single image in a section identified by an ID, using the .lazy() method. The section has a background set through CSS. Here's the current code snippet:

HTML

<section id="showcase">
  </section>

CSS

/* Showcase*/
#showcase {
  min-height: 400px;
  background: url("../img/example.jpg") no-repeat -300px -500px;
  background-size: cover;
}

JS

<script>
  $(function() {
     $("#showcase").lazy();
  });
 </script>

How do I need to modify the code? It’s currently not functioning as expected. It seems to work only when I include the

<img class="lazy" data-src="../img/example.jpg" />
tag as shown in the example below:

Answer №1

The JQuery.Lazy documentation and demonstrations on their website suggest loading background images by specifying the data-src attribute to the element where you want the background image to appear.

In your scenario, you can follow these steps:

<section id="showcase" data-src="../img/example.jpg"></section>

Subsequently, remove the background-image declaration from the CSS style sheet.

For a live demonstration, refer to their example on JQuery.Lazy's official website.

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

"Uncovering the versatility of jQuery when selecting options with multiple values

Is it possible to check if a value of an attribute within a selector contains 'a', 'b', or 'c' in a single line? I tried using $('input[name]=a|b|c'), but unfortunately, that did not work as expected. Is there anothe ...

Eliminate unnecessary space from a Bootstrap 4 Card

Using the Card class from Bootstrap 4, I'm struggling with an extra space on the right side: https://i.sstatic.net/5qb8y.png (Please ignore the 'porno' thing, haha). You can clearly see the extra space on the right. I've been attemp ...

Using PHP to generate JSON output with and without specifying keys

I am encountering a strange bug and I'm not sure about its cause. It is possible that there is an error lurking within my code. Here is the JSON data sent by PHP (via Ajax) to be used by JQuery in the browser. This is the correct format that I desire ...

Issue: Generated fewer hooks than anticipated. This situation could be a result of an unintentional premature return statement

view image description see image here I encountered an issue while trying to retrieve a specific item from my customer list on the first attempt. The strange behavior occurs when I search for a name not present in the table, then search for an existing en ...

Generating examples of two models that are interdependent

In my Javascript form, I have implemented an AJAX POST request that successfully creates a new instance of a model called Component. Now, my goal is to allow users to input keywords for the Component model through the same form. To achieve this, I have al ...

Utilize AJAX without jQuery to toggle between different content displayed on the index page and the login page

One issue I am facing is with the setup of my index.php page, which contains a sign-up form and a switch button. The button triggers an ajax request to fetch content from login.php and updates the form. However, I'm encountering difficulties when tryi ...

Transferring form data to JavaScript for executing jQuery/AJAX request to fetch information from the database – what a journey!

Currently, I am attempting to extract a zip code from my form and then submit it to JavaScript/jQuery to retrieve data from the database associated with that specific zip code. After copying some code from w3schools and making a few modifications, as well ...

What is the best way to create a hover effect using jQuery?

How can I achieve a hover effect on my products similar to the ones on this website? I have tried using the $(".element").hover(); command, but it's not working. Can anyone help me troubleshoot this issue? ...

Creating a unique custom authentication controller in Strapi.OR

While using Strapi for my API and Back office, I've encountered a problem. Everything works fine except for one issue: I am struggling to override the controller used for the forgot password feature. Despite attempting to follow the documentation, par ...

The Facebook SDK fails to activate in Internet Explorer

I am currently working on implementing a Facebook login using the JavaScript SDK. Everything is functioning correctly in most browsers, but I am experiencing issues with certain versions of Internet Explorer. The login functionality is not working on my l ...

In JavaScript, locate the closest element in an array based on its value, avoiding cases where the value exceeds the nearest element

I am trying to find the nearest element in an array based on a given value, but I specifically want it to be the nearest one that is greater than or equal to the value. For example, if I have an array like [3000, 5000, 8000], when I search for a number bel ...

CSS disruptions occur during the deployment of Gatsby with MaterialUI components

I have a Gatsby site with MaterialUI Components, but I'm facing an issue with CSS styles being applied to the wrong components. The root of the problem seems to lie in the following code snippets: Layout.js <ThemeProvider theme={theme}> ...

Showing information from page's table using find_element_by_xpath (Selenium with Python)

I am currently exploring the implementation of the find_element_by_xpath() method to locate table elements on a webpage and present them in either a display or print format. Below is an excerpt from the page content: <tbody id="listContainer_databody" ...

Ensure that images in a browser maintain their proportions without distortion on all device screens and orientations by setting their height and width to match the

I'm working on a slideshow component in React and I want the images to dynamically adjust to fit any device screen size and orientation. The goal is for the image to resize proportionally until it reaches either the top and bottom edges or left and ri ...

Error: Disappearing textarea textContent in HTML/TS occurs when creating a new textarea or clicking a button

I've encountered an issue with my HTML page that consists of several textareas. I have a function in place to dynamically add additional textareas using document.getElementById("textAreas").innerHTML += '<textarea class="textArea"></text ...

Having trouble getting a .net webservice to function on my iPad

I have developed a basic webservice in .net [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string ActivateVendor(int ID) { var result = Business.Business.EnableRetailer(ID); return result.ToString() ...

Issue with two Jquery slider forms

Within a Jquery slider, I have implemented two distinct forms (using this specific Jquery slider: http://tympanus.net/Tutorials/FancySlidingForm/) . My goal now is to establish JavaScript/jQuery validation for these two forms separately BASED on the form ...

issue with mobile devices not properly supporting position: fixed

I'm currently working on a responsive website and have encountered an issue with the topbar. When the website switches to mobile layout, the topbar scrolls down a bit before sticking to the top of the screen as intended. This causes misalignment in th ...

Is it possible to maintain the width of an element even when its height is set to 0px?

Let me provide a detailed explanation of my issue below, but the question remains the same: Is there a way to make an element hidden or invisible with a height of 0px or slightly more than 0px while maintaining its width for functionality in Safari? Here ...

Resize image to fit within a div using Bootstrap

I'm attempting to resize some images to fit into horizontal cards created with Bootstrap-5, but I'm having trouble filling up the div completely. I've been using a main.scss file to manage the Bootstrap CSS and have tried using classes like ...