javascript - CSS 动画在恢复之前下降

标签 javascript html css animation css-animations

我正在尝试创建一个导航栏,其中导航项会在用户滚动时上拉, Logo 会在用户滚动时缩小,而在用户向上滚动时会缩小。

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

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 fiddle :

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

最佳答案

您在菜单栏上遇到冒泡运动的原因是因为您有

.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 中测试

关于javascript - CSS 动画在恢复之前下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34458885/

相关文章:

javascript - 添加一个额外的自定义类到 select2-container

javascript - 自关闭 .js 切换菜单

html - 使用 CSS 更改 anchor 标记的内容

javascript - CSS - 显示动态高度 float DIV - 缺少背景图片

javascript - AngularJS ng- View 不工作

javascript - AngularJS 过滤

javascript - 如何增加相反方向的高度?

html - <img> 和相邻的 <div> 之间不需要的空格

html - 网站如何告诉浏览器它是一个搜索引擎并且可以添加为搜索引擎?

css - JSF : Changing component style in jSTL c:foreach array not working