Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  76] [ 6]  / answers: 1 / hits: 20928  / 6 Years ago, sun, may 27, 2018, 12:00:00

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;
}
}

More From » html

 Answers
72

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;

[#54340] Wednesday, May 23, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braydon

Total Points: 0
Total Questions: 102
Total Answers: 111

Location: Sao Tome and Principe
Member since Wed, Dec 29, 2021
2 Years ago
;