A sticky Top Bar makes the page jump up when scrolling past it with Zurb Foundation

前端 未结 4 1345
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 06:47

I am using the Zurb Foundation 4 framework for my site. I want to have a navbar that is positioned beneath a header that sticks to the top of the page when you scroll past.

相关标签:
4条回答
  • 2021-01-13 07:05

    This issue was fixed: https://github.com/zurb/foundation/issues/1993

    Answer:

    If you don't want it to jump to the top specify in data-options:

    <nav class="top-bar" data-options="scrolltop: false">
    

    or on initialization:

    $(document).foundation('topbar', {scrolltop: false});
    
    0 讨论(0)
  • 2021-01-13 07:12

    On this page: https://github.com/jordanmerrick/foundation/commit/47391710c7b6d30ad54e50f3b2526cb8f6184be1

    I found the code in foundation.topbar.js that adds padding to the body tag depending on whether top-bar is sticky or not:

    if ($('.sticky').length > 0) {
       var distance = $('.sticky').length ? $('.sticky').offset().top: 0,
           $window = $(window);
          var offst = $('nav.top-bar').outerHeight()+20;
         (".sticky").after("<div class='top-bar-sticky-padding'></div>");
         $window.scroll(function() {
           if ( $window.scrollTop() >= ( distance ) ) {
              $(".sticky").addClass("fixed");
    

    - $('body').css('padding-top',offst); }

          else if ( $window.scrollTop() < distance ) {
             $(".sticky").removeClass("fixed");
    

    - $('body').css('padding-top','0'); } }); }

    I'm not a javascript wizard - but it seems as if instead of setting offst to the height of the .top-bar, you set it to the value of .top-bar-sticky-padding directly, you can control the offset with a media query instead of forcing an offset the size of the top-bar.

    Am I wrong? I'm nervous about "hacking core" but this seems to have solved the problem for me.

    0 讨论(0)
  • 2021-01-13 07:23

    This appears to be a hard-coded "feature" in Foundation 4's Javascript. Instead of merely preventing the default behavior of the link, it automatically forces the link to redirect to #, which causes the browser to jump to the top of the page. This appears to be intentional (more on that in a second).

    The only alternative for now is to just not use the Top Bar component and create your own navigation using other, more reliable Foundation components. For instance, you can build your own navigation easily enough using both the .sticky class and simply defining a fresh <nav> element with all necessary <ul> menu items within.

    The Top Bar has a very specific use by design... clicking "Menu" will use Javascript to reveal the navigation as a single column of options under the Top Bar. To ensure that mobile users can scroll a big set of options, this jumps the window to the top of the page and removes the fixed behavior until you close the menu. This works well enough when your Top Bar starts at the top of the page, but has serious implications when it doesn't (for instance, scrolling to the top of the page might move the menu out of view).

    Now, this is purely opinion... but I'm really not a fan of Foundation's Top Bar implementation. Usability tests have shown that putting your mobile menus in the footer and linking to them with an anchor are more efficacious and user-friendly. You can use .hide-for-small to hide your desktop menu items and .show-for-small to reveal your own custom, anchored "Menu" link... that menu link would anchor to a mobile-specific menu in your footer (which again, you would reveal with .show-for-small).

    Long story short: Top Bar is sloppy from a usability standpoint and broken (by design) for your use-case. I'd recommend building your own sticky menu :-)

    0 讨论(0)
  • 2021-01-13 07:25

    Remove the "sticky" class if you don't need it. Worked for me.

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