9th Jun '24
/
0 comments

Auto-switching Bricks Tabs

Looking to implement an auto-play/rotate feature for the Bricks‘ Tabs (Nestable) element where the tabs switch every x seconds when the element is scrolled into view?

Here’s the code for that, thanks to ChatGPT.

First, select the Tabs (Nestable) element, go to STYLE → CSS and paste this in the ‘CSS classes’ field: autoplaying-tabs

Then click on the Settings gear icon at the top → PAGE SETTINGS → CUSTOM CODE and paste this in Body (footer) scripts:

<script>
        document.addEventListener('DOMContentLoaded', function () {
            const tabsContainer = document.querySelector('.autoplaying-tabs');
            const tabTitles = tabsContainer.querySelectorAll('.autoplaying-tabs .tab-title');
            const tabPanes = tabsContainer.querySelectorAll('.autoplaying-tabs .tab-pane');
            let activeIndex = 0;
            let autoPlayInterval;

            // Helper function to handle the activation of tabs
            function activateTab(index) {
                tabTitles.forEach((title, i) => {
                    if (i === index) {
                        title.classList.add('brx-open');
                    } else {
                        title.classList.remove('brx-open');
                    }
                });

                tabPanes.forEach((pane, i) => {
                    if (i === index) {
                        pane.classList.add('brx-open');
                    } else {
                        pane.classList.remove('brx-open');
                    }
                });
            }

            // Function to kick-start the auto-play
            function startAutoPlay() {
                autoPlayInterval = setInterval(() => {
                    activeIndex = (activeIndex + 1) % tabTitles.length;
                    activateTab(activeIndex);
                }, 3000); // Switch every 3 seconds
            }

            // Function to stop the auto-play
            function stopAutoPlay() {
                clearInterval(autoPlayInterval);
            }

            // Intersection observer to detect scrolling into view
            const observer = new IntersectionObserver(entries => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        console.log('Tabs container is in view. Starting Auto-Play.');
                        startAutoPlay();
                    } else {
                        console.log('Tabs container is out of view. Stopping Auto-Play.');
                        stopAutoPlay();
                    }
                });
            }, { threshold: 0.1 });

            observer.observe(tabsContainer);
        });
    </script>

Note: The code is only meant for the case of 1 Tabs (Nestable) element per page.

Get access to all 630 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Dynamic Tabs in Bricks

Dynamic Tabs in Bricks

This Pro tutorial provides the steps to show the terms of a taxonomy as tab titles and corresponding posts as tab panes using a Tabs…
Categories:
Tags:
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:
McDonald’s Order Promise Analogy

McDonald’s Order Promise Analogy

From the JavaScript course I am doing now: A Promise is like placing an order at a restaurant. You receive a ticket (Promise object) that…
Categories:
Pro
Post Title at a Different Location on Image Hover

Post Title at a Different Location on Image Hover

This Pro tutorial provides the steps to update the text of a sticky div with the title of the post that is hovered on the…
Categories:
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…
Categories:
How to change the text of an element based on breakpoints without creating duplicated content

How to change the text of an element based on breakpoints without creating duplicated content

In this tutorial, we'll learn how to dynamically change the text of a basic text element based on the viewport width in Bricks. Introduction Sometimes…
Categories:
Enqueueing a JavaScript File in Bricks

Enqueueing a JavaScript File in Bricks

Bricks child theme's functions.php comes with code to enqueue (load) style.css on the front end. What if you want to load a custom JavaScript file…
Categories: