I'm struggling with adding a jQuery scroll function to nav-tab
(Bootstrap 3). I want the user to be able to select the tab they want and within the tab content, have a link which smoothly scrolls to an anchor. This is my code probably with loads of errors:
<div class="container_navtab">
<script>
$('#myTab a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})
$('#myTab a[href="#profile"]').tab('show') // Select tab by name
$('#myTab a:first').tab('show') // Select first tab
$('#myTab a:last').tab('show') // Select last tab
$('#myTab li:eq(2) a').tab('show') // Select third tab (0-indexed)
</script>
<ul class="nav nav-tabs">
<li><a href="#buildingcontracts" data-toggle="tab">Bulding Contracts</a></li>
<li><a href="#landscaping" data-toggle="tab">Landscaping</a></li>
<li><a href="#maintenance" data-toggle="tab">Maintenance</a></li>
<li><a href="#consultations" data-toggle="tab">Consultations</a></li>
<li><a href="#casestudies" data-toggle="tab">Case Studies</a></li>
</ul>
</div>
<div class="tab_container">
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane fade in active" id="buildingcontracts"><p>Building Contracts:
Introduction The company has successfully operated in the Hull and surrounding areas for the past ten years working on both small and large scale construction, repair, and alteration projects. With the business boom that is occurring in our local area and the desire to improve overall profit margins, the company is planning to shift its target
<a href="services.html#build" >Take me there..</a></p></div>
I found this jQuery function here Smooth scrolling when clicking an anchor link, but not sure where to place it.
var $root = $('html, body');
$('a').click(function() {
var href = $.attr(this, 'href');
$root.animate({
scrollTop: $(href).offset().top
}, 500, function () {
window.location.hash = href;
});
return false;
});
` tag. After you load jQuery. You could alternatively put your js inside your `
` but then the rest of the document won't load until your js finishes loading, so placing it at the end of the document is better.
– MilkyTech May 30 '14 at 15:26