jquery - 向下滚动时如何构建 float 菜单栏

标签 jquery wordpress css css-transitions

当我向下滚动站点时,顶部显示黑色菜单栏,看起来像 float 栏。 但我认为这与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');
    }
  });
});

不记得我从哪里得到这个,所以没有向我致敬,但它对我有用。

关于jquery - 向下滚动时如何构建 float 菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17165915/

相关文章:

javascript - 从 jquery 数组值中选择 div

php - WordPress - 更新数据库 - $wpdb->prepare

Wordpress Gutenberg anchor 支持动态 block

javascript - Safari 返回值 "normal"为 "line-height"样式

javascript - 无法滚动 IE7 滚动条

php - 运行单行 javascript 代码?

javascript - Cufon,cufontext 在 jQuery 克隆 IE7 上中断

javascript - 使用 Javascript 隐藏相对 div

javascript - 使用 jQuery 多次更改 DIV 文本

javascript - 如何将div字段动态添加到html页面中?