Tips for creating an animated effect on the active tab when it switches

Currently, I have a tab list containing li items. When an item is active, the li item has rounded corners and a box shadow effect. My goal is to add an animation that shows the active pill moving to its next position when it changes, but I am unsure of how to achieve this. Here's my current setup:

ul {
  display: flex;
  list-style-type: none;
  padding: 4px;
  margin: 0;
  background: #efefef;
  border-radius: 44px;
}

li span {
  display: block;
  text-align: center;
  text-decoration: none;
  padding: 7px 16px;
  transition:all 0.7s;
  &.active {
    border-radius: 50px;
    background: #ffffff;
    box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.08);
    transition:all .5s;
  }

  &:hover {
    cursor: pointer;
  }
}
<ul>
   <li class="active"><span>one</span></li>
   <li><span>two</span></li>
</ul>

If you'd like to see this in action, feel free to check out my React codesandbox here.

Answer №1

Since you've labeled the question with css, I'll share my approach based on the animation effect you're aiming for.

Although I'm not well-versed in reactjs (apologies in advance), achieving the desired "move" effect on your <a> element may be challenging (if feasible at all).

My suggestion would be to create a box below your link with similar styling to the link, positioned using absolute. By utilizing simple javascript functions triggered by link clicks, you can modify its position to appear under the other link.

Here's an example:

$(".li1").on("click", function(){
        $(".btn").attr('class', 'btn');
    $(".btn").addClass("eff1");
});
$(".li2").on("click", function(){
    $(".btn").attr('class', 'btn');
    $(".btn").addClass("eff2");
});
$(".li3").on("click", function(){
    $(".btn").attr('class', 'btn');
    $(".btn").addClass("eff3");
});
$(".li4").on("click", function(){
        $(".btn").attr('class', 'btn');
    $(".btn").addClass("eff4");
});
div {
    display:inline-block; 
    text-align:center;
    position:relative;}
