A guide to implementing lazy loading using BXSlider

I have implemented an image gallery using Bxslider, but I am facing an issue with the loading time as there are more than 15 images in each slide. To address this problem, I want to load images one by one when a particular slide appears. I came across an example on how to achieve this using LazyLoad and Bxslider from here, however, I am having trouble getting it to work for me. Can anyone provide assistance?

This is the code I am currently using:

$(document).ready(function(){
    $("#moreLessons").bxSlider( { 
        startingSlide:1,
        pager: false
    });
    // trigger lazy to load new in-slided images
    $("a.bx-prev, a.bx-next").bind("click", function() {
        // extra call for lazy loading
        setTimeout(function() { $(window).trigger("scroll"); }, 100);
    });
});

Answer №1

<div class="carousel">
    <div>
        <img src="http://placekitten.com/400/200">
    </div>
    <div>
        <img class="lazyload" src="http://placekitten.com/450/200" data-src="http://placekitten.com/450/200">
    </div>
    <div>
        <img class="lazyload" src="http://placekitten.com/500/200" data-src="http://placekitten.com/500/200">
   </div>
</div>

$(function(){

    // set up the configuration for the interactive slider
    var carousel_config = {
        mode: 'horizontal', // include all relevant slider settings here
        onSlideBefore: function($slideElement){
            var $lazyload = $slideElement.find(".lazyload")
            var $source = $lazyload.attr("data-src");
            $lazyload.attr("src",$source).removeClass("lazyload");
             alert(JSON.stringify($lazyload));
        }
    }

    // initialize the carousel with the specified options
    var carousel = $('.carousel').bxSlider(carousel_config);
});

Answer №2

By adding a simple trick in your code, you can make the browser move by 1 pixel. Just include $(window).scrollTop($(window).scrollTop()+1); in the functions called by a.bx-prev and a.bx-next buttons.

To ensure that the page remains in place, you can use $(window).scrollTop($(window).scrollTop()-1); immediately after these functions are executed. This way, if there are 50 images in the slideshow, the page will not scroll down by 50 pixels at the end.

This technique can also be applied to bxslider in Drupal 7 with the lazyload module enabled. Set the loading icon to 'none', add the mentioned JavaScript calls to the callback section of bxslider settings in your view, and enjoy incorporating high-resolution images without any hassle.

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

Sort users by their data attribute using jQuery

I need to sort users based on their data attribute. Within the main div called user-append, I have a variable number of users retrieved from an ajax get request. It could be 3 users or even up to 100, it's dynamic. Here is the structure with one user ...

Update the text with jQuery and AJAX

I've written a script using jQuery and AJAX to change my button text from "Approve" to "Approved." However, I'm facing an issue where clicking on one row changes the button text for all rows. Here's the code for my button: <td> & ...

How to use Javascript to pause an HTML5 video when closed

I am new to coding and currently working on a project in Adobe Edge Animate. I have managed to create a return button that allows users to close out of a video and go back to the main menu. However, I am facing an issue where the video audio continues to p ...

Resolved issue with sticky header movement after scrolling event

I've been working on a sticky header code, but I'm having trouble achieving a smooth scroll transition. The fixed header seems to jump after just one scroll. Here is the basic HTML structure: <div class="headerWrapper"> <div id="to ...

Set the height of the div to match the length of the downward swipe

I am working on gradually revealing a div as the user swipes down on the body element. Currently, I am using jQuery along with hammer.js to detect the swipe gesture, but I need assistance in determining the distance of the swipe and adjusting the height o ...

The lower division remains static when the browser window is resized

Why does the div with class .lower not move to the bottom of the page when the screen is resized? Could this be because of its CSS property position:absolute;? Check out the code snippet here. #lower { width: 100%; position: absolute; bot ...

Restricting Checkbox Choices with JavaScript by Leveraging the forEach Function

I am developing a checklist application that limits the user to selecting a maximum of three checkboxes. I have implemented a function to prevent users from checking more than three boxes, but it is not working as expected. The function detects when an ite ...

JavaFX Animation for Charting

My FXML file contains a JavaFX AreaChart <AreaChart id="myAreaChart" onMouseClicked="#handleAreaChartMouseClicked" GridPane.columnIndex="0" GridPane.rowIndex="0" fx:id="myAreaChart" > <xAxis> <Nu ...

Tips on adjusting the size of a Materialize CSS datepicker to perfectly fit within a card

Currently, I am developing a login page in html utilizing the materialize css framework. In my design, I created a card where Login and Register are displayed as tabs within the card. However, one of the forms includes a datepicker from materialize which ...

Using Jquery for handling specific click events, then disabling all clicks with a common class name using the off method

Is there a way to assign individual handlers to clicked anchor tags and then disable all anchor tags within a div with a specific class name? <div id="x"> <a id="a">link one</a> <a id="b">link two</a> <a id="c" ...

The new version 5.1 of Bootstrap modal does not disable scrolling on the <body> element

I'm currently working on a project in Angular 11 using Bootstrap (v5.1.3). My main goal is to create a functionality where clicking on a card, similar to a blog post, will trigger a Bootstrap modal popup displaying the content of that specific post. B ...

Submit data in the manner of a curl request

Instead of using a curl command to push a file to a web server, I am attempting to create a simple webpage where I can select the file and submit it. Here is the HTML and JavaScript code I have written for this purpose: <html> <body> <i ...

Do XAML-based apps on Windows 8 provide a noticeable speed advantage over those built with HTML and CSS?

We are in the process of developing a photo-heavy Windows 8 Metro-style app and are concerned about UI performance. While choosing Objective-C over HTML for iOS was an easy decision to achieve the necessary UI performance, we are unsure about the speed com ...

Rails is being overwhelmed by an excessive number of icons being added by Bootstrap

I'm having an issue with adding a user icon to my header. When using Bootstrap, it seems to be adding more icons than I have specified in my code. Can anyone help me figure out what's going on? Thanks in advance! <ul class="nav pull-right"> ...

How can jQuery be utilized to duplicate an XML node into a separate XML document?

Is there a way to combine data from various XML sources into a single XML file using JavaScript or jQuery? I am currently iterating through the files and using $.ajax in jQuery to read each file. My goal is to select the first node, copy it, append it to m ...

What is the best method to substitute a comma and period with just a period in a textarea

Creating a text area with auto-replacing characters like straight quotes, double dashes to em dash, and double spaces to single space was simple. However, I'm facing difficulty in adding a script for replacing certain characters: word . as word. wor ...

Creating a color-changing checkbox toggle in Bootstrap 5

Is it possible to customize the color of a Bootstrap 5 switch without changing it site-wide? I want some checkbox switches to have a unique color. Most tutorials online only cover how to change colors globally or use images, but I'm hoping to achieve ...

What could be causing the loss of my <textarea> information in the $_POST submission?

I have been working on a script that takes form data and displays it in a PDF or another printable format. There is a "New Line" button at the bottom of the form, which when clicked adds a new line to the form and stores the previous line's data in an ...

PHP service encountering a failure during an AJAX call

After setting up a web service with PHP, I found that when accessing it from a rest client, the JSON response is correct. However, when calling the method from an AJAX call integrated into a Drupal site, the service response comes back as HTML, leading to ...

What is the correct way to customize colors for specific components in Material-ui?

Struggling with theming in Material-UI, particularly when it comes to customizing element colors. Some elements default to 'theme.palette.main.dark' and I want to override this behavior. For example, the TextField and SpeedDial components automa ...