Coder Social home page Coder Social logo

alirezamirmahdi / sammscroll.service Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 945 KB

scroll service with angular

License: MIT License

JavaScript 19.88% CSS 15.15% TypeScript 61.03% HTML 3.94%
angular angular4 angular-service angular-scroll scroll-service angular-parallax parallax-scrolling parallax parallax-effect

sammscroll.service's Introduction

sammScroll.Service

This is an angular service to trace scroll event to subscribe all elements which registered in it. With this service you have only on event listener and many elements waiting for browser to meet a special height.

Demo

http://mirmahdi.pro/projects/sammscroll

How it works

Every element which needs to be subscribed in a special height can be registered in this service with setEventListener() and gets an observable object and Id (needs to remove listener). When the user scrolls, this service checks the list of heights and for every height which is reached the number will be subscribed and removed from the list.

What should you do

The scroll.service file address is : src/app/services/sammScrollSrv/sammScroll.service.ts

Firstly, import the service in your module :

import { SammScrollService } from ../../services/sammScrollSrv/sammScroll.service’;

@NgModule({
    imports: [
        /* Other imports here */
      ],
providers: [
/* Other imports here */
        SammScrollService
    ]
})
export class AppModule {
}

Then import it in your component and use AfterViewInit Interface :

import { Component, OnInit, AfterViewInit, ElementRef, Input } from '@angular/core';
import { SammScrollService } from '../../../services/sammScrollSrv/sammScroll.service';
.
.
.
.
export class PipeNodeComponent implements AfterViewInit {

constructor(private scrollSrv : SammScrollService) { }

ngAfterViewInit(): void { 
        var pageOffset = (window.innerHeight / 20 )* 19;
        var listenY = this.elm.nativeElement.getBoundingClientRect().top + window.pageYOffset;
        if(listenY <= pageOffset) 
        {
            setTimeout(() => {
                this.NodeCurrentState = "active"; 
            }, 100);
        } else {
             this.ScrollEventInfo = this.scrollSrv.setEventListener(listenY - pageOffset);
            this.ScrollEventInfo.eventObserver.subscribe(
                () => { 
                    this.NodeCurrentState = "active"; 
                }
            );
        }
    }
}

I used pageOffset variable to set the event trigger in 19/20 of the screen.

sammscroll.service's People

Contributors

alirezamirmahdi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.