Is it possible to automatically loop through Bootstrap tabs like a carousel?

I am looking to create a Bootstrap tab system that automatically cycles through each tab, similar to a carousel, with a new tab showing every 10 seconds. I know this will likely require some custom JavaScript, but I'm still relatively new to it!

In addition, I would like the automatic cycling to pause if a tab is manually clicked, although I see this as more of an advanced goal.

Here is what I have tried so far: http://jsfiddle.net/59Lyhqmn/31/

I have attempted to implement solutions from similar questions, including:

$(function(){
var tabs = $('#myTab a');
    var counter = 0;
    window.setInterval(activateTab, 1000);
    function activateTab(){

       tabs.removeClass('active');
       var currentLink = tabs[counter];

       $('.tab-pane').removeClass('.active').hide();
       currentLink.addClass('active');
       $(currentLink.attr('href')).addClass('active').show();
       if(counter  < tabs.length)
         counter= counter + 1;
       else 
         counter = 0;
    }
});

However, I have not been successful with my attempts so far!

Any assistance or guidance on this matter would be highly appreciated!

Answer №1

I came across your fiddle and developed a solution using a simple iteration method. JSFiddle Link Here is the working version

    $(function(){
    var count = 0;
    setInterval(function(){
    if(count == 1){
      $("ul>li:nth-child(4)>a").removeClass("active");
      $("ul>li:nth-child(1)>a").addClass("active");
      $("#first-tab").addClass("active show in");
      $("#fourth-tab").removeClass("active show in");
    }
    else if(count == 2){
      $("ul>li:nth-child(2)>a").addClass("active");
      $("ul>li:nth-child(1)>a").removeClass("active");
      $("#second-tab").addClass("active show in");
      $("#first-tab").removeClass("active show in");
    }
    else if(count == 3){
      $("ul>li:nth-child(3)>a").addClass("active");
      $("ul>li:nth-child(2)>a").removeClass("active");
      $("#third-tab").addClass("active show in");
      $("#second-tab").removeClass("active show in");
    }
    else if(count == 4){
      $("ul>li:nth-child(4)>a").addClass("active");
      $("ul>li:nth-child(3)>a").removeClass("active");
      $("#fourth-tab").addClass("active show in");
      $("#third-tab").removeClass("active show in");
    }
    if (count == 4){count=1;}else{count+=1}
  }, 10000);
});

Answer №2

A more user-friendly solution has been developed using Bootstrap Framework 3.4 and above. The previous code did not alter the panel and could potentially create conflicts with other active navigation statuses.

Stay calm and keep coding!

       $(function(){
            var count = 0;
            setInterval(function(){
                if(count === 1){
                    $('#myTab a[href="#home"]').tab('show') // Select tab by name
                }
                else if(count === 2){
                    $('#myTab a[href="#profile"]').tab('show') // Select tab by name
                }
                else if(count === 3){
                    $('#myTab a[href="#contact"]').tab('show') // Select tab by name
                }
                else if(count === 4){
                    $('#myTab a[href="#packaging"]').tab('show') // Select tab by name
                }
                if (count === 4){count=1;}else{count+=1}
            }, 2000);
        });

Answer №3

If you happen to be utilizing bootstrap 4 or a newer version, this script may be of use:

function switchTab() {
    var tabs = $('.nav-tabs > li');
    var current = $('.nav-tabs > li').find('a.active');
    var nextTab = current.parent('li').next('li').find('a');
    if (nextTab.length === 0) {
      nextTab = $('.nav-tabs > li').first().find('a').click();
   }
    nextTab.tab('show');
}
var tabRotation = setInterval(switchTab, 5000);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Production build encountering HTML responses instead of JSON for Axios requests. (Using React, Node.js, and Heroku)

During the testing phase, while sending requests from the React frontend, I examined the type of response Axios provided on different platforms like Chrome, Firefox, and Postman. It was observed that on localhost, the response appeared as application/json, ...

Utilizing the default validation in Bootstrap 4 to ensure proper email format in form submissions

Can anyone assist with validating an email form field using Bootstrap 4 without the need for any additional libraries? I believe it might require a simple JavaScript function at the end of the document to check if the fields match. Any guidance on this mat ...

Ways to identify whether a day is in Pacific Standard Time (PST) or Pacific Daylight

While working on setting a date in node js for my server located in IST, I am trying to figure out whether the date would fall under PDT or PST time (depending on Daylight Saving Time being on or off). If my server was in PST/PDT time zone, this decision ...

Utilizing Bootstrap Icons in Rails 6 Using Webpacker Integration

Looking to incorporate Bootstrap icons from the beta version of "Official open source SVG icon library for Bootstrap" into my project. You can find it at . Currently working with Bootstrap 4 in Rails 6. After attempting different imports and includes in b ...

having difficulty preserving the edited HTML document with JSoup and Java

I am facing an issue with modifying and saving changes to an existing HTML file. I can make modifications, but when it comes to saving the changes back to the HTML file, I encounter difficulties. https://i.sstatic.net/CRhUV.jpg The specific modification ...

Legends for Jqplot Donut Chart disappearing after saving changes

I am currently working on creating a donut chart using jqplot which displays legends outside the grid. However, I have encountered an issue where when attempting to save the chart as a PNG image, the resulting image shows empty legends. var imgData = $(&a ...

What is the reason behind observing numerous post requests in the Firebug console after submitting a form through Ajax in jQuery?

I have integrated the jquery Form plugin for form submission and everything seems to be functioning properly. However, upon turning on the firebug console and clicking the submit button, I notice that there are 10 post requests being sent with the same da ...

Tips for utilizing createTextNode in JSDOM

When attempting to create a UI test for an HTML element using Jasmine and jsdom, I encountered an issue. Within my component, I utilized the createTextNode function to populate the content of the DOM element. Unfortunately, during testing, the document.c ...

Applying a trailing slash to every URL with Express.js

In my server.js file, I have implemented the following code using express js: var express = require('express'); var app = express(); var fs = require('fs'); var publicdir = __dirname + '/client'; app.set('port', 8 ...

Displaying Image Previews in Rails View Using JavaScript

I am working on a functionality where I have a text_field that contains the URL of an image. The goal is to load this URL into an img tag and update the preview image when the user changes the URL in the text field. Below is the relevant erb code for the ...

What is the best way to place a floating elastic div on top of an elastic background and ensure that the div stays in place?

Looking for some creative input here. Don't have a specific question in mind, but open to ideas. Just to clarify, I want both the floating div and the background to resize as the window size changes, while keeping the div in the same position on top ...

Manipulate a nested array in MongoDB with JavaScript array functions

Having trouble updating a field with mixed types in my mongoose schema. Although I acknowledge this may not be the ideal schema setup, there are specific reasons why it's structured this way. My goal is to perform array-like operations using JavaScrip ...

What is a sleek and stylish method to tally numbers in ascending order?

Is there a way to create a function in JavaScript or PHP that counts smoother than simply adding numbers together? For example: If we have 53 + 39, the result would instantly be 92. But instead of this instant calculation, I want the count to go from 0-1 ...

What is the best way to compare two object arrays in javascript/angularjs?

Need to merge two objects into one object array by comparing with JavaScript/AngularJS. A = [ {date: "2013-07-31", start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"}, {date: "2013-08-03", start_time:"2013-08-03 17:00:00+1 ...

Using jQuery to merge various .HTML elements and set their attributes

A series of values received through ajax are stored in this data variable, such as: $("#datset_egn").html(data['egn']); $("#datset_tabnomer").html(data['tab_nomer']); $("#datset_vhnomer").html(data['vh_nomer']); $("#datset_po ...

Unable to load app.component.html on Plnkr

Plunker link: Click here to view the Plunker https://i.sstatic.net/QXZDv.png Resource Directory: https://i.sstatic.net/yS423.png app.ts File Content: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/plat ...

Getting a specific value from a REST API array in JavaScript: Tips and tricks

After being stuck on this problem for 8 hours, I finally decided to seek help: In my JavaScript file, I am attempting to extract data from a REST API response. The response consists of an array of objects structured like this: [{"start":"2017-04-21 14:40 ...

Using Selenium and Python to scrape text from a continuously refreshing webpage after each scroll

Currently, I am utilizing Selenium/python to automatically scroll down a social media platform and extract posts. At the moment, I am gathering all the text in one go after scrolling a set number of times (see code below), but my objective is to only gathe ...

What is the best way to vertically center an item at the end of a card in Bootstrap 4?

In the process of creating a long bootstrap card containing two rows of information including a title and description, I encountered an alignment issue with the button. The button is intended to be aligned to the right of the card while remaining centered. ...

Customizing ExtJS 4.1: Mastering field overrides

Seeking guidance on applying a plugin to all fields(numberfield, textfield, datefield, etc.) within the ExtJS 4.1 library. Does anyone have suggestions on how to achieve this? I understand that all fields are derived from BaseField. I attempted the follow ...