I'm trying to create a smooth scroll effect while scrooling within a div on a click event. I kind of made it work, but I can't configure it properly.
The objective is to create kind of a carousel, when the user clicks on the buttonto go right, then only the div containing the items of the carousel will scroll horizontally.
But, when i click on the button, the scroll is way too smooth, taking about 2-3 seconds to complete.
This is the code I have:
private parentEl;
ngOnInit() {
this.parentEl = document.getElementsByClassName('carousel_wrap');
}
public goRight(): void {
const el = this.parentEl[0];
const endPosition = el.scrollLeft + this.scrollFactor; // scrollFactor is the width of the child items on the carousel
let interval = setInterval(() => {
el.scrollLeft++;
if ( el.scrollLeft === endPosition ) {
clearInterval(interval);
}
});
}
If I give the setInterval a delay, such as setInterval(() => {...}, 16); then it will take longer to complete.
What I was expecting is to execute the whole animation in a total of 300ms, for example. Does anyone knows how to solve this? Or what I'm doing wrong?