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

179
Visualizações
Angular 8 , how to detect current "scrolled to" div?

I have a div with inner divs that i can scroll to by passing their id to a function. i wonder how can do the opposite - detect div's id when it's revealed/ scroll to ,and by that changing the class of the button which it's belongs to. So for example - if i scroll to "middle div" (number 5) - i want to be able to detect it and highlight "middle" button - the for all...scroll to last div - and then "last"button should be highlighted.

enter image description here

Example

    @Component({
selector: 'my-app',
template: `
 <div class="main-wrapper">
  <div class="button-wrapper">
  
    <button *ngFor="let item of buttonsArr" 
    [ngClass]="{'active': item.isActive}"
    (click)="scrollToElement(item)"
    class="nav-btn">{{item.key}}</button>
</div>
<div class="content">
    <button (click)="scrollToElement()">Scroll prev</button>
    <button (click)="scrollToElement()">Scroll next</button>
<div class="wrapper">
<li *ngFor="let item of itemList; let i = index" id="elem-{{i}}">{{item}}</li>
</div>
</div>
</div>
 `,
 })
 class HomeComponent {
 text = 'foo';
 buttonsArr = [{key:'first', value:1},{key:'middle', value:4}, {key:'last', 
  value:11}];
testObject = {fieldFirst:'foo'};
itemList = [
'1', 
'2', 
'3', 
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
];

 scrollToElement(elemId?) {
   if(elemId){
    document.getElementById("elem-"+elemId.value).scrollIntoView({behavior: "smooth", 
    block: "start", inline: "nearest"});
  this.buttonsArr.forEach(i => { i.isActive = false})
  elemId.isActive = true;
  }else{
  document.getElementById("elem-7").scrollIntoView({behavior: "smooth", block: 
   "start", inline: "nearest"});
  }

}

}

about 4 years ago · Juan Pablo Isaza
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