I have a normal bootstrap tab panel for switching blocks
When switching blocks in the navigation, the active tab is highlighted
But now when I click the background of active tabs changes instantly
Is it possible to make it so that when switching tabs, the background does not change immediately, but smoothly moves left and right, depending on which tab is needed?
Can this be done with css
or do i have to use js
?
li {
margin: 0;
display: block;
}
li:before {
display:none;
}
.nav {
border-radius: 10px 10px 0 0;
justify-content: center;
}
.nav > li > a {
margin-top: 10px;
padding: 10px 40px;
display: block;
}
.nav-tabs > li > a {
font-size: 18px;
color: black;
border: none;
text-decoration: none;
}
.nav-tabs > li > a.active {
margin: 10px 0;
color: white;
background: #A700FF;
border-radius: 10px;
}
.info {
padding: 30px;
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
<ul class="nav nav-tabs" role="tablist">
<li role="presentation"> <a href="#list1" id="list1-tab" data-bs-toggle="tab" data-bs-target="#list1" role="tab" aria-controls="list1" aria-selected="true" class="active">List 1</a> </li>
<li role="presentation"> <a href="#list2" id="list2-tab" data-bs-toggle="tab" data-bs-target="#list2" role="tab" aria-controls="list2" aria-selected="false" class="">List 2</a> </li>
<li role="presentation"> <a href="#list3" id="list3-tab" data-bs-toggle="tab" data-bs-target="#list3" role="tab" aria-controls="list3" aria-selected="false" class="">List 3</a> </li>
<li role="presentation"> <a href="#list4" id="list4-tab" data-bs-toggle="tab" data-bs-target="#list4" role="tab" aria-controls="list4" aria-selected="false" class="">List 4</a> </li>
</ul>
<div class="tab-content">
<div id="list1" role="tabpanel" aria-labelledby="list1-tab" class="tab-pane active">
<div class="info">List 1 info</div>
</div>
<div id="list2" role="tabpanel" aria-labelledby="list2-tab" class="tab-pane">
<div class="info">List 2 info</div>
</div>
<div id="list3" role="tabpanel" aria-labelledby="list3-tab" class="tab-pane">
<div class="info">List 3 info</div>
</div>
<div id="list4" role="tabpanel" aria-labelledby="list4-tab" class="tab-pane">
<div class="info">List 4 info</div>
</div>
</div>
3
Answers
Smooth Transition Effect for Bootstrap Tabs without JavaScript
To achieve a smooth transition effect when switching tabs without using JavaScript, you can utilize CSS transitions. In this example, we’ll animate the background color of the active tab. Here’s how you can do it:
Add the following CSS rule to handle the transition effect:
css
The transition property specifies the CSS property (background-color in this case) that will be animated, the duration of the transition (0.3 seconds), and the easing function (ease for a smooth transition).
Modify the CSS rule for the active tab to set the background color directly without the transition effect:
css
By adding transition: none; to the active tab rule, it ensures that the background color changes instantly without the transition effect.
Here’s the updated HTML and CSS code:
HTML:
CSS:
css
Now, when you switch tabs, you should see a smooth transition of the background color between the active and inactive tabs.
I have managed to make it work using a combination of
linear-gradient
,background-position
andtransition
:Hers is a way you could do it with a little jQuery and using bootstraps
shown.bs.tab
event for detecting when the nav tab is completely shown.This will only work, if you are using
.nav-tabs
once on your page, as per your example. But if you want this to work on multiple instances of.nav-tabs
on your page, then you will need to modify this a bit.Here is your example with sliding .
nav-tab
background as per your question.See notes in jquery code so you know is happening…
Here is a jsfiddle version… https://jsfiddle.net/joshmoto/32epbqu4/4/
Also you may want to test in responsive mode and devices to check there are no problems with positioning, but as far as I can tell it is currently working well responsively too.
This script will animate nav tab background to any nav tab position or size in your document.
Hope this helps!