Skip to content Skip to sidebar Skip to footer

Screen Width Detection With Javascript / Jquery - Sidebar To Tabbed Pullout

I have a responsive design, with selectable sidebar data drawn from a database. At a screen width less than 751 pixels, that sidebar becomes a pull-out tab on the left of the scree

Solution 1:

The problem seemed to be faulty conditions in the if-clauses (see comments under the question).

This should do the trick:

$(window).load(function() {
  $(window).resize(function() {
    if ($(window).width() < 751) {
      if ($('.extruder.left .flap').css('display') != 'block') {
        $('#extruderLeft').closeMbExtruder();
        $('.extruder.left .flap').css('display','block'); // The tab
        $('.site_wrapper').css('padding-left','30px');
      }
    } elseif ($('.extruder.left .flap').css('display') != 'none') {
      $('#extruderLeft').openMbExtruder(true);
      $('.extruder.left .flap').css('display','none');
      $('.site_wrapper').css('padding-left','0');
    }
  }).resize();
});

Notice the extra if-clauses checking the display-state: if ($('.extruder.left .flap').css('display') != 'block') { and } else if ($('.extruder.left .flap').css('display') != 'none') {

This makes sure the sidebar/tab-switch only occurs on the break point of the specified screen-width, and the if-clauses aren't unnecessarily executed.


I also changed your script a bit to make more efficient use of jQuery. This way you don't have to create a named function AND call it twice. (I always put window.resize inside window.load instead of document.ready because if you need to scale things that only works properly after load anyway, but for your purpose both will work.)

Post a Comment for "Screen Width Detection With Javascript / Jquery - Sidebar To Tabbed Pullout"