微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 如何在向下滚动时构建浮动菜单栏

当我向下滚动网站显示黑色菜单栏在顶部看起来像浮动吧。
但我认为这是jquery。我已经尝试了CSS,但似乎不像我想要的那样为我工作
#menucontainer {
    position:fixed;
    top:0;
    height: 250px
}

#firstElement {
    margin-top: 250px
}

这是网站的基本想法,我希望菜单如下所示:

http://avathemes.com/WP/Hexic/

解决方法

将您的菜单包装在带有ID或类的div或部分。
#yourID.fixed {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    border-bottom: 5px solid #ffffff;
}

//STICKY NAV
$(document).ready(function () {  
  var top = $('#yourID').offset().top - parseFloat($('#yourID').css('marginTop').replace(/auto/,100));
  $(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();

    // whether that's below the form
    if (y >= top) {
      // if so,ad the fixed class
      $('#yourID').addClass('fixed');
    } else {
      // otherwise remove it
      $('#yourID').removeClass('fixed');
    }
  });
});

不记得我从哪里得到了,所以没有对我的赞美,但它对我有用。

原文地址:https://www.jb51.cc/jquery/183529.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