Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Last active October 17, 2017 13:03
Show Gist options
  • Select an option

  • Save NickDeckerDevs/7137c0346d1a006d2ebc526ba96b7a0d to your computer and use it in GitHub Desktop.

Select an option

Save NickDeckerDevs/7137c0346d1a006d2ebc526ba96b7a0d to your computer and use it in GitHub Desktop.
Moves a custom sidebar through the content and back on breakpoint.
var $sidebar_container = $('.custom-mobile-left-sidebar');
var has_three_column = $('.three-column-equal').length > 0 ? true : false;
var $content_container = $('h1').parent();
var sidebar_is_swapped = false;
function move_sidebar_if_mobile() {
var wwidth = $(window).width();
if(wwidth <= 767 && sidebar_is_swapped == false) {
sidebar_is_swapped = true;
console.log('moving sidebar to main container');
$content_container.append('<div id="sidebar-inserted">' + $sidebar_container.html() + '</div>');
$('.custom-mobile-left-sidebar').html('');
return true;
} else {
return false;
}
}
function move_sidebar_back() {
var wwidth = $(window).width();
if(wwidth >= 768 && sidebar_is_swapped) {
console.log('moving sidebar back to original space');
var $sidebar = $('#sidebar-inserted').html();
$('.custom-mobile-left-sidebar').html($sidebar);
$('#sidebar-inserted').remove();
sidebar_is_swapped = false;
}
}
$(document).ready(function() {
if($sidebar_container && has_three_column) {
move_sidebar_if_mobile();
}
});
$(window).resize(function() {
if($sidebar_container && has_three_column) {
var is_moved = move_sidebar_if_mobile();
if(!is_moved) {
move_sidebar_back();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment