問題描述
如何在 Ionic 2 中手動定位彈出框?
How do I manually position a popover in Ionic 2?
我無法在 css 類中設(shè)置位置,因為彈出框控制器使用內(nèi)聯(lián)樣式設(shè)置位置.
I can't set the position in a css class, since the popover controller sets the position with an inline style.
推薦答案
查看代碼,彈出框的位置似乎沒有選項.但是,當(dāng)由于用戶點擊某物而打開彈出框時,它可以通過點擊事件定位.我們也可以將這些知識用于手動定位:
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});
需要注意的幾點:
- 您可以設(shè)置
top
、left
或兩者的值. - 值必須以像素為單位,如數(shù)字.
- 如果
top
或left
沒有給出,那么通常的定位算法將用于該值.
- You can set the value for
top
,left
, or both. - The values must be given in pixels, as numbers.
- If
top
orleft
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.
這當(dāng)然適用于 Ionic2 和 3,很高興有人確認(rèn)它是否也適用于 Ionic4!
This certainly works in Ionic2 and 3, happy to have someone confirm if it works in Ionic4 as well!
這篇關(guān)于如何在 Ionic 2 中定位彈出框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!