简体   繁体   English

javascript上的arguments.callee的解决方法下滑

[英]Workaround for arguments.callee on javascript slide down

I have been following a CSS Tricks article on animating section height and I would like to use the solution in my Angular 2 app.我一直在关注 关于动画部分高度CSS 技巧文章,我想在我的 Angular 2 应用程序中使用该解决方案。 My app has the following function for section expansion:我的应用程序具有以下用于部分扩展的功能:

expandSection(element) {
  // get the height of the element's inner content, regardless of its actual size
  var sectionHeight = element.scrollHeight;

  // have the element transition to the height of its inner content
  element.style.height = sectionHeight + 'px';

  // when the next css transition finishes (which should be the one we just triggered)
  element.addEventListener('transitionend', function (e) {
    console.log(arguments)
    // remove this event listener so it only gets triggered once
    element.removeEventListener('transitionend', arguments.callee);

    // remove "height" from the element's inline styles, so it can return to its initial value
    element.style.height = null;
  });
}

and the arguments.callee line is throwing an error:并且arguments.callee行抛出错误:

Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them异常:类型错误:“调用者”、“被调用者”和“参数”属性可能无法在严格模式函数或调用它们的参数对象上访问

Can someone provide me with a workaround for arguments.callee ?有人可以为我提供arguments.callee的解决方法吗?

You could declare the function explicitly :您可以显式声明该函数:

expandSection(element) {
  // get the height of the element's inner content, regardless of its actual size
  var sectionHeight = element.scrollHeight;

  // have the element transition to the height of its inner content
  element.style.height = sectionHeight + 'px';

  function myFunc(e) {
    console.log(arguments)
    // remove this event listener so it only gets triggered once
    element.removeEventListener('transitionend', myFunc);

    // remove "height" from the element's inline styles, so it can return to its initial value
    element.style.height = null;
  }

  // when the next css transition finishes (which should be the one we just triggered)
  element.addEventListener('transitionend', myFunc);
}

If you only want the function to run once you can pass this as an option to addEventListener: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener如果您只希望该功能运行一次,您可以将其作为选项传递给 addEventListener: https : //developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

element.addEventListener('transitionend',  function () {
  console.log(arguments);
  element.style.height = null;
}, { once: true });

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

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