javascript - 在特定的转换值触发函数

标签 javascript jquery html css

我想在特定的 transitionY 值触发一个函数。

我发现了这个:

document.getElementById("main").addEventListener("webkitTransitionEnd", myFunction);

document.getElementById("main").addEventListener("transitionend", myFunction);

function myFunction() {
    alert('huuh');
}

但它只是一个transition End后的trigger。当我在我的页面上向下滚动时,一个 div 框将样式值更改为 (transform: translateY(-100%))。

我试过:

if (document.getElementsByClassName('scroll-container').style.transform == "translateY(-100%)")
.......

但它不起作用。

最佳答案

您可以使用 jQuery's $.animate function .使用 progress 属性,您将获得动画的当前状态。例如

setTimeout(function() {
  var transitionPointReached = false;
  $('#some-id').animate({
    opacity: 0.1
  }, {
    duration: 1000,
    progress: function(animation, progress, remaining){
      console.log('progress called', progress, remaining);
      if(!transitionPointReached && progress >= 0.7) {
        transitionPointReached = true;
        // call the required callback once.
      }
    }
  });
}, 1000);
#some-id {
  width: 200px;
  height: 200px;
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="some-id"></div>

希望对您有所帮助。

关于javascript - 在特定的转换值触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57367871/

相关文章:

javascript - AngularJS 注入(inject)器 - 错误 : [$injector:unpr] Unknown provider: $rootScopeProvider <- $rootScope

javascript - 从 jQuery 同步运行 CSS 动画

javascript - Jquery Div 从右到左的幻灯片转换无法正常工作

javascript - 如何在纯 Javascript 中删除和隐藏 HTML 元素?

javascript - Vuejs 变异对象作为 prop 传递

javascript - FancyBox 2.x 中显示/隐藏标题不可靠

javascript - 将单独的入口点脚本分配给单独的 HtmlWebpackPlugin 实例

javascript - Framework 7 onclick 事件绑定(bind)不起作用

html - 如何在不同屏幕尺寸之间更改 html 中渐变线的方向

html - 在 res.sendfile express js 上发送 html 和 css 文件