javascript - 为 DOMMouseScroll 事件添加延迟

标签 javascript jquery css

我有一些应该在用户滚动后出现的 div。所以我跟踪 mouseScroll 事件。但我想为我的事件添加一些延迟。我尝试使用 setTimeout 但它没有帮助......如果我做一个完整的轮子滚动(每个 Action 超过一个),除了删除我的类 .shown< 之外我什么也得不到。我应该怎么办?这是我的代码:

$(document).ready(function() {
  var timer;
  var delay = 1000;

  $(window).bind('mousewheel DOMMouseScroll', function(event) {
    var shown = $(".shown").removeClass('shown');
    timer = setTimeout(function() {
      if (event.originalEvent.wheelDelta > 14 || event.originalEvent.detail < 14) {
        // scroll up
        if (shown.next() && shown.next().length) {
          shown.next().addClass('shown');
        } else {
          shown.siblings(":first").addClass('shown');
        }
      } else {
        // scroll down
        if (shown.next() && shown.next().length) {
          shown.next().addClass('shown');

        } else {
          shown.siblings(":first").addClass('shown');
        }
      }
    }, delay);
  }, function() {
    clearTimeout(timer);
  });
});
.view {
  width: 300px;
  height: 20px;
  display: none;
  clear: both;
  transition: opacity 1s ease-out;
  opacity: 0;
}
#first {
  background: grey;
}
#second {
  background: red;
}
#third {
  background: blue;
}
#fourth {
  background: orange;
}
#fifth {
  background: green;
}
.view.shown {
  display: block;
  opacity: 1;
  -webkit-animation: fadeIn 1s;
  animation: fadeIn 1s;
}
@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div class="view shown" id="first"></div>
<div class="view" id="second"></div>
<div class="view" id="third"></div>
<div class="view" id="fourth"></div>
<div class="view" id="fifth"></div>

不幸的是,我也不知道如何以相同的延迟反转这个脚本。任何帮助将不胜感激!

最佳答案

尝试使用:

  $(window).bind('mousewheel DOMMouseScroll', function(event) {
    var shown = $(".shown").removeClass('shown');
    clearTimeout(timer);
    timer = setTimeout(function() {
      if (event.originalEvent.wheelDelta > 14 || event.originalEvent.detail < 14) {
        // scroll up
        if (shown.next() && shown.next().length) {
          shown.next().addClass('shown');
        } else {
          shown.siblings(":first").addClass('shown');
        }
      } else {
        // scroll down
        if (shown.next() && shown.next().length) {
          shown.next().addClass('shown');

        } else {
          shown.siblings(":first").addClass('shown');
        }
      }
    }, delay);
  });

关于javascript - 为 DOMMouseScroll 事件添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700647/

相关文章:

javascript - 当我用 jquery 添加新的 div 时,试图让这些 div 不重置位置

jquery - jquery.d.ts 定义在 TypeScript 0.9 中产生了许多错误

javascript - 衡量用户在网站访问期间花费的时间

css - 希伯来语排版

javascript - OO Javascript 是否有 `to_bool` 或 `__len__` 等效项?

javascript - 闭包中如何引用局部变量?

javascript - 从子窗口访问父窗口的内容

firefox - 只有 Firefox 对内联 block 元素表现得很奇怪

html - 当 flex div 的 1 项在另一个 flex div 中时,如何一直正确地 flex 它?

javascript - Object.observe() 不适用于 native Image 对象