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 524 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 your comment

 

Related Tutorials..

Pro
Custom Meta Box Thumbnail Slider in WordPress

Custom Meta Box Thumbnail Slider in WordPress

This Pro tutorial provides the steps to set up an image and YouTube video thumbnail slider in WordPress using Splide and Meta Box. The site…
Categories:
Tags:
Opening RSS Block’s Links in New Tabs

Opening RSS Block’s Links in New Tabs

Looking to have links output by RSS Block (in the block editor) in new tab/window when using Bricks? Add this in your Bricks template/Page at…
Categories:
How to Create a Grid Slider in Bricks

How to Create a Grid Slider in Bricks

In this tutorial, we'll learn how to create multiple rows and columns inside the default nested slider of Bricks. Introduction A user recently asked in…
Categories:
How to import custom posts with featured images and custom fields by fetching an API using a single AJAX call

How to import custom posts with featured images and custom fields by fetching an API using a single AJAX call

In this tutorial, we will import a list of movies - including images and custom fields - inside a custom post type by fetching a…
Categories:
Tags:
Pro
Bricks Slider Images From ACF Flexible Content Sub Field

Bricks Slider Images From ACF Flexible Content Sub Field

Consider the following field group associated with Pages when using ACF Pro: "Page Fields" field group|_"Page Content" Flexible Content field|__"Layout 1" layout|___"Title" Text field|___"Layout 1…
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
[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

Setting up a sticky section that fades in when scrolling down and fades away when scrolled to the top.
Categories: