19th Nov '22
/
7 comments

How to sync two nestable sliders in Bricks

How to sync two nestable sliders in Bricks

In this tutorial, we’ll learn how to sync two nestable sliders in Bricks: one will be the main slider and the other one will be a thumbnail slider.

Table of Contents

Introduction

This tutorial is a follow-up to the great video of Udoro ‘Cracka’ Essien shared recently in the group. He explained how to sync two nested sliders in Bricks: one main slider and one used as a thumbnail.

This tutorial enhances the previous code shared by Cracka and allows you to:

  • set and forget the Javascript Code. Once enqueued, you don’t have to touch it again as it’s based on classes rather than IDs
  • insert multiple sync sliders on the same page
  • avoid possible conflict with other plugins/snippets
  • reduce the amount of JS calculation by debouncing the function on resize

Let’s dive into it!

The DOM tree

In order to work, we need

  • a container (or a section / block / div) with the class .sync-sliders-wrapper
  • the two sliders to be children of the container
  • the main slider needs the class .sync-sliders-main
  • the thumbnail slider needs the class .sync-sliders-thumb

The JSON settings for the Thumbnail slider

As explained by Cracka in his video, you need to insert a custom JSON object as the thumbnail slider setting options. I’ll just paste the code for reference:

{
   "type":"slide",
   "direction":"ltr",
   "keyboard":"global",
   "height":"auto", 
   "gap":10,
   "start":0,
   "perPage":"3",
   "perMove":1,
   "speed":400,
   "interval":3000,
   "autoHeight":true,
   "autoplay":false,
   "pauseOnHover":false,
   "pauseOnFocus":false,
   "arrows":false,
   "pagination":false,
   "focus": "center",
   "isNavigation": true,
   "updateOnMove": true, 
   "breakpoints":{
      "580":{
         "perPage":"2"        
      }
   }
}

The most important string here is "isNavigation": true that will set the slider as a navigation slider.

Note: There should NOT be a comma before the cursor position as it appears in the screenshot

The JavaScript Code

Enqueue the following code in your child theme or use a code snippet plugin:

document.addEventListener('DOMContentLoaded', () => {

   // Query all the wrappers
   const wrappers = document.querySelectorAll('.sync-sliders-wrapper');

   // Stop the function if there is no sync slider
   if (wrappers.length < 1) return;

   // Debounce function
   const debounce = (fn, threshold) => {
      var timeout;
      threshold = threshold || 200;
      return function debounced() {
         clearTimeout(timeout);
         var args = arguments;
         var _this = this;

         function delayed() {
            fn.apply(_this, args);
         }
         timeout = setTimeout(delayed, threshold);
      };
   };

   // Init function
   const init = (mainSliderId, thumbSliderId) => {
      const main = bricksData.splideInstances[mainSliderId],
         thumbnail = bricksData.splideInstances[thumbSliderId];

      // Mount the sync slider
      main.sync(thumbnail);

   };

   // Loop into each wrapper
   wrappers.forEach(wrapper => {

      // Set the ID of the main slider
      const mainSlider = wrapper.querySelector('.sync-sliders-main');

      // Check if the main slider class is correctly set
      if (!mainSlider) return console.log('No .sync-sliders-main class found');
      const mainSliderId = mainSlider.dataset.scriptId;

      // Set the ID of the thumb slider
      const thumbSlider = wrapper.querySelector('.sync-sliders-thumb');

      // Check if the thumb slider class is correctly set
      if (!thumbSlider) return console.log('No .sync-sliders-thumb class found');
      const thumbSliderId = thumbSlider.dataset.scriptId;

      // Run the function on load
      setTimeout(() => {
         init(mainSliderId, thumbSliderId);
      }, 0);

      // Rerun the function on resize because bricks reinit the sliders on each resize event
      window.addEventListener("resize", debounce(() => {
         init(mainSliderId, thumbSliderId);
      }, 300));
   });
});

The result

If everything worked, you should see this result on frontend:

Resources

Debounce function

https://www.freecodecamp.org/news/javascript-debounce-example/

The YouTube video of Cracka

The original JavaScript code of Cracka

https://pastebin.com/fZ6XqwEa

The JSON object of the thumbnail slider

https://pastebin.com/rrNVFxtP

Get access to all 610 Bricks code tutorials with BricksLabs Pro

7 comments

  • Dale Donnolly

    Hi Sridhar,

    I've implemented both versions of this method (Maxime & Cracka) and both work as expected.

    I am however trying to open the Main Slider Image (sync-sliders-main) in a lightbox when clicked by setting the Link To 'Lightbox' option in Bricks. The lightbox opens but as it zooms in it opens the Placeholder image > '/wp-content/themes/bricks/assets/images/placeholder-image-800x600.jpg' instead of the product / gallery image.

    How can I remedy this?

  • Gustavo Denecken

    Hi all ! It worked very good for me but... Even when the "big images" are not shown and lazy loaded, when I run the site on Google's PageSpeed, it shows "white squares" (that are the slider's images not being yet shown) telling me to use a "Proper Image Size" for those images... Is there any way to improve that and allow the images to load ONLY when the thumbnail is clicked or similar?

    Thanks !

  • Travis Reason

    Any help?

  • Travis Reason

    As far as I can tell, I've followed everything in these instructions (including watching both of Cracka's videos) but I can't figure out what I'm doing wrong. The bottom slider (thumb) will not display for me. As soon as I set the Options type to Custom and paste in the json, the thumb slider shrinks down very small, but only displays 1 slide, not 3. then, when I look on the front end there's nothing there. It's in the DOM, but not displaying.

    I've set this up in a template and here's an example post: https://spacecityfencing.northonyx.com/fencing/spring-break-camp/

    Any advice?

Leave a Reply to Sridhar Katakam (Cancel Reply)

 

Related Tutorials..

Setting Active Tab in Bricks’ Tabs (Nestable) Element

Setting Active Tab in Bricks’ Tabs (Nestable) Element

With Bricks' Nestable tab element, here's how we can set which tab is active by default.
Categories:
Pro
Post/Page-Specific Background Slideshow in Bricks using Backstretch

Post/Page-Specific Background Slideshow in Bricks using Backstretch

This Pro tutorial provides the steps to load and initialize Backstretch on the images uploaded to a post (of any post type like Pages or…
Categories:
How to disable smooth scroll in Bricks

How to disable smooth scroll in Bricks

In certain situations like using ScrollSmoother, you may be looking to disable smooth scrolling functionality that’s built into Bricks.
Categories:
How to create a dynamic infinite logo slider with ACF and Bricks

How to create a dynamic infinite logo slider with ACF and Bricks

This tutorial provides the builder settings, PHP & CSS codes that can be pasted in order to create an infinite logo slider in pure CSS…
Dynamic Post Galleries in Lightboxes using BricksExtras

Dynamic Post Galleries in Lightboxes using BricksExtras

How to show post-specific galleries as sliders using BricksExtras when a “Open Gallery” link is clicked in post cards in a posts grid.
Categories:
Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.
Pro
tsParticles in Bricks

tsParticles in Bricks

Updated on 5 Aug 2023 This Pro tutorial provides the steps to set up tsParticles, a lightweight TypeScript (uses JavaScript) library for creating particles as…
Categories:
Pro
Grid.js in Bricks

Grid.js in Bricks

This Pro tutorial provides the steps to use Grid.js in a WordPress site powered by Bricks builder. Grid.js is a lightweight and performant JavaScript library…
Categories: