Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!