How do you create a collapsable sidebar using Twitter's bootstrap

后端 未结 2 509
甜味超标
甜味超标 2021-02-09 02:40

I have been trying to create a sidebar that will expand and collapse using Twitter\'s Bootstrap project but I am not able to get it working. I am trying to use their basic cont

相关标签:
2条回答
  • 2021-02-09 02:47

    This is easily achieved… Please see the following Fiddle..

    This is the gist of it, though… Basically, the sidebar get's hidden after one second of inactivity with the mouse. You swap out the content (has sidebar) and container-fluid (no sidebar) for your main block, and hide the sidebar. Easy as that.

    function onInactive(ms, cb){ 
        var wait = setTimeout(cb, ms); 
        document.onmousemove = document.mousedown =
        document.mouseup = document.onkeydown = 
        document.onkeyup = document.focus = 
        function(){
            clearTimeout(wait);
            wait = setTimeout(cb, ms);
    }; }
    var sidebar = $('div.sidebar'); 
    var content = $('div.content');
    $('body').live('mousemove', function(event) {
        sidebar.addClass('sidebar').fadeIn();
        content.addClass('content').removeClass('container-fluid');
    });
    
    onInactive(1000, function(){
        sidebar.fadeOut(300);
        content.removeClass('content').addClass('container-fluid');
    });
    
    0 讨论(0)
  • 2021-02-09 02:56

    The problem with the layouts in Bootstrap is they aren't fluid - if you use any of their span- classes for layout, they are fixed values (look in the bootstrap.css). Now, that being said, their container-fluid class IS fluid - sort of. It has a min-width set to 940px, and when used with the sidebar class, it has a 240px margin. This is why your sidebar is not collapsing.

    You can either modify the bootstrap css (kinda defeating the purpose, IMO), or create a 'custom' stylesheet or use inline styles that override the bootstrap classes, and 'hotwire' those classes to suit your needs, without compromising the bootstrap css directly (making it easy to upgrade).

    0 讨论(0)
提交回复
热议问题