简体   繁体   English

使用requestAnimationFrame

[英]using requestAnimationFrame

I'm using this function to set a transform property of some elements,but the animation is not so smooth in firefox and it's less smooth when window size is bigger(in any browser).I have read a lot of thing's on blogs which are saying that I can make much smoother animation using requestAnimationFrame,but I don't understand how I can implement it inside of my function.Can somebody explain me how I can use it inside of my function? 我正在使用此功能设置某些元素的transform属性,但是在firefox中动画不是那么平滑,并且当窗口大小更大时(在任何浏览器中)动画的平滑度都不太平滑。我在博客上阅读了很多内容说我可以使用requestAnimationFrame制作更流畅的动画,但是我不明白如何在函数内部实现动画。有人可以解释一下如何在函数内部使用动画吗?

function sectionMovement(delay,section) {
  setTimeout(function () {
    var val = ((v.sectionIndex + 1) > v.leavingSection) ?
      val = 100 : 
      val = 0;
    document.getElementById("sec_"+section+"").style.transform = "translateY(-"+val+"%)"
  }, delay);
};

Something like this: 像这样:

function updateSection(selector) {
  var elem = document.getElementById("sec_" + section);
  return function step() {
    var val = ((v.sectionIndex + 1) > v.leavingSection) ? // not sure what 'v' is?
      100 : 
      0;

    elem.style.transform = "translateY(-"+val+"%)";
    requestAnimationFrame(step);
  }
}

var sel = "myElementId";
requestAnimationFrame(updateSection(sel));

You will also likely want an external variable to check against to know when to stop the animation. 您可能还希望检查外部变量以知道何时停止动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM