Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 scroll a div a certain amount

I have a div whose height is given and it has overflow-y: auto, so when necessary it has a scrollbar.
Now I want to be able to scroll that div a certain amount on an event. So far I've been able to scrollIntoView(false) which just scrolls it to the bottom. I want to scroll it almost, but not quite to the bottom. I do not want to scroll the window, as this div is position: fixed relative to the window.
From what I've seen this is technically possible, but people keep referring to various plugins. Right now a plugin is not an option (maybe for some future release, but not now).

<form novalidate #searchFilterForm [formGroup]="filterForm" role="application">
<section id="searchFilters" role="form">
  <div class="search-filter-tab" #searchFilterTab>
    ..
  </div>
<div #searchFormContainer class="search-filter-container" >
  <div class="search-filter-header">  
    ...
  </div>
  <fieldset class="search-filter-checkboxes search-mobile-small" >
    ...
  </fieldset>
  <fieldset class="search-filter-sliders search-mobile-small" >
    ...
    </div>
  </fieldset>
  <fieldset class="search-filter-dropdowns search-mobile-small" >
    ...
  </fieldset>
  <fieldset >
    <span #scrollToHere></span>
    <div class="search-filter-text-input-container search-mobile-small" *ngFor="let textItem of searchBoxList; trackBy: trackByFunc; let i = index;">

      <app-auto-complete 
              #autoCompleteBoxes
              ...
              (showToggled)="scrollToEndOfFilter($event)"
              >
          <input 
            type="text" 
            autocomplete="off"
            [attr.name]="textItem.apiName" 
            [placeholder]="textItem.label" 
            [attr.aria-label]="textItem.label"         
            class="search-filter-text-input" 
            >
      </app-auto-complete>
    </div>
  </fieldset>
</div>
</section>
</form>     

Typescript:

scrollToEndOfFilter(ev){ //ev == {shown: true/false, ref: ElementRef}
    if (ev == null || (ev.shown == true && ev.ref)){
      this.searchFormContainer.nativeElement.scrollTop = 950;
    }
}
like image 933
AvailableName Avatar asked May 27 '18 06:05

AvailableName


People also ask

How do I scroll to a specific div?

The scrollIntoView method: The scrollIntoView() is used to scroll to the specified element in the browser. Example: Using scrollIntoView() to scroll to an element.

How do I scroll specific elements?

Use the scroll() Function to Scroll to an Element in JavaScript. The element interface's scroll() function scrolls to a specific set of coordinates within a given element. This is suitable for Chrome and Firefox and not for the rest. Copy window.

How can I fix Div while scrolling?

To make div fixed scrolling to that div with CSS, we can use the position: sticky CSS property. position: -webkit-sticky; position: sticky; top: 0; z-index: 1; to set position to sticky and z-index to 1 to make the element with the sticky position show on top after we scroll to it.


1 Answers

How about standard approach: Fist you assign a reference to your div like this:

<div #divToScroll></div>

then, you get a reference to your div:

@ViewChild('divToScroll') divToScroll: ElementRef;

and finally when you need to scroll you just call:

divToScroll.nativeElement.scrollTop = 30;
like image 68
Jack Avatar answered Sep 27 '22 20:09

Jack