Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
Angular: follow cursor has too much delay/lag

I wanted to try to do the angular version of a cursor follow movement, but the mouse movement is not smooth, how do I fix this and what's the reason for it?: https://stackoverflow.com/a/48756322/5152892

My angular attempt: https://stackblitz.com/edit/angular-vnx9yd?file=src/app/app.component.ts

export class AppComponent {
  name = 'Angular';

  constructor(
    private el: ElementRef
  ) {}

  get tooltip() {
    return this.el.nativeElement.querySelector('.sites-circle');
  }

  enter(source: string) {
    this.tooltip.classList.add('show');
  }

  move(e: { pageX: number; pageY: number }) {
    const tooltipStyle = this.tooltip.style;

    tooltipStyle.left = e.pageX + 'px';
    tooltipStyle.top = e.pageY + 'px';
  }

  leave() {
    this.tooltip.classList.remove('show');
  }
}

CSS:

p {
  font-family: Lato;
}

.mouse-circle {
  position: absolute;
  border: solid 1px #ccc;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: white;
}
.show {
  opacity: 1 !important;
}

.sites-circle {
  z-index: 500;

  position: absolute;
  border: solid 1px #ccc;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: blue;
  pointer-events: none;
  transition-duration: 200ms;
  transition-timing-function: ease-out;
  transform: translate(-50%, -50%);
  opacity: 0;
}

.wrapper {
  height: 100vh;
  width: 100%;
}

HTML:

<div
  class="wrapper"
  (mouseenter)="enter()"
  (mousemove)="move($event)"
  (mouseleave)="leave()"
>
  <p>Start editing to see some magic happen :)</p>
  <div class="sites-circle">hello</div>
</div>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

sometimes is better use fromEvent rxjs operator to mannage mousemove. This allow use different operators to not emit constantly the event

Use Viewchild to get the elements

<div #mydiv class="show"
  class="wrapper"
  (mouseenter)="enter()"
  (mouseleave)="leave()"
>
  <div #tooltip class="sites-circle">hello</div>
</div>

@ViewChild('mydiv',{static:true}) div:ElementRef
@ViewChild('tooltip',{static:true}) tooltipEl:ElementRef

Then

  ngOnInit()
  {
    this.subscription=fromEvent(this.div.nativeElement,'mousemove').pipe(
        throttleTime(200,asyncScheduler,{ trailing: true }))
          .subscribe((e:any)=>{
             const tooltipStyle = this.tooltip.style;

             tooltipStyle.left = e.pageX + 'px';
             tooltipStyle.top = e.pageY + 'px';
             this.cont++;
    })
  }
  ngOnDestroy()
  {
    this.subscription.unsubscribe()
  }

In the e.g. (see stackblitz) I use throttleTime to not emit a new event until 200 miliseconds (you can change to 100 or 150)

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda