Load the image as soon as the slide comes into view using slick.js

I am aiming to achieve a specific result by utilizing slick.js. My goal is to dynamically load images only when the corresponding slide is within the viewport. Upon inspecting the console, I noticed that an active slide will be marked with the classes "slick-current slick-active".

HTML:

    <div class="container">
      <div class="slider slider-for">
        <div class="third-slide">
          <img src="media/bg3.png" />

          <div class="third-slide-menu">
            <a href="#" data-slide="3" class="subnav-current">Page1</a>
            <a href="#" data-slide="4">Page2</a>
            <a href="#" data-slide="8">Page3</a>
            <a href="#" data-slide="12">Page4</a>
            <a href="#" data-slide="15">Page5</a>
            <a href="#" data-slide="2">Page6</a>
          </div>

          <div class="third-slide-first">
            <img src="media/second-slide-left.png" />
          </div>
        </div>
       </div>
    </div>

jQ:

$('.slider-for').slick({
    vertical: true,
    slidesToShow: 1,
    arrows: false,
    speed: 0,
    swipe: false
  });
  
   $('a[data-slide]').click(function(e) {
     e.preventDefault();
     var slideno = $(this).data('slide');
     $('.slider-for').slick('slickGoTo', slideno - 1, false);
   });

With this setup, my objective is to delay the loading of the image inside the div with the class ".third-slide-first" until a few seconds after the slide with the class ".third-slide" enters the view.

Upon further investigation in the console, it was observed that this slide will be assigned the classes "slick-current slick-active" when it is in the viewport. While lazyLoad can preload the image instantly, I prefer a slight delay, potentially adding some CSS transitions for animation effects.

Answer №1

One option to consider is utilizing the afterchange function in Slick slider:

$('.slider-for').on('afterChange', function(event, slick, currentSlide, nextSlide){
    console.log(currentSlide);
});

This will provide you with the current slide number, which can be useful for handling image loading.

For more information, you can refer to this link

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

Emulate the CSS hover effect with jQuery 코드 희미한

Is there a method in jquery or any other technology that can detect event X and trigger a different event elsewhere? Currently, I am working with an image that has an image map. When a user hovers over a specific area on the map, I would like another part ...

I utilized Bootstrap to validate the form, however, despite the validation, the form is still able to be submitted. What steps can I take to

I have implemented form validation using Bootstrap's 'needs-validation'. However, I am facing an issue where the form still gets submitted even when the validation fails. What I want is for the form submission to be prevented if the validati ...

Is it possible to consolidate all HTML button functionality into a single jQuery function?

Currently, I'm in the process of developing a price calculator that adjusts the price based on the selected button. However, I'm seeking a solution where I can achieve this functionality with just one function instead of cluttering the code with ...

Dynamic divs to occupy the rest of the available vertical area

I have been experimenting with a new website layout and created a form setup. However, I am facing an issue with the fluidity of the layout. While the items are floating into position, there is a problem where if the item on the right is 400px bigger than ...

Change the image displayed in a div based on certain conditions dynamically

I am currently developing a website that is solely built using Django. I am interested in having either of two images displayed under a certain condition, similar to the example shown in this VueJs demonstration. Is it achievable without utilizing a front ...

The Jquery Object #<Object> does not have the 'getElement' method available

I've been attempting to set up this table, following the instructions here: Despite verifying that my browser is correctly pulling the CSS and .js files, I keep encountering an error related to my sortabletable.js file. (screenshot of the error) htt ...

Issue in IE with click event not firing from parent page

Currently, I am facing an issue with a filter on one of my website's pages that is designed to display various products. My goal is to set it up so that when a button from an external page is clicked, the user will be redirected to the product page wh ...

Tips for implementing the same autocomplete feature across multiple form fields

Struggling to add multiple products and provide auto-suggest for each product name? You're not alone. It seems like the auto suggest feature is only working for the first product. Can anyone point out what's going wrong here? Here's my HTML ...

CSS guidelines for handling single line breaks with the white-space property set to pre-wrap

Is it considered acceptable to include solitary carriage return characters in an HTML block styled with white-space: pre-wrap? I am working with an application that receives SOAP messages and logs them to a file. The logging process involves removing new- ...

Issue with margin-top not meeting set expectations

To assist in providing assistance, I have put together a quick jsfiddle project. I am curious why the use of margin-top for #container causes the entire body to be pushed down from html, rather than just pushing #container away from #containerWrapper. Th ...

Crafting media queries to specifically target Galaxy Nexus and Nexus 7 devices

I am faced with the challenge of testing site design on two different devices: Samsung Galaxy Nexus and Asus Nexus 7 tablet. I am struggling to target these devices individually using media queries, as I am unsure about the appropriate values for max-width ...

Update the display of the mouse pointer

I use CSS to set a custom cursor image: #scroll_up{ display: block; position: absolute; width: 960px; height: 300px; cursor: url(../imgs/cursor_up.png), auto; } The above style is assigned to a div element so that the cursor appea ...

The sticky navigation bar allows for the overlapping of additional div elements

Here is the HTML AND SCSS(SASS) code for a NAVBAR: <div id="navbar"> <ul> <li><a href="#home">Home</a></li> <li><a href="#main">Main</a></li> ...

Interacting with a PHP script utilizing jQuery

After creating a jQuery function to submit data from a textarea, an unexpected error regarding 'recievedData' being undefined occurred. Despite using firebug to debug the script, a response was visible. Below is the jQuery function in question. ...

Effortlessly creating a sine wave effect for a link underline on hover using CSS and jQuery

Here is an example: $(function(){ var x = 0; setInterval(function(){ x-=1; $(".link-wavy:hover").css('background-position', x + 'px 100%'); }, 20); }) body { font-family: 'So ...

The auto-complete feature is malfunctioning after retrieving input suggestions from a PHP file

My goal is to achieve the following: 1. Create a list of suggestions with same labels but different descriptions. 2. Upon selecting an item from the list, the input field of another item should change to the corresponding description of the selected item. ...

Retrieve the position with respect to the browser viewport

My current scenario involves two divs, with one nested within the other. The task at hand is to determine the position of the child div in relation to the browser window. Specifically, I aim to detect when the user scrolls down and have the child div fade ...

I need to adjust my navbar in Bootstrap 5 so that one <li> item floats to the left while the remaining items float to the right

Currently embarking on my inaugural html/css venture while utilizing bootstrap. I do have some previous html experience from using Blogger in the past. Issue I'm encountering is that when expanding my navbar, I aim to have the first li item (a button ...

Alignment of a Definition List with Omissions of Definitions

Often, I come across applications with forms containing optional fields. In the Show view, these fields tend to "collapse" together and lose alignment with their corresponding labels. For instance, if I have 3 fields that should be displayed as: Phone: 3 ...

What is the best way to blend the color of element 1 with the color of the body or element 2?

I am trying to overlay another element on top of the body tag in my HTML, which has already been assigned a color. I have experimented with using position relative and absolute along with z-index but haven't found a solution yet. Here's how I&ap ...