jquery - 滚动触发的动画极度延迟

标签 jquery css jquery-animate

使用 jQuery,我试图制作一个类似于 this Wordpress plugin 的导航栏: 导航栏在页面加载时隐藏/离开屏幕,当到达某个滚动位置时向下滑动到固定位置,当用户向上滚动回到顶部时向上滑动回到其离开屏幕的位置。

我设法做到了,但是,时间完全错误:滚动后可能需要几秒钟才能出现菜单栏。向上滚动时情况更糟:起初我认为它根本不起作用,但有时 15 秒甚至更长时间后,它最终又向上移动。

这是我的代码:

$(window).scroll(function() {
  var scrollposition = $(window).scrollTop();
  if (scrollposition > 100) {
    $("#main_navigation").animate({
      top: "0px"
    }, 600);
  };
  if (scrollposition < 100) {
    $("#main_navigation").animate({
      top: "-82px"
    }, 400);
  };
});
html,
body {
  margin: 0;
  height: 100%;
}
.content {
  height: 200%;
  background: #fda;
  padding: 5em 3em;
}

nav#main_navigation {
  position: fixed;
  z-index: 1;
  top: -82px;
  width: 100%;
  background: #fff;
  height: 54px;
  border-bottom: 1px solid #eee;
}

.logo {
  display: inline-block;
  position: relative;
  top: 50%;
  left: 2em;
  margin: 0;
  width: 36px;
  transform: translateY(-50%);
}

nav#main_navigation ul {
  position: relative;
  top: 50%;
  margin: 0;
  transform: translateY(-50%);
  display: inline-block;
  list-style: none;
  float: right;
  margin-right: 0.8em;
}

nav#main_navigation li {
  display: inline-block;
  margin-right: 1.2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="main_navigation">
  <div class="logo">(logo)</div>
  <ul>
    <li><a href="#">Welcome</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
<div class="content">
  <p>This is the content. Scroll down at least 100px to make the navbar appear. This should take 0.6 seconds, but takes much longer.</p>
  <p>Then scroll back up to make the navbar disappear again. This should only take 0.4 seconds...</p>
</div>

我想这可能与必须处理的滚动事件太多有关,但我不知道如何避免或过滤它。还是另有原因?

最佳答案

我想通了:有很多的滚动事件,并且每个滚动事件都会触发一个动画,这显然对浏览器来说太多了。需要很长时间才能处理完所有这些并最终执行动画,从而导致长时间延迟。

所以我寻找一种方法,让向下和向上滑动动画在另一个动画被触发之前只触发一次一次。我使用了一个变量 (status_1),其默认值为“closed”。当滚动值超过 100 时,只有当 status_1 为“closed”时才会触发第一个(向下滑动)动画。但是一旦触发,status_1就被设置为“open”,所以只要滚动值在100以上就不会再次触发。同第二个if条件和动画:

var status_1 = "closed";
$(window).scroll(function() {
  var scrollposition = $(this).scrollTop();
  if ((scrollposition > 100) && (status_1 == "closed")) {
    $('#main_navigation').animate({
      top: '0px'
    }, 600);
    status_1 = "open";
  };
  if ((scrollposition < 100) && (status_1 == "open")) {
    $('#main_navigation').animate({
      top: '-82px'
    }, 400);
    status_1 = "closed";
  };
});
html,
body {
  margin: 0;
  height: 100%;
}

.content {
  height: 200%;
  background: #fda;
  padding: 5em 3em;
}

nav#main_navigation {
  position: fixed;
  z-index: 1;
  top: -82px;
  width: 100%;
  background: #fff;
  height: 54px;
  border-bottom: 1px solid #eee;
}

.logo {
  display: inline-block;
  position: relative;
  top: 50%;
  left: 2em;
  margin: 0;
  width: 36px;
  transform: translateY(-50%);
}

nav#main_navigation ul {
  position: relative;
  top: 50%;
  margin: 0;
  transform: translateY(-50%);
  display: inline-block;
  list-style: none;
  float: right;
  margin-right: 0.8em;
}

nav#main_navigation li {
  display: inline-block;
  margin-right: 1.2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="main_navigation">
  <div class="logo">(logo)</div>
  <ul>
    <li><a href="#">Welcome</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
<div class="content">
  <p>This is the content. Scroll down at least 100px to make the navbar appear. This should take 0.6 seconds.</p>
  <p>Then scroll back up to make the navbar disappear again. This should only take 0.4 seconds...</p>
</div>

关于jquery - 滚动触发的动画极度延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183911/

相关文章:

css - 如何使用 JSF 和 CSS 为表格行获取渐变背景?

html - shopify 中响应式设计的 CSS 问题

javascript - 在不降低内容的情况下显示导航下拉菜单

javascript - jquery mobile 中的动画按钮 css 属性

javascript - jquery 动画和 CSS 位置

jquery - var anSelected = fnGetSelected( oTable ) 未执行

jquery 焦点不会触发 css 焦点

jquery - jquery追加和json数据的问题

css - 如何使 CSS 考虑具有绝对定位的侧边栏

javascript - EasyUI DateBox - 无法提交表单