【问题标题】:How to correctly format waypoints jquery for sticky navigation without content under it 'jumping'?如何正确格式化航点 jquery 以实现粘性导航而没有“跳跃”下的内容?
【发布时间】:2026-02-15 18:35:01
【问题描述】:

这是 html 和脚本。想出了如何让粘性导航标题正常工作,但是当我尝试将 if/else 语句合并到脚本中以防止通过航点后内容“跳跃”时,问题就来了

     <div id="wrapper2">
        <!-- Header -->  
        <div id="top_header"></div> 
             <div id="header_nav">
                 <div id="header">
                <a id="header_logo" href="{$link->getPageLink('index.php')}" title="{$shop_name|escape:'htmlall':'UTF-8'}">
                    <img class="logo" src="{$img_dir}DOMS4SQRwht.png?{$img_update_time}" alt="{$shop_name|escape:'htmlall':'UTF-8'}" />
                </a>
                    <div id="header_right">
                    {$HOOK_TOP}
                    </div> 
                 </div>
             </div>

        <script type="text/javascript" src="{$js_dir}jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="{$js_dir}waypoints.min.js"></script>
        <script type="text/javascript">

        $(document).ready(function() {
            $('.top').addClass('hidden');
            $.waypoints.settings.scrollThrottle = 30;
            $('#wrapper2').waypoint(function(event, direction) {
                $('.top').toggleClass('hidden', direction === "up");
            }, {
                offset: '-100%'
            }).find('#header_nav').waypoint(function(event, direction) {
                $(this).parent().toggleClass('sticky', direction === "down");
                event.stopPropagation();
        if (direction == 'down')
          nav_container.css({ 'height':nav.outerHeight() });
        else
          nav_container.css({ 'height':'100%' });

            });
        });

        </script>

    Here's the CSS

        div#header {width:100%;margin-left:auto;margin-right:auto;height:49px;border-bottom:2px solid #ffffff;no-repeat;zoom:1;z-index:9999;background: url(../img/bg_meshdot.png)rgba(255,255,255,0.2);background-repeat:repeat;display:inline-block;}
        .sticky #header_nav {width:100%;position: fixed;top:0;margin-top:0;margin-bottom:0;box-shadow:0 0 7px rgba(0,0,0,.5);z-index:9;}
        div#top_header {width:100%;height:51px;display:inline-block;background: url(../img/bg_meshdot.png)rgba(255,255,255,0.2);position:relative;}

【问题讨论】:

    标签: navigation sticky jquery-waypoints


    【解决方案1】:

    注意:这并不完美。

    我能够通过这个实现大致相同的目标。

    $('div.checklist').waypoint({
        handler: function(event, direction) {
          var name = ".checklist";
          var menuYloc;
          var offset;
          $(window).scroll(function () {
            if(direction === "down"){
              menuYloc = 48;
            }else if(direction === "up"){
              menuYloc = 106;
            }
            offset = menuYloc+$(document).scrollTop()+"px";
            $(name).animate({top:offset},{duration:500,queue:false});
            event.stopPropagation();
          });
          event.stopPropagation();
        },
        offset: 45,
        scrollThrottle : 30
      });
    

    有一个小错误,上升并不总是刷新。我不完全确定是什么原因造成的。但是菜单会很粘。我从Nettuts 借了一些。

    【讨论】:

      最近更新 更多