Reputation: 1527
I'm using Bootstrap pills in my app:
<div class="container">
<ul class="nav nav-pills">
<li class="active"><a data-target="#home" data-toggle="tab">Home</a></li>
<li><a href="#about" data-toggle="tab">About</a></li>
<li><a href="#contacts" data-toggle="tab">Contacts</a></li>
</ul>
</div>
<div class="tab-content">
<div class="tab-pane active" id="home">Home</div>
<div class="tab-pane" id="about">About</div>
<div class="tab-pane" id="contacts">Contacts</div>
</div>
In order to stay on the same tab after page reload I use the following function:
$(function() {
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
localStorage.setItem('lastTab', $(this).attr('href'));
});
var lastTab = localStorage.getItem('lastTab');
if (lastTab) {
$('[href="' + lastTab + '"]').tab('show');
}
});
The problem is that after page reload, the first tab becomes active for 1 second and then it jumps to the actual active tab. Can I keep current tab without this 'jumping'?
Upvotes: 4
Views: 8406
Reputation: 1
Simple answer: just remove the ACTIVE CLASS from the default tab and its contents.
<div class="container">
<ul class="nav nav-pills">
<li><a data-target="#home" data-toggle="tab">Home</a></li>
<li><a href="#about" data-toggle="tab">About</a></li>
<li><a href="#contacts" data-toggle="tab">Contacts</a></li>
</ul>
</div>
<div class="tab-content">
<div class="tab-pane" id="home">Home</div>
<div class="tab-pane" id="about">About</div>
<div class="tab-pane" id="contacts">Contacts</div>
</div>
In the Javascript file in the script tag:
$(function() {
var lastTab = localStorage.getItem('lastTab');
if (lastTab) {
$('[data-target="' + lastTab + '"]').tab('show');
}
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
localStorage.setItem('lastTab', $(this).data('target'));
});
});
Upvotes: 0
Reputation: 42054
This line:
$('[href="' + lastTab + '"]').tab('show');
must be changed in:
$('[data-target="' + lastTab + '"]').tab('show');
You cannot refer to href but to data-target.
The same apply for: localStorage.setItem.....
The fixed code (jsfiddle):
$(function() {
var lastTab = localStorage.getItem('lastTab');
$('.container, .tab-content').removeClass('hidden');
if (lastTab) {
$('[data-target="' + lastTab + '"]').tab('show');
}
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
localStorage.setItem('lastTab', $(this).data('target'));
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container hidden">
<ul class="nav nav-pills">
<li><a data-target="#home" data-toggle="tab">Home</a></li>
<li><a data-target="#about" data-toggle="tab">About</a></li>
<li><a data-target="#contacts" data-toggle="tab">Contacts</a></li>
</ul>
</div>
<div class="tab-content hidden">
<div class="tab-pane" id="home">Home</div>
<div class="tab-pane" id="about">About</div>
<div class="tab-pane" id="contacts">Contacts</div>
</div>
Upvotes: 3
Reputation: 1527
The answer is as simple as ABC.
I just needed to remove class="active" from the first <li></li>
tag:
<li><a data-target="#home" data-toggle="tab">Home</a></li>
Upvotes: 1