Angular detect when scroll reached certain point

javascript detect scroll to bottom of page
angular detect scroll to bottom of div
angular scroll event
show div on scroll position javascript
angular 7 scroll event
jquery trigger event when scroll to div
javascript horizontal scroll event
scroll tracking javascript

I'm trying to achieve the following effect: The first container, which has among others a class called .top-container, has a height of about one and a half pages. In the beginning, the user shall be able to scroll until the end of this container, but as soon as he reaches it, the container shall become fixed and the following elements are supposed to overlap it by scrolling.

My approach was to detect when the user reaches top-container's end, in order to then change its position to fixed. But how do I detect when the user has reached a certain scroll point?

Are there other approached to achieve the desired effect?

My code:

<div class="container-fluid px-0 top-container">

  <div class="container-fluid bg-test justify-content-center min-vh-100 d-flex">
    <h1 class="align-self-center">PURE</h1>
  </div>

  <div class="container-fluid justify-content-center d-flex collections" *ngIf="collections">
    <div class="row align-self-center justify-content-around">
      <div class="col-12 mb-5 text-center">
        <h1>ALL COLLECTIONS</h1>
      </div>
      <div class="col-3" *ngFor="let collection of collections">
        <div class="flex-row">
          <div class="col-12 text-center">
            <h4>{{collection.name}}</h4>
            <p>- {{collection.desc}} -</p>
          </div>
          <div class="col-12 d-flex justify-content-center">
            <img [src]="collection.image" [alt]="collection.name" class="w-75 align-self-center">
          </div>
        </div>
      </div>
    </div>
  </div>

</div>

<div class="padding-top">
  <app-slider></app-slider>
  <app-presentation></app-presentation>
</div>

CSS:

.top-container {
  background-color: #EDEDED;

  .collections {
    padding: 5%;
    background-color: white;
    h1 {
      font-family: "Zuric Light";
      text-transform: uppercase;
      color: #D3072A;
    }
    h4 {
      font-family: "Zuric Light";
      text-transform: uppercase;
    }
    p {
      font-family: "Zuric Light";
    }
  }

  .bg-test {
    background-image: url("/assets/images/products/pure/pure-upper-side-red.png");
    background-position: bottom left;
    background-size: contain;
    background-repeat: no-repeat;

    h1 {
      line-height: 1;
      margin: 0;
      font-family: "Zuric Bold";
      color: white;
      font-size: 200px;
      text-transform: uppercase;
      &::first-letter {
        margin-left: -0.05em;
      }
    }
    p {
      color: white;
      font-family: "Zuric Light";
      font-size: 30px;
    }
  }
}

try something like this to get scroll position, by creating an event observable using fromEvent and use sampleTime to prevent having thousands of values

import { fromEvent } from 'rxjs';
import { map, sampleTime } from 'rxjs/operators'; 
...
ngAfterViewInit(): void {
  const tracker = document.querySelector('.yourElement'); // get your element by css class
  const scroll$ = fromEvent(tracker, 'scroll').pipe(
   sampleTime(300), // to prevent getting multiple values in a milisecond
   map(() => tracker.scrollTop)
  );
  let scrollSubscription = windowYOffsetObservable.subscribe((scrollPos) => {
    console.log(scrollPos); // get scroll position
    // you can do your check here
  });
}

Check if a user has scrolled to the bottom, How can I see the scroll reached at the bottom? Detect Scroll Direction Using Angular 2. Scrolling can be detected by binding scroll event to window. To determine the scroll direction we need to compare the present scroll position with previous scroll position. Source Code DEMO.

Try to listen to the scroll event with @HostListener until the end of your container. Then, call your function.

Bootstrap JS Scrollspy Reference, position. Tip: The Scrollspy plugin is often used together with the Affix plugin. Detect When User Scrolls To Bottom of Page with Angular 2 | SodhanaLibrary Detect When User Scrolls To Bottom of Page with Angular 2 Whenever user scroll to the bottom, we can show Go to top button to make user reached to top of the page. This program will help you to find out bottom of the page.

This is a good job for Intersection Observer (IO). With it you can check how much an element intersects with another element or the screen. Listening to scroll events and calculating the position of an element can result in bad performance, so using IO is recommended approach for such problems.

First you have to define your options for your IO (you can have more than one IO)

let options = {
  rootMargin: '0px',
  threshold: 1.0
}

let observer = new IntersectionObserver(callback, options);

Second, you define which element(s) to watch:

let target = document.querySelector('#yourHeader');
observer.observe(target);

Final step is then to define the callback function and what should happen when the element comes into view/leaves the view:

let callback = (entries, observer) => { 
  entries.forEach(entry => {
    // Each entry describes an intersection change for one observed
    // target element
  });
};

You can also use this polyfill from w3c to support older browsers if needed.

Change css header after scrolling, 100px from the top if ( $(window). scrollTop() >= 100 ) { $('#logo'). Ionic 5|4 How to Scroll to Top ,Bottom, to Element with Ion Content Scroll Events Last updated on May 3, 2020 Jolly.exe In this Ionic tutorial, we’ll discuss Scroll events and methods available on the ion-content component in the Ionic Framework.

How to detect scroll to bottom of html element, I think that all you want to do is detect the position of scroll. //In chrome and some browser scroll is given to body tag let pos = (document. Also don't forget to import HostListener from @angular/core. clientHeight; console.log(scrollPos, limit); if (scrollPos === limit) { alert('end reached'); } }); }. Update. This feels like something that would not work properly if native momentum scroll kicks in. (I might be wrong. Haven’t tested it.) So if my “touchend” (end of scroll) occures below the magic 147px (or whatever your number is) but the momentum scrolls the window all the way to the top the “fix-search”-class will not be removed.

Angular 2 detect scroll to bottom of div, Check If DIV is Overflowing Elements or Not Using Angular 2 Friday, 14 October It allows us to make elements stick when the scroll reaches a certain point. [ ] bug report [x] feature request [ ] support request. Current behavior. I've done a research to find a functionality within Angular 2 framework to detect the vertical position of the scroll bar so I can trigger animations in certain breakpoints, but the only helpful solution was a work around using Renderer class.

How to detect when user scrolls to the bottom of a div, The task is to detect the bottom of the <div> element when user scrolls to the bottom This method adds one or more event handlers for the selected elements and Example 2: This example alerts End of DIV is reached! when the user scrolls to How to Change Image Dynamically when User Scrolls using JavaScript ? The scrollIntoView() method scrolls the specified element into the visible area of the browser window. Browser Support The numbers in the table specifies the first browser version that fully supports the method.

Comments
  • Thanks. Sorry for the dumb question - but where do I put that code? Since simply adding it e.g. AfterViewInit or a HostListener won't log anything.
  • How do I listen to a @HostListener only until the end of a container?