簡體   English   中英

相對於視口角度6的位置元素

[英]Position element relative to viewport angular 6

在我的項目中,我想從單擊位置放置元素。 現在,一個模態彈出窗口打開並向下推動內容。 當此內容關閉時,下一個模態將再次被拉起,因為它的位置是相對的。 我最理想的目標是使所有模態保持在單擊狀態,最高位置將其他模態向下拉。

我正確地獲得了元素的頂部位置,但是當我使用.css時,出現ERROR TypeError: this.singleQuestion.nativeElement.css is not a function 有沒有辦法修復此代碼或設置相對於視口的頂部位置。

public toggle() {
            this._active = !this._active;
            if (this._active) {
                const top = this.singleQuestion.nativeElement.getBoundingClientRect().top;
                this.singleQuestion.nativeElement.css({top: top});
            }
            this.clickEvent.emit((this._active) ? this._question : null);
        }

有更好的版本可以滾動

scroll(): void {
    ...
    const top = this.singleQuestion.nativeElement.getBoundingClientRect().top;
    window.scrollTo({top: top, left: 0, behavior: 'smooth'});
    ....
}

但是,如果您只想設置y位置,則可以使用

this.singleQuestion.nativeElement.offsetTop = top;

要么

this.singleQuestion.nativeElement.style.top = top;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM