Skip to content

Instantly share code, notes, and snippets.

@nixonmedia
Last active March 3, 2018 13:51
Show Gist options
  • Save nixonmedia/1231a332e396099c53b9d94f86ec2748 to your computer and use it in GitHub Desktop.
Save nixonmedia/1231a332e396099c53b9d94f86ec2748 to your computer and use it in GitHub Desktop.
offcanvas.js
<script>
$ = jQuery;
$(document).ready(function(e) {
// when you click and element that has the attribute data-toggle="offcanvas"
// open the sidebar
$('[data-toggle="offcanvas"]').click(function () {
//add an active class to the button
$(this).toggleClass('active');
//toggle the burger to an x
if($('i',this).hasClass('fa-times')){
$('i',this).removeClass('fa-times').addClass('fa-bars');
}else{
$('i',this).addClass('fa-times').removeClass('fa-bars');
}
//toggle an active class on the main wrapper
$('.row-offcanvas').toggleClass('active');
//match the sidebar height to the main offcanvas height
$('#sidebar').height($('.row-offcanvas-right').height());
//if the main wrapper is active
if($('.row-offcanvas').hasClass('active')){
// append the primary navigation
$('.primary').clone().appendTo('#sidebar');
//append the secondary after the primary
$('.secondary').clone().appendTo('#sidebar');
}else{
//otherwise remove anything you appended to the sidebar when it's closed
$('#sidebar .primary, #sidebar .secondary').remove();
}
$('#sidebar nav li').each(function(){
if($(this).children("ul").length){
$(this).addClass('has-children');
}
colCount = $(this).data('col-count');
$(this).on('click',function(){
console.log('click');
if($(this).attr('data-col-count') && $(this).hasClass('has-children')){
$('ul', this).toggle();
$('> a',this)
.attr('href', '#')
.click(function(e){
e.preventDefault();
})
}
});
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment