elementRef.style.top ="100px"不起作用 [英] elementRef.style.top = "100px" doesn't work

查看:40
本文介绍了elementRef.style.top ="100px"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用elementRef来移动组件的位置.我的以下代码似乎很合理-但不起作用.为什么?

I want to move the position of a component using elementRef. My code below seems pretty reasonable - but doesn't work. Why?

  ngAfterViewInit() {
      let element = this.elementRef.nativeElement
      element.style.color = "red" // WORKS!
      element.style.top = "100px" // NO EFFECT!
  }

这是我的示例的链接: https://stackblitz.com/edit/angular-qlzkkc?file=app%2Fhello.component.ts

here's a link to my example: https://stackblitz.com/edit/angular-qlzkkc?file=app%2Fhello.component.ts

推荐答案

top,bottom属性仅适用于定位的元素.因此,通过向其添加任何position属性可以解决该问题.

top, bottom properties work only for positioned elements. So by adding any position property to it can fix the issue.

例如

element.style.position = "relative";

这篇关于elementRef.style.top ="100px"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