添加填充在 Chrome 和 Firefox 中的工作方式与在 Safari 中不同

Adding padding is working differently in Chrome and Firefox than it is in Safari

我在做什么

在我的网站上,导航栏不是从页面顶部开始,而是在页面下方的 header/banner 下方。当用户向下滚动经过导航栏时,我将其位置更改为固定,以便在他们滚动浏览其余内容时它现在保持在页面顶部可见。

我希望它看起来真正无缝,所以,当导航栏固定后,我向主要内容添加一些填充以阻止它跳到导航栏所在的 space。我在我的 javascript 中做了所有这些,使用 jQuery 将 类 和样式添加到特定元素。


问题

在 Safari 中,我的代码运行完美!我遇到的问题是,在 Chrome 和 Firefox 上,我似乎需要添加比在 Safari 上更多的填充。在 Chrome 和 Firefox 上,导航栏固定后内容仍然略微向上跳动。在尝试不同的值时,我发现在这些浏览器上,我需要恰好 20 个额外的填充来实现无缝过渡,但内容在 Safari 上跳得太远了!

为什么某些浏览器需要额外的填充,但 Safari 不需要?

如果有人能提供帮助,我将不胜感激,因为这真的很烦我!我不知道为什么这在不同的浏览器中表现不同。

这是一个代码片段。我已尝试将重现问题所需的代码量降至最低,但 CSS 有点冗长,因为我认为我最好将其全部包含在内,以防万一出现问题。

----- 编辑 -----

好的,我发现导致问题的元素是我的 .navbar-button,我没有将其包含在我的原始代码段中。我现在已经把它加进去了。这是在不同浏览器中表现不同的东西。

我必须为此元素添加边距,因为它在 Chrome 或没有它的 Firefox 中定位不正确,而在 Safari 中它定位得很好。

这个额外的边距是导致问题的原因。

