Lecture automatique des tabs

Avoir des tabs qui changent automatiquement sans action manuelle

Language: Javascript

Catégorie : Utilities

Favicon de Justa
<script>

var Webflow = Webflow || [];
Webflow.push(function () {
  let tabTimeout;
  clearTimeout(tabTimeout);

  let autoTime = document.querySelector('[js-tabs-auto-time]')
    ? parseInt(document.querySelector('[js-tabs-auto-time]').getAttribute('js-tabs-auto-time'))
    : 5000;

  tabLoop();

  function isNavbarOpen() {
    const navbarButton = document.querySelector('[js-tabs-navbar-button]');
    return navbarButton && navbarButton.classList.contains('w--open');
  }

  function tabLoop() {
    tabTimeout = setTimeout(function () {
      let currentLinks = document.querySelectorAll('[js-tabs-link].w--current');
      let nextLink = currentLinks.length > 0 ? currentLinks[0].nextElementSibling : null;

      if (nextLink) {
        if (isNavbarOpen()) {
          tabLoop();
        } else {
          nextLink.removeAttribute('href');
          nextLink.click();
        }
      } else {
        if (isNavbarOpen()) {
          tabLoop();
        } else {
          let firstLink = document.querySelector('[js-tabs-link]:first-child');
          firstLink.removeAttribute('href');
          firstLink.click();
        }
      }
    }, autoTime);
  }

  let tabLinks = document.querySelectorAll('[js-tabs-link]');
  tabLinks.forEach(function (link) {
    link.addEventListener('click', function () {
      clearTimeout(tabTimeout);
      tabLoop();
    });
  });
});

</script>
Sommaire
1
Le script
2
Les attributes
3
Tuto vidéo
4
Template à cloner
5
Source du script

Utiliser un LLM pour résumer ou expliquer ce script

1

Le script

Ajouter le script sur la page ou le projet Webflow

<!-- Scripts by Justa | Auto Tabs -->
<script src="https://cdn.jsdelivr.net/npm/@justaa/scripts/dist/tabs/auto-tabs.js"></script>
<script>

var Webflow = Webflow || [];
Webflow.push(function () {
  let tabTimeout;
  clearTimeout(tabTimeout);

  let autoTime = document.querySelector('[js-tabs-auto-time]')
    ? parseInt(document.querySelector('[js-tabs-auto-time]').getAttribute('js-tabs-auto-time'))
    : 5000;

  tabLoop();

  function isNavbarOpen() {
    const navbarButton = document.querySelector('[js-tabs-navbar-button]');
    return navbarButton && navbarButton.classList.contains('w--open');
  }

  function tabLoop() {
    tabTimeout = setTimeout(function () {
      let currentLinks = document.querySelectorAll('[js-tabs-link].w--current');
      let nextLink = currentLinks.length > 0 ? currentLinks[0].nextElementSibling : null;

      if (nextLink) {
        if (isNavbarOpen()) {
          tabLoop();
        } else {
          nextLink.removeAttribute('href');
          nextLink.click();
        }
      } else {
        if (isNavbarOpen()) {
          tabLoop();
        } else {
          let firstLink = document.querySelector('[js-tabs-link]:first-child');
          firstLink.removeAttribute('href');
          firstLink.click();
        }
      }
    }, autoTime);
  }

  let tabLinks = document.querySelectorAll('[js-tabs-link]');
  tabLinks.forEach(function (link) {
    link.addEventListener('click', function () {
      clearTimeout(tabTimeout);
      tabLoop();
    });
  });
});

</script>
2

Les attributes

Tab Link - Identifier tous les éléments Tab Link

Tabs Menu - Identifier le Tabs Menu (parent des Tabs Links) OPTIONNEL

Navbar - Identifier le menu button de votre Navbar OPTIONNEL

3

Tuto

4

Template

5

Code source du script

<script>

var Webflow = Webflow || [];
Webflow.push(function () {
  let tabTimeout;
  clearTimeout(tabTimeout);

  let autoTime = document.querySelector('[js-tabs-auto-time]')
    ? parseInt(document.querySelector('[js-tabs-auto-time]').getAttribute('js-tabs-auto-time'))
    : 5000;

  tabLoop();

  function isNavbarOpen() {
    const navbarButton = document.querySelector('[js-tabs-navbar-button]');
    return navbarButton && navbarButton.classList.contains('w--open');
  }

  function tabLoop() {
    tabTimeout = setTimeout(function () {
      let currentLinks = document.querySelectorAll('[js-tabs-link].w--current');
      let nextLink = currentLinks.length > 0 ? currentLinks[0].nextElementSibling : null;

      if (nextLink) {
        if (isNavbarOpen()) {
          tabLoop();
        } else {
          nextLink.removeAttribute('href');
          nextLink.click();
        }
      } else {
        if (isNavbarOpen()) {
          tabLoop();
        } else {
          let firstLink = document.querySelector('[js-tabs-link]:first-child');
          firstLink.removeAttribute('href');
          firstLink.click();
        }
      }
    }, autoTime);
  }

  let tabLinks = document.querySelectorAll('[js-tabs-link]');
  tabLinks.forEach(function (link) {
    link.addEventListener('click', function () {
      clearTimeout(tabTimeout);
      tabLoop();
    });
  });
});

</script>

Voir d'autres scripts de la même catégorie

Je veux lancer mon projet avec Justa

Nous contacter