【问题标题】:scroll triggered animation extremely delayed滚动触发动画极度延迟
【发布时间】:2018-02-21 08:39:14
【问题描述】:

使用 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>

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

【问题讨论】:

    标签: jquery css jquery-animate


    【解决方案1】:

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

    所以我寻找一种方法,让向下和向上滑动的动画只在触发另一个动画之前触发一次。我使用了一个变量(status_1),其默认值为“关闭”。当滚动值超过 100 时,仅当 status_1 为“关闭”时才会触发第一个(向下滑动)动画。但是一旦触发,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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多