$(document).ready(function() {

  var $navBar = $(".navbar");

  $(window).scroll(handleScroll);

  function handleScroll() {
    fixNavbarToTopIfNecessary();
  }

  function fixNavbarToTopIfNecessary() {
    var bannerHeight = $("#banner").outerHeight();

    //When user scrolled past the initial position of the navbar
    if ($(window).scrollTop() > bannerHeight) {

      $navBar.addClass("navbar-fixed");
      $("#content").css("padding-top", $navBar.outerHeight() + "px"); // So that the content doesn't jump underneath the fixed nav.
    } else {
      $navBar.removeClass("navbar-fixed");
      $("#content").removeAttr("style");
    }
  }
});
* {
  box-sizing: border-box;
}
html {
  font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
  font-weight: normal;
  color: #888;
  -webkit-text-size-adjust: 100%;
  /* Prevent font scaling in landscape while allowing user zoom */
}
body {
  line-height: 1.5;
  font-size: 14px;
}
html,
body,
h1,
h2,
h3,
h4,
h5,
h6,
ul,
p,
img {
  margin: 0;
  padding: 0;
  font-weight: normal;
}
button {
  border: none;
  cursor: pointer;
}
.row::before,
.row::after {
  display: table;
  content: " ";
}
.row::after {
  clear: both;
}
.row {
  margin-left: -15px;
  margin-right: -15px;
}
.column {
  display: block;
  float: left;
  min-height: 1px;
  padding-left: 15px;
  padding-right: 15px;
}
.s12 {
  width: 100%;
}
p {
  margin-top: 10px;
  margin-bottom: 20px;
}
a {
  text-decoration: none;
  color: inherit;
}
section {
  padding: 50px 0;
}
.container {
  width: 970px;
}
#banner {
  background-color: #794f29;
  width: 100%;
  height: 600px;
  padding: 150px 0;
  position: relative;
  text-align: center;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
}
.banner-intro {
  width: 50%;
  float: right;
  right: 5%;
  top: 50%;
  margin: 0 auto;
  position: relative;
  transform: translateY(-50%);
}
.banner-intro-heading {
  color: white;
  font-size: 60px;
  text-shadow: 0 0 5px #ffecb0;
  margin-bottom: 10px;
}
.banner-intro-lead {
  color: white;
  font-size: 24px;
}
.btn {
  background-color: #a16fff;
  border: 1px solid #8748ff;
  color: #fff;
  display: inline-block;
  border-radius: 5px;
  padding: 15px 30px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  text-align: center;
  overflow: hidden;
  transition: .3s;
  cursor: pointer;
}
.btn:hover {
  background-color: #a16fff;
  color: white;
  border-color: #8748ff;
}
.banner-intro-button {
  margin-top: 30px;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.15);
  box-shadow: -2px 3px 10px rgba(0, 0, 0, 0.3);
}
.navbar {
  -webkit-font-smoothing: subpixel-antialiased;
  /* this stopped the font weight from changing when the navbar is fixed */
  width: 100%;
  height: 60px;
  background: rgba(255, 236, 176, 0.97);
  line-height: 60px;
  display: block;
  position: relative;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
  z-index: 99;
  transform: translateZ(0);
  /* hack to make sure the navbar is repainted when it's set to a fixed-navbar on iOS */
}
.navbar-fixed {
  position: fixed;
  top: 0;
}
.navbar-brand {
  font-family: "Merienda", cursive;
  display: inline-block;
  padding: 0 15px;
  font-size: 18px;
  float: left;
}
.navbar-items {
  float: right;
}
nav ul {
  list-style: none;
  text-align: center;
}
nav li {
  display: block;
  float: left;
}
nav a {
  position: relative;
  display: block;
  font-size: 16px;
  font-weight: 500;
  color: #794f29;
  transition: .3s;
  padding: 0 25px;
}
.navbar-button {
    background-color: #a16fff;
    border-color: #8748ff;
    padding: 8px 10px;
    margin: 10px 15px;
    line-height: normal;
    box-shadow: -2px 3px 7px rgba(0, 0, 0, 0.2);
}
.featured {
  text-align: center;
}
.featured-title {
  font-size: 40px;
  margin-bottom: 1.5rem;
  color: #ccaa8c;
}
.featured-subtitle {
  font-size: 22px;
  margin-bottom: 1.5rem;
  color: #666;
}
.featured .lead {
  line-height: 2;
  font-size: 16px;
  margin-bottom: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>

  <section id="banner">
    <div class="banner-intro">
      <h1 class="banner-intro-heading">Grand Title!</h1>
      <p class="banner-intro-lead">Lorem ipsum lorum ipsum sausage rat cake mammoth hair.</p>
      <a class="btn banner-intro-button" href="#">Call to Action</a>
    </div>
  </section>

  <nav class="navbar"> <!-- This is what I add the navbar-fixed class to -->
    <div class="container">
      <a href="#" class="navbar-brand">Brand</a>
      <div class="navbar-items">
        <ul>
          <li><a href="#">Link1</a>
          </li>
          <li><a href="#">Link2</a>
          </li>
          <li><a href="#">Link3</a>
          </li>
          <li><a href="#">Link4</a>
            
                <li><a class="btn navbar-button" href="#">Button</a></li>
          </li>
        </ul>
      </div>
    </div>
  </nav>

</header>

<main id="content"> <!-- This is what I add the padding to -->
  <div class="container">
    <div class="row">
      <div class="column s12">
        <section class="featured">
          <h2 class="featured-title">Featured Title</h2>
          <h3 class="featured-subtitle">Featured Subtitle</h3>
          <p class="lead">Lots of info about said feature that rambles on forever and forever.</p>
          <p class="lead">Even more info blablablablalblablabla.</p>
        </section>

      </div>
      <div class="column s12">
        <section class="featured">
          <h2 class="featured-title">Featured Title</h2>
          <h3 class="featured-subtitle">Featured Subtitle</h3>
          <p class="lead">Lots of info about said feature that rambles on forever and forever.</p>
          <p class="lead">Even more info blablablablalblablabla.</p>
        </section>
      </div>
      <div class="column s12">
        <section class="featured">
          <h2 class="featured-title">Featured Title</h2>
          <h3 class="featured-subtitle">Featured Subtitle</h3>
          <p class="lead">Lots of info about said feature that rambles on forever and forever.</p>
          <p class="lead">Even more info blablablablalblablabla.</p>
        </section>
      </div>
      <div class="column s12">
        <section class="featured">
          <h2 class="featured-title">Featured Title</h2>
          <h3 class="featured-subtitle">Featured Subtitle</h3>
          <p class="lead">Lots of info about said feature that rambles on forever and forever.</p>
          <p class="lead">Even more info blablablablalblablabla.</p>
        </section>
      </div>
    </div>
  </div>
</main>

我为您的问题找到了两个解决方案(使用它们中的每一个就足够了)。

方法 1).navbar-buttonmargin-topmargin-bottom 更改为 0(以防止垂直边距折叠.. .) ,并使用 vertical-align: middle; 将其垂直居中:

.navbar-button {
  background-color: #a16fff;
  border-color: #8748ff;
  padding: 8px 10px;
  margin: 0 15px; /* *** margin top & bottom are changed to zero! */
  vertical-align: middle; /* *** this is new! */
  line-height: normal;
  box-shadow: -2px 3px 7px rgba(0, 0, 0, 0.2);
}

方法 2).btn 中删除 display: inline-block class:

.btn {
  background-color: #a16fff;
  border: 1px solid #8748ff;
  color: #fff;
  /* display: inline-block; */ /* *** this is removed! */
  ...
}

很明显,如果您不想更改 .btn 的默认样式,您可以将 display: block 作为内联样式手动添加到您的导航栏按钮 (style="display: block;")或使用新的 css class(例如:.block)。

<li>
  <a class="btn navbar-button" href="#" style="display: block;">Button</a>
</li>