Design6 min read
Automatic tab switching every X seconds in Webflow

Introduction
| CSS Class | Webflow Element | Role |
|---|---|---|
| tabs-menu | Tab Menu | Tab container (1st component) |
| tab-button | Tab Link | Each tab button (1st component) |
| tabs-menu-2 | Tab Menu | Tab container (2nd component) |
| tab-button-2 | Tab Link | Each 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:
- User Engagement: Motion or periodic changes can grab users' attention and encourage them to interact more with each tab's content.
- 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.
- 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

Add the “tab-button” class to each of your tab links

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

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



