Reputation: 327
I am trying to link Bootstrap navigation items to Tabs on the same page.
The desired action:
Click on nav item > associated tab open >page scrolls down to the top of the tabs.
I have been working with the following code, but can't get it to work. I can get the tab content to shift, but cannot get the active tab to change or the page to scroll down to the tabs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Bootstrap 3 Dynamic Tabs</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type='text/javascript'>
$('#ProductDetailTabs a[data-toggle="tab"]').click(function (e) {
e.preventDefault()
$(this).tab('show')
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var divId = $(e.target).attr("href")
$('html,body').animate({ scrollTop: $(divId + 'Content').offset().top - 60 }, 500);
});
</script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">
<a data-toggle="tab" href="#sectionA" title="#sectionA" aria-controls="#sectionA">Section A</a>
</li>
<li>
<a data-toggle="tab" href="#sectionB" title="#sectionB" aria-controls="#sectionB">Section B</a>
</li>
<li>
<a data-toggle="tab" href="#sectionC" title="#sectionC" aria-controls="#sectionC">Section C</a>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<ul class="nav nav-tabs" id="ProductDetailTabs" style="padding-top:1000px;">
<li class="active"><a data-toggle="tab" href="#sectionA">Section A</a></li>
<li><a data-toggle="tab" href="#sectionB">Section B</a></li>
<li><a data-toggle="tab" href="#sectionC">Section C</a></li>
</ul>
<div class="tab-content" id="ProductDetailsContent">
<div id="sectionA" class="tab-pane fade in active">
<h3>Section A</h3>
<p>Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui. Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth.</p>
</div>
<div id="sectionB" class="tab-pane fade">
<h3>Section B</h3>
<p>Vestibulum nec erat eu nulla rhoncus fringilla ut non neque. Vivamus nibh urna, ornare id gravida ut, mollis a magna. Aliquam porttitor condimentum nisi, eu viverra ipsum porta ut. Nam hendrerit bibendum turpis, sed molestie mi fermentum id. Aenean volutpat velit sem. Sed consequat ante in rutrum convallis. Nunc facilisis leo at faucibus adipiscing.</p>
</div>
<div id="sectionC" class="tab-pane fade">
<h3>Section C</h3>
<p>WInteger convallis, nulla in sollicitudin placerat, ligula enim auctor lectus, in mollis diam dolor at lorem. Sed bibendum nibh sit amet dictum feugiat. Vivamus arcu sem, cursus a feugiat ut, iaculis at erat. Donec vehicula at ligula vitae venenatis. Sed nunc nulla, vehicula non porttitor in, pharetra et dolor. Fusce nec velit velit. Pellentesque consectetur eros.</p>
</div>
</div>
</body>
</html>
Upvotes: 1
Views: 2057
Reputation: 5537
Here's some code that might help. This is what I did:
moved your script just before the closing <body>
tag so the DOM elements that it references are loaded when the script is evaluated.
bound the click
handler to the navbar items bs-example-navbar-collapse-1
instead of the tab items
got the reference to the target tab by using event.target.hash
Hope this helps.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example of Bootstrap 3 Dynamic Tabs</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">
<a data-toggle="tab" href="#sectionA" title="#sectionA" aria-controls="#sectionA">Section A</a>
</li>
<li>
<a data-toggle="tab" href="#sectionB" title="#sectionB" aria-controls="#sectionB">Section B</a>
</li>
<li>
<a data-toggle="tab" href="#sectionC" title="#sectionC" aria-controls="#sectionC">Section C</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<ul class="nav nav-tabs" id="ProductDetailTabs" style="padding-top:1000px;">
<li class="active"><a data-toggle="tab" href="#sectionA">Section A</a>
</li>
<li><a data-toggle="tab" href="#sectionB">Section B</a>
</li>
<li><a data-toggle="tab" href="#sectionC">Section C</a>
</li>
</ul>
<div class="tab-content" id="ProductDetailsContent">
<div id="sectionA" class="tab-pane fade in active">
<h3>Section A</h3>
<p>Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui. Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache
cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth.</p>
</div>
<div id="sectionB" class="tab-pane fade">
<h3>Section B</h3>
<p>Vestibulum nec erat eu nulla rhoncus fringilla ut non neque. Vivamus nibh urna, ornare id gravida ut, mollis a magna. Aliquam porttitor condimentum nisi, eu viverra ipsum porta ut. Nam hendrerit bibendum turpis, sed molestie mi fermentum id. Aenean
volutpat velit sem. Sed consequat ante in rutrum convallis. Nunc facilisis leo at faucibus adipiscing.</p>
</div>
<div id="sectionC" class="tab-pane fade">
<h3>Section C</h3>
<p>WInteger convallis, nulla in sollicitudin placerat, ligula enim auctor lectus, in mollis diam dolor at lorem. Sed bibendum nibh sit amet dictum feugiat. Vivamus arcu sem, cursus a feugiat ut, iaculis at erat. Donec vehicula at ligula vitae venenatis.
Sed nunc nulla, vehicula non porttitor in, pharetra et dolor. Fusce nec velit velit. Pellentesque consectetur eros.</p>
</div>
</div>
<script>
$('#bs-example-navbar-collapse-1 a[data-toggle="tab"]').click(function(e) {
e.preventDefault()
$("#ProductDetailTabs [href=" + e.target.hash + "]").tab('show')
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
var divId = $(e.target).attr("href")
$('html,body').animate({
scrollTop: $(divId).offset().top - 60
}, 500);
});
</script>
</body>
</html>
Upvotes: 0