【问题标题】:CSS Animation drops before going back upCSS 动画在回升之前下降
【发布时间】:2015-12-25 09:02:25
【问题描述】:

我正在尝试有一个导航栏,其中导航项目被拉起,并且徽标在用户滚动时缩小,并在用户向上滚动时返回。

但目前导航项目在上升之前会下降......当它恢复时它最终会在不同的位置......

HTML:

<nav class="navbar">
 <a href="#">James Jones</a>
  <ul class="nav-items">
   <li><a href="#about">About</a></li>
   <li><a href="#">Work</a></li>
   <li><a href="#">Photography</a></li>
   <li><a href="#">Videos</a></li>
   <li><a href="#">Contact</a></li>
  </ul>
</nav>

CSS:

.navbar {
  background: #fff;
  font-family: ShorelinesScriptBold;
  font-size: 2em;
  font-weight: 900;
  height: 150px;
  line-height: 100px;
  position: fixed;
  text-align: center;
  -webkit-transition: all 1.0s ease;
  transition: all 1.0s ease;
  width: 100%;
  z-index: 10000;
}

.navbar a {
  color: #343434;
  display: block;
  /*margin: 0 0 -24px 0;*/
  margin: 0 0 -55px 0;
  text-decoration: none;
}

.navbar a:hover {
  color: #343434;
  text-decoration: none;
}

.shrink {
  font-family: 'Radley', serif;
  font-size: 1em;
  font-weight: 500;
  height: 50px;
  letter-spacing: 1px;
  line-height: 50px;
  position: fixed;
  top: -5px;
  text-transform: uppercase;
}

.nav-items {
  background: #fff;
  -webkit-transition: all 1.0s ease;
  transition: all 1.0s ease;
}

.nav-items li {
  display: inline-block;
  font-family: Helvetica;
  font-size: 12px;
  letter-spacing: 0;
  text-transform: uppercase;
}

.nav-items a {
  color: gray;
  font-weight: 300;
  margin: 0 15px;
  text-decoration: none;
}

.nav-items a:hover {
  color: #FFAED8;
}

.margin {
  margin: 30px 0 0 0;
}

Javascript:

$(window).scroll(function() {
   if ($(document).scrollTop() > 150) {
     $('.navbar').addClass('shrink');
     $('.nav-items').addClass('margin');
   } else {
     $('.navbar').removeClass('shrink');
     $('.nav-items').addClass('margin');
   }
});

JS 小提琴:

https://jsfiddle.net/gkrtk8s3/4/

【问题讨论】:

  • 也......它的行为更像我在 jsfiddle 上想要的方式......但在本地它的行为不同(使用 chrome)
  • 您的意思是两次都运行$('.nav-items').addClass('margin'); 吗?

标签: javascript html css animation css-animations


【解决方案1】:

您在菜单栏上出现冒泡的原因是因为您有

.nav-items {
  background: #fff;
  -webkit-transition: all 1.0s ease;/* Duplicate Transition. you already have a transition on your nav tag
  transition: all 1.0s ease; /* Duplicate Transition. you already have a transition on your nav tag
}

从上面取出-webkit-transition: all 1.0s easetransition: all 1.0s ease;这两行

注意:在 chrome 版本 46.0.2490.71 中测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多