How to use the new affix plugin in twitter's bootstrap 2.1.0?

后端 未结 9 1591
梦谈多话
梦谈多话 2020-11-29 15:17

The bootstrap documentation on that topic is a little confusing to me. I want to achieve similar behaviour like in the docs with the affix navbar: The navbar is below a para

9条回答
  •  粉色の甜心
    2020-11-29 15:32

    Thanks to namuol and Dave Kiss for the solution. In my case I had a tiny problem with navbar height and width when I used afflix and collapse plugins together. The problem with width can be easily solved inheriting it from parent element (container in my case). Also I could manage to make it collapsing smoothly with a bit of javascript (coffeescript actually). The trick is to set wrapper height to auto before collapse toggle occurs and fix it back after.

    Markup (haml):

    #wrapper
      #navbar.navbar
        .navbar-inner
          %a.btn.btn-navbar.btn-collapse
            %span.icon-bar
            %span.icon-bar
            %span.icon-bar
    
          #menu.nav-collapse
            -# Menu goes here
    

    CSS:

    #wrapper {
      width: inherit;
    }
    
    #navbar {
      &.affix {
        top: 0;
        width: inherit;
      }
    }
    

    Coffeescript:

    class Navigation
      @initialize: ->
        @navbar = $('#navbar')
        @menu = $('#menu')
        @wrapper = $('#wrapper')
    
        @navbar.affix({offset: @navbar.position()})
        @adjustWrapperHeight(@navbar.height())
    
        @navbar.find('a.btn-collapse').on 'click', () => @collapse()
    
        @menu.on 'shown', () => @adjustWrapperHeight(@navbar.height())
        @menu.on 'hidden', () => @adjustWrapperHeight(@navbar.height())
    
      @collapse: ->
        @adjustWrapperHeight("auto")
        @menu.collapse('toggle')
    
      @adjustWrapperHeight: (height) ->
        @wrapper.css("height", height)
    
    $ ->
      Navigation.initialize()
    

提交回复
热议问题