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

Troubleshooting peerDependencies issue in Laravel 9 while installing ReactJS and VueJS with laravel/ui

I added Laravel/Ui to a Fresh Laravel 9 installation for setting up ReactJs I followed these commands step by step in the command line interface: composer require laravel/ui php artisan ui react npm install After running the npm install command, I en ...

Failing to verify the presence of specific text within a dropdown menu using Selenium

Previously, I successfully implemented this code, however, the HTML/CSS for the dropdown has since changed and now I am unable to get it to function correctly. Below is the structure for the dropdown code, with specific text highlighted that I am trying t ...

Tips for preventing menu flickering caused by overflow during CSS animations

I recently created a vertical menu with an interesting underline effect that appears when hovering over the menu items. Even though the snippet initially failed to execute, I discovered this cool hover effect (taken from here) which I wanted to implement: ...

Sophisticated filter - Conceal Ancestry

Check out this snippet of my HTML: <td> <a class="button" href="#"> <input id="download">...</input> </a> <a class="button" href="#"> <input id="downloadcsv">...</input> </a> </td> I am ...

Is there a way to stop TinyMCE from adding CDATA to <script> elements and from commenting out <style> elements?

Setting aside the concerns surrounding allowing <script> content within a Web editor, I am fully aware of them. What I am interested in is permitting <style> and <script> elements within the text content. However, every time I attempt to ...

Is there a way for me to update the placeholder text in my script from "CHANGE ME

Can anyone help me troubleshoot this code that's not working on my computer? I have a paragraph with the text CHANGE ME inside an element with the id "warning". I want to update this text when the login button is clicked, but it doesn't seem to ...

What is the purpose of applying the two unique class names (root and disabled) to the DOM in order for it to function correctly?

When it comes to customizing components using material-ui, the process can be a bit tricky. Here's how you can do it: const styles = { root: { '&$disabled': { color: 'white', }, }, disabled: {}, // This is i ...

Tips for adjusting the position of the second child in my navigation menu using CSS

Struggling with customizing my navigation menu in CSS, I'm seeking assistance. My goal is to have the second child element of the navigation menu on a new line instead of below the first child (refer to the image for clarification). An example of the ...

Tips for Sending <Div> Data to a Servlet

I attempted to pass the content of an entire div in a form action URL using JavaScript. However, when I try to retrieve this request parameter as a String on the servlet side, it is returning [object Object]. Below is my code for the form and JavaScript: ...

Steps to display a JSX component stored in a variable

Presently, I am implementing an if/else statement within my code to determine the content that will be displayed in the JSX element named 'signupMessage'. Subsequently, I render the contents of this 'signupMessage' element. render() { ...

Customizing HTML Select Elements

Can the HTML Select attribute be customized to have a flatter appearance? Although I can set the border to be 1px solid, the dropdown part still appears 3D and unattractive. ...

Displaying a document file on a webpage hosted by a raspberry pi

My Raspberry Pi hosts a local website that does not require an internet connection. I want the webpage to display text from a large file line by line as users access it. For example, if the text file contains: Hello How are You doing Today? The brow ...

Tips for adjusting the width of a Facebook embedded video within its container using ReactPlayer

Has anyone encountered issues with adding an embedded video to a NextJS app using ReactPlayer and Facebook videos? It seems that Facebook does not support the width="100%" attribute, as it always snaps back to 500px. Interestingly, this works wel ...

Warning: Ensure each item in a list has a unique key when

I'm relatively new to react and encountering a problem that's stumping me. Here is the react component I'm working with: import React from 'react'; import Header from './Header'; import ContestPreview from './Contes ...

Utilizing Angular controllers to access data attribute values from child elements

Today I embarked on the journey of learning AngularJs through online tutorials. Excited about my new project, I started working on creating some useful features using Angular. Here is a snippet of my progress so far: The HTML Part <div data-ng-control ...

Text fields do not show a cursor icon

On the website http://www.legrandclub.net, there are two text fields. Everything works fine in all web browsers except for Internet Explorer. When I click on one of the text fields, the cursor is not displayed, although it is possible to write text. What c ...

Tips for integrating an HTML template into a React project

I'm finding it challenging to integrate an HTML template into React. The template I am using is for an admin dashboard with multiple pages. I have successfully copied and pasted the HTML code into JSX files and fixed any syntax issues. Here's wh ...

Ways to ensure that a div's height matches its width

Is there a way to make three divs, each with the CSS property display:inline-block, have a 1:1 ratio of width to height using only CSS? Currently, the width is set to 30% and the height to 75%, causing the width to be greater than the height. #our_servi ...

React Router's nested route causes a full page reload when navigating

I have been working on setting up nested routing in React Router and here is my code: import React from 'react'; import DefaultSwitch from './components/DefaultSwitch/DefaultSwitch'; import './scss/App.scss'; const App = () ...

Every time I attempt to run the npx command, I encounter an error that says: "Error: EPERM: operation not permitted

Struggling to install the create-guten-block package using the command npx create-guten-block my-block, but unfortunately, it's not working out for me. My current node version is 10.15.1 Npm version I'm on is 6.4.1 Encountering the error shown ...