Skip to content

Design6 min read

Automatic tab switching every X seconds in Webflow

Introduction

CSS ClassWebflow ElementRole
tabs-menuTab MenuTab container (1st component)
tab-buttonTab LinkEach tab button (1st component)
tabs-menu-2Tab MenuTab container (2nd component)
tab-button-2Tab LinkEach tab button (2nd component)

Automatically switching tabs every X seconds can serve a few different purposes.

Here are a few reasons you might do it:

  1. User Engagement: Motion or periodic changes can grab users' attention and encourage them to interact more with each tab's content.
  2. Content Highlighting: If you have multiple offers or pieces of information you want to showcase, automatic tab switching can make sure each one gets equal attention from visitors.
  3. Limited Space: If you have limited space on your website to display information, using tabs with automatic switching can be an effective way to show more content in a small area.

Here's how to do it:

Add a tabs element

You can add as many tabs as you want

automatic tab switching Webflow
automatic tab switching Webflow

Add the “tabs-menu” class to your tab menu

automatic tab switching Webflow

Add this custom code to your page before the <body> tag

If you have just one tab element

If you're using multiple tabs elements on your page

Make sure you use:

  • .tabs-menu-2
  • .tab-button-2

for the classes on your second tabs element

<script>
	var Webflow = Webflow || [];
	Webflow.push(function () {

		// Safari fix
		function fixForSafari(selector) {
			if (navigator.userAgent.includes("Safari")) {
				document.querySelectorAll(selector).forEach((t) => (t.focus = function() {
					const x = window.scrollX, y = window.scrollY;
					const f = () => {
						setTimeout(() => window.scrollTo(x,y), 1);
						t.removeEventListener("focus", f);
					};
					t.addEventListener("focus", f);
					HTMLElement.prototype.focus.apply(this, arguments);
				}));
			}
		}

		function tabLoop(menuSelector, btnSelector, interval) {
			var tabTimeout;
			clearTimeout(tabTimeout);

			function loop() {
				tabTimeout = setTimeout(function() {
					var $next = $(menuSelector).children('.w--current:first').next();

					if($next.length) {
						$next.click();  // a user click resets the timeout
					} else {
						$(btnSelector + ':first').click();
					}
				}, interval); 
			}

			$(btnSelector).click(function() {
				clearTimeout(tabTimeout);
				loop();
			});

			loop();
		}

		// Apply the functions
		fixForSafari(".tab-button");
		tabLoop('.tabs-menu', '.tab-button', 5000); // 5-second rotation

		// For the second component
		fixForSafari(".tab-button-2");
		tabLoop('.tabs-menu-2', '.tab-button-2', 5000); // 5-second rotation

	});
</script>
<script>
// For the second component		fixForSafari(".tab-button-2");		tabLoop('.tabs-menu-2', '.tab-button-2', 5000); // 5-second rotation
</script>

Change the rotation duration

All you need to do is change the delay (in milliseconds) in the code here:

<script>
}, 5000); // 5-second rotation
</script>

Publish and test the result

Published on Apr 10, 2025

Sandro DA SILVA

Growth Marketer, Webflow expert, and jack-of-all-trades. Sandro handles commercial operations, ensures projects run smoothly, and addresses marketing-related issues (SEO, Tracking, Copywriting, etc).

You're going to working with us, guaranteed

Ready to launch your business?

get a quote

Let’s f*****G GO !!