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 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

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:
Pro
Post Titles and Post Content Tabs in Bricks

Post Titles and Post Content Tabs in Bricks

How to set up Nestable tabs with post titles as the tab menu, and post content and/or any other post-specific data as the tab content.
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:
Pro
Switching Tabs on Hover in Bricks

Switching Tabs on Hover in Bricks

This Pro tutorial shows how to make the tabs of Tabs (Nestable) elements active on hover in Bricks. Step 1 Add one or more Tabs…
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:
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:
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
How to Add a Live Search to Filter Posts Grouped by Categories in Tabs

How to Add a Live Search to Filter Posts Grouped by Categories in Tabs

This Pro tutorial builds on top of the earlier Dynamic Tabs in Bricks tutorial and provides steps to add a live search input for filtering…
Categories:
Tags: