Activate the tab in the center of the tab selection when clicked, utilizing either JavaScript or CSS

Attempting to display the active tab in the middle of a list of tabs using only pure javascript, without relying on the document.

List of Tabs:

.tablist li:nth-of-type(1) { order: 3; }
.tablist li:nth-of-type(2) { order: 2; }
.tablist li:nth-of-type(3) { order: 5; }
.tablist li:nth-of-type(4) { order: 4; }
.tablist li:nth-of-type(5) { order: 1; }
.tablist li.tabs.tab-active{ order: 3 !important }
<ul class="tablist">
<li class="tabs">Marketing</li>
<li class="tabs">Manufacturing</li>
<li class="tabs tab-active" >Municipalities</li>
<li class="tabs">Retail</li>
<li class="tabs">Healthcare</li>
</ul>

Only successful with first 3 tabs, unable to position last 2 tabs correctly as they switch places at the end of the list.

Implementation is being done in Frontity, which does not support the use of document.

Attempted solutions using CSS, but no luck so far with javascript. Any assistance would be greatly appreciated.

Answer №1

Check out this javascript implementation that updates the order of tabs when one is clicked.

const tablist = document.querySelector(".tablist"),
      tabs = Array.from( tablist.querySelectorAll(".tabs")).sort((a,b) => getComputedStyle(a).order - getComputedStyle(b).order); //establish initial order

tablist.addEventListener("click", e =>
{
  if (!e.target.matches(".tabs"))
    return;

  for(let i = 0, order = 0, half = Math.round(tabs.length / 2); i < tabs.length; i++)
  {
    tabs[i].classList.toggle("tab-active", tabs[i] === e.target);
    tabs[i].style.order = (tabs[i] === e.target ? half : ++order == half ? ++order : order);
  }
});

tablist.querySelector(".tab-active")?.click(); //initialize order
.tablist li:nth-of-type(1) { order: 3; }
.tablist li:nth-of-type(2) { order: 2; }
.tablist li:nth-of-type(3) { order: 5; }
.tablist li:nth-of-type(4) { order: 4; }
.tablist li:nth-of-type(5) { order: 1; }

.tablist { display: grid; }
.tablist li.tab-active { font-weight: bold; }
.tabs { cursor: pointer; user-select: none; }
<ul class="tablist">
  <li class="tabs">Marketing</li>
  <li class="tabs">Manufacturing</li>
  <li class="tabs">Retail</li>
  <li class="tabs tab-active">Municipalities</li>
  <li class="tabs">Healthcare</li>
</ul>

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

Tips for enabling/disabling a Chrome extension through the utilization of local storage in the background page

Even after reading numerous answers on similar questions, I am still facing some difficulties. My goal is to allow the user to disable my chrome extension by simply clicking on the icon at any time. The extension is designed to run once on every page load, ...

Updating classes in a CSS style for a chosen item, and modifying additional classes for similar elements to match the selected one

How can I apply the 'selected' class to only the clicked item and remove it from all other items with similar classes? <ul class="nav-list"> <li> <a href="#"> <i class="fa-soli ...

Preventing the Vue compiler from managing static assets: A step-by-step guide

In my Vue 3 application, I have a variety of static assets that are organized in their own file structure separate from the app's source directory. Fortunately, my web server is configured to serve files from both the Vue build directory and the stati ...

Utilize the $(#id).html(content) method to populate the following column with desired content

Here is a snippet of my HTML code: <div class="row margin-top-3"> <div class="col-sm-7"> <h2>NFTs</h2> <div class="table-responsive"> <table class="table table-bordered&qu ...

Tips on creating a responsive absolute div

I'm currently working on creating a profile component with Material UI and React.js. I'm having trouble making the Avatar/profile photo and profile name div responsive. Here are some screenshots to illustrate my issue: The specific div that need ...

Vue.js component mismatch in the layout

Attempting to set up a Vue application with vuetify while incorporating layouts. As a newcomer to Vue, I may have made some beginner errors. Here is the structure of my app: main.js // The Vue build version to load with the `import` command // (runtime- ...

There is a missing dependency in the root installation of a custom node module

Currently, I have a project for an app and two separate node module projects. The dependency structure looks something like this: App { NodeModule1 { NodeModule2, ... }, ... } The issue I am facing is that instead of NodeModule2 being instal ...

How can one get rid of a sudden strong beginning?

Recently, I delved into the world of CSS animation and encountered a frustrating issue. I've tried numerous methods and workarounds to achieve a smoothly looping animation without interruptions. Despite my efforts, I have not been able to replicate th ...

Safari is experiencing a unique DOM Exception 11 error when making XHR requests

In my code, I have a function that is responsible for downloading chunks of a file using an xhr request. The function is structured like this: var blobXHR = new XMLHttpRequest(); //api.media.chunkURL() returns the correct URL for each chunk blobXHR.open( ...

Looking to execute a jQuery function as soon as the input value matches?

Can anyone help me with this coding issue? I have a code that is supposed to display the #result div when the user enters "TV" into the input field, but I need the page to refresh after entering the text. Is there a way to do this live so that the div wi ...

Fetch the Cart Id from the JSON response

I am working on capturing the cartID in a JavaScript variable from a JSON response. Here is the code I have so far that requests the cart information: function getCart(url) { return fetch(url, { method: "GET", credentials: "same-orig ...

What could be the reason for the failure of @babel/preset-react automatic transformation with Webpack?

The setup involves a Yarn monorepo with a babel.config.js that specifies the environment. React version being used is 18.1.x. I have a similar configuration that works perfectly fine in another setup, but it's failing here. I've been painstaking ...

Flexbox sets aside space for resized svg images

I am currently working on my site header and I want to implement flexbox for this purpose. I tried using justify-content: space-between; to evenly distribute the empty space between the divs. However, when I add an SVG image and scale it down to fit the ...

What is the best way to bring a list from one .js file into my main.js file?

Here is a demonstration using Ania Kubow's Climate change API. My query is: I have a lengthy list similar to this one, and I would like to store it in a separate file named "exampleList.js" and then import it into my main.js file. const newspapers = ...

What is the best method for fetching the values of a select element in React.js?

I'm struggling to retrieve the value of a selected element in a dropdown list. I've tried debugging it, but haven't been able to get the value. I attempted to console log e.target.value, but unfortunately, it didn't work. Any thoughts o ...

Tips for showing the previous value associated with a specified value in an array using JavaScript

Is it possible to retrieve the previous value of a given input from an array? If so, how can we achieve this? var theArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 14, 15]; const findValue = (arr, input) => { // To do } findValue(theArray, 15) If we provide the ...

Arrange the array to show 8 elements, ensuring to first review the likes object within the array

I am currently iterating through an array of objects in the following manner: <% for(post of posts) { %> <% if(post.likes > 5){ %> <div class="col"> <div class="ca ...

Creating several divs with an image tag within each div individually using Jade HTML pre-processor

Check out the code snippet below: <div id="cubeSpinner"> <div class="face one"> <img src="/images/Windows%20Logo.jpg" class=""> </div> <div class="face two"> <img src="/images/Turtle.jpg" class ...

What is the best way to include a new line or HTML code in this particular JavaScript function?

I am attempting to create a typewriter effect and I have everything working except for creating new lines. I've tried using the following: \n \r <br /> but it just types those characters instead of creating a new line. Here is my j ...

Disabling a button following a POST request

Is there a way to prevent multiple clicks on a button after a post request is made? I want the button to be disabled as soon as it is clicked, before the post request is executed. Below is an example of my code where the button is supposed to be disabled w ...