[英]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.