ul{
    display: flex;
    list-style-type: none;
    padding: 4px;
    margin: 0;
    background: #efefef;
    border-radius: 44px;     
}
li {
  position:relative;
  z-index:1;
}
li a {
    display: block;
    text-align: center;
    text-decoration: none;
    padding: 7px 16px;
    -webkit-transition: all 0.7s;
    transition: all 0.7s;
}
.btn {
  background-color:#fff;
  border-radius: 44px; 
  height:calc(100% - 8px);
  width:90px;
  position:absolute;
  top:4px;
  left:4px;
  transition:left 0.51s ease;
}
.eff1 {left:4px;}
.eff2 {left:90px;}
.eff3 {left:180px;}
.eff4 {left:270px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
  <ul class=" XTdVG">
    <li class="li1"><a href="#">wwwww</a></li>
    <li class="li2"><a href="#">wwwww</a></li>
    <li class="li3"><a href="#">wwwww</a></li>
    <li class="li4"><a href="#">wwwww</a></li>
  </ul>
  <div class="btn"></div>
</div>

Note: if your links vary in width, remember to adjust the .eff1, .eff2, .eff3, .eff4 accordingly.

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

What is the best way to incorporate width adjustments in an image source for a responsive website?

Currently learning CSS and experimenting with it on an HTML page. For reference, I'm using this link. Encountering an issue with a responsive header image that adjusts size based on device (small on mobile, large on desktop). Is it possible to achiev ...

Set the height of the last child element to 100% in the CSS

Seeking assistance to adjust the height of the final list item to 100% in order to match the varying height of the right-floated div. ul.menu li:last-child {height:100%} Here is an example of my current situation: http://jsfiddle.net/kvtV8/1/ Any help ...

Is there a way to have two SVG files displayed on a single HTML page at the same time

Currently, I am facing an issue with my graphs. I have a line graph and a boxplot, but the boxplot is appearing below the line graph when I want it to be next to it. Any suggestions on how I can achieve this layout? Thank you! I attempted to use 2 differe ...

Steps for designing a footer with a fixed width and transparent center gap that stays in place

Looking to create a fixed footer with a transparent gap at the center that is 100% fixed width? No scripts needed! EXPAND THIS WAY <<< ______ FIXED WIDTH GAP ______ >>> EXPAND THIS WAY MY OWN SOLUTION HTML <div id="Ftr"> ...

How can I capture a screenshot of the div displaying pictures uploaded by the user on the frontend?

Take a look at this code snippet. It allows users to upload images, move them around, resize, and rotate them once uploaded. $(function() { var inputLocalFont = $("#user_file"); inputLocalFont.change(previewImages); function previewImages() { ...

In relation to the topic of navigating pages in React applications

My website is set up with routing in the startup component The main component on my site is Home: "use client"; import { useState } from "react"; import { Nav, Navbar, Sidebar } from "./views/test.js"; import "./css/app. ...

Methods for adjusting columns and rows in HTML5 tables

As I was preparing to create a compatibility guide, I noticed that the HTML code consisted of a table. While tables are effective for displaying data, they can be cumbersome to edit frequently. What tools are available to quickly and cleanly edit an exist ...

jQuery appears to be unresponsive or inactive

I'm trying to implement a jQuery script that will slide in a header after scrolling on the page, but for some reason, it's not working. When I reach the 3rd line, my code editor displays a !read only alert, suggesting there may be a syntax issue? ...

There seems to be an issue with the zebra striping in the rich:datatable component when using the rowClasses

I'm attempting to give my table a "zebra" color pattern for the rows, but it seems like the rowClasses attribute isn't functioning properly (edit: I don't see any colors applied to the table at all). Here is how I defined the styles: <s ...

There seems to be a malfunction with the hide and reset features, as they are

I have encountered an issue while working on hiding a series in Google Charts when clicked on the legend. The problem arises when an extra column is added to display tooltips on the bars, causing the functionality to break. Please check out the demos below ...

Text positioned in a fixed location

How can I ensure that the main content stays on the right side of the sidebar without adding margin-right? Currently, the sidebar is being covered by the body content due to its fixed position. Is there a solution to prevent this issue? .App { displa ...

Clicking on the react router does not trigger any action

I'm currently developing a Pokemon list app similar to the one in this tutorial: https://www.youtube.com/watch?v=XehSJF85F38&t=5904s However, I've encountered an issue where my routes are not functioning properly, even though I followed the ...

Implementing beans within an HTML form action allows for seamless communication between the form and

I am seeking assistance as I am unable to locate any tutorials online for the issue I am facing. How do you go about using a <bean:write....> within a <html:form....> The code I have written is: <html:form action="/restricted/client/so ...

Remove any errors as soon as the input field becomes valid

My current setup involves AngularJS with node.js. To handle errors, I have devised the following strategy: node router effect.js: router.post('/', function(req, res, next){ req.checkBody('name', 'Eff ...

Uploading multiple files simultaneously in React

I am facing an issue with my React app where I am trying to upload multiple images using the provided code. The problem arises when console.log(e) displays a Progress Event object with all its values, but my state remains at default values of null, 0, and ...

The function this.someFunction does not exist

Even though I have gone through the details of the bind requirement for methods to be associated with a React ES6 class, I am still facing challenges with this specific example: class ProductList extends React.Component { constructor(props) { super( ...

When clicking on links that are not within the ng-view element, you will be redirected to the

I'm having trouble coming up with a name for this question, but here's my current situation This is the structure of my template: ├── index.html ├── ... ├── account │ ├── index.html │ ├── authorizat ...

In MUI v5, the Autocomplete default value is not set

When I try to use the defaultValue prop in the Autocomplete component of MUI v5, the value always ends up being undefined. This is a snippet from my code: const vehicles = [ { name: "Toyota", model: "Camry" }, { name: "Ford&qu ...

Executing a Python script within an HTML page using Django and extracting a variable from an input form

I have created a sentiment analysis Python script, but now I want to integrate it with an HTML page. The goal is to take input from the HTML form, process it using the Python script, and then display the results back on the HTML page. Currently, I am usin ...

Having trouble deleting a component from an array in React?

I am facing an issue with my array and component functions. Each component has a handleRemove function that is supposed to remove the selected component from the array. However, the function is not working as expected. Instead of deleting just the selected ...