Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

184
Vistas
Bind translateX() transform in Angular 2+

How can one bind the transform: translateX() style in Angular?

What I've tried:

<div [style.transform]="translateX({{x}})">

and

<div [style.transform.translateX.px]="x">
8 months ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This should work

<div [style.transform]="'translateX(' + x + 'px)'">

Edit

It seems that it is necessary to bypass XSS protection for this to work.

8 months ago · Santiago Trujillo Denunciar

0

[ngStyle]="{'transform': 'translateX(' + transX + 'px)'}"

where transX is a component variable.

8 months ago · Santiago Trujillo Denunciar

0

The accepted answer works but you can make you code a lot cleaner by using methods as the binding of your style properties

<div *ngFor="let il of imageLayers">
    <img class="divFloatLayer" 
        [src]="il.img_src" 
        [style.left]="il.getLeftPx()"
        [style.top]="il.getTopPx()"
        [style.z-index]="il.getZindex()"
        [style.transform]="il.getTransform()"
    />
</div>

In the class that contains the values that you need to bind to:

getLeftPx() {
    return `${this.left}px` ;
}

getTopPx() {
    return `${this.top}px` ;
}

getZindex() {
    return `${this.z_index}` ;
}

getTransform() {
    return `translateX(${this.x}px)`;
}
8 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.