Skip to content

How to have the navbar in Twitter Bootstrap stay fixed for screen width less than 768px

January 14, 2016
Here is some useful code for how to have the navbar in Twitter Bootstrap stay fixed for screen width less than 768px: - 

class="navbar-static-top visible-xs-block">
class="navbar-default hidden-xs">

Otherwise, use media queries to control what is visible/hidden. I don’t have any experience with Foundation, but I imagine the solution will be similar.

or, toggle between those classes using jQuery

var screen-xs = 767;

// an assumption is made here that the targeted div will have a static identifier, like class="navbar"
// this initializes the navbar on screen load with an appropriate class
if (window.innerWidth <= screen-xs) {
  $(".navbar").addClass("navbar-static-top");
} else {
  $(".navbar").addClass("navbar-default");
}

// if you want these classes to toggle when a desktop user shrinks the browser width to an xs width - or from xs to larger
$(window).resize(function() {
  if (window.innerWidth <= screen-xs) {
    $(".navbar").removeClass("navbar-default");
    $(".navbar").addClass("navbar-static-top");
  } else {
    $(".navbar").removeClass("navbar-static-top");
    $(".navbar").addClass("navbar-default");
  }
});
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: