0

我正在尝试使用 JS 让我的标题在使用 JQuery Waypoints 到达页面顶部时变得粘滞。问题是它没有粘住。有什么建议么?

以下是代码中的选择:

HTML:

<div id = 'top-container'>
   <div id = 'wrapper'>
       <div id = 'top-img'>
           <img src='top-img.png' width = "600" height = '115'>
       </div>
       <h1 id = 'top-slogan'>A Video Production Studio</h1>
       <div class = 'nav-container'>
           <nav>
               <div id = 'header-img'>
                   <img src='top-img.png' width = "450" height = '86.25'>
               </div>
               <div id = 'nav-items-container'>
                   <ul class = 'nav-items'>
                       <li class = 'nav-item'><a href='#'><b>w</b>hat</a></li>
                       <li class = 'nav-item'><a href='#'><b>h</b>ow</a></li>
                       <li class = 'nav-item'><a href='#'><b>w</b>hy</a></li>
                       <li class = 'nav-item'><a href='#'><b>w</b>here</a></li>
                       <li class = 'nav-item'><a href='#'><b>w</b>ho</a></li>
                   </ul>
               </div>
           </nav>
       </div>
   </div>
</div>

jQuery:

  $(function() {
        // Do our DOM lookups beforehand
        var nav_container = $(".nav-container");
        var nav = $("nav");
        nav_container.waypoint({
            handler: function(event, direction) {
                nav.toggleClass('sticky', direction=='down');
            }
        });
    });

CSS:

.sticky {
    position: fixed;
    top: 0;
}
4

1 回答 1

0

您传递给 .waypoint() 的处理程序具有错误的签名。direction是第一个也是唯一的参数。它应该如下所示:

handler: function(direction) {
    nav.toggleClass('sticky', direction=='down');
}
于 2013-07-03T15:30:16.350 回答