如何在 Ionic 2 中定位弹出框

2022-01-13 00:00:00 javascript ionic2

如何在 Ionic 2 中手动定位弹出框?

How do I manually position a popover in Ionic 2?

我无法在 css 类中设置位置,因为弹出框控制器使用内联样式设置位置.

I can't set the position in a css class, since the popover controller sets the position with an inline style.

推荐答案

查看代码,弹出框的位置似乎没有选项.但是,当由于用户点击某物而打开弹出框时,它可以通过点击事件定位.我们也可以将这些知识用于手动定位:

Looking through the code, there doesn't seem to be an option for the popover's position. However, when opening the popover as a result of the user tapping something, it can be positioned by the click event. We can use that knowledge for manual positioning as well:

let pop = this.popoverCtrl.create(MyPopover);

let ev = {
  target : {
    getBoundingClientRect : () => {
      return {
        top: 100
      };
    }
  }
};

pop.present({ev});

需要注意的几点:

  • 您可以设置 topleft 或两者的值.
  • 值必须以像素为单位,如数字.
  • 如果 topleft 没有给出,那么通常的定位算法将用于该值.
  • You can set the value for top, left, or both.
  • The values must be given in pixels, as numbers.
  • If top or left is not given, then the usual positioning algorithm is used for that value.

我很高兴知道是否有更好的方法,但到目前为止这是我能想到的最好方法.

I'd be happy to know if there's a better way, but so far this is the best I could come up with.

这当然适用于 Ionic2 和 3,很高兴有人确认它是否也适用于 Ionic4!

This certainly works in Ionic2 and 3, happy to have someone confirm if it works in Ionic4 as well!

相关文章