Giter VIP home page Giter VIP logo

ngscroll's Introduction

ngScroll

Angular module for handling scroll events in your angular js applications. Some of goals of this project worth noting include:

  • Be lightweight, flexible and easy to use
  • Only bind scroll event handler one time
  • Throttle scroll events

#Usage ngScroll is composed of one factory and one directive. The factory can be injected into any controller, service, factory, or directive and be used to programatically bind scroll events in your applications. The factory can be injected and used like so:

###Factory: scroll

app.directive('someDirective', ['scroll', function(scroll){
  return{
    controller: function($scope){
      $scope.setDimensions = function(data){
        $scope.x = data.x;
        $scope.y = data.y;
      }
    },
    link: function($scope, $element, $attributes, controller){
      scroll.bind();
      $scope.$on('scroll', function(event, data){
        $scope.setDimensions(data);
      });
    }
  };
}])

####Methods

name description
checkBind()
| Check's to see if the scroll event handler has been bound. Returns boolean.
setBind()
| Binds and throttles the scroll event handler to the document - This shouldn't be used to bind events, instead use the `bind()` method to bind scroll events programatically.
bind()
| Check's to see if the scroll event handler is bound, if not then it will bind the event to document. Useful one liner to ensure event is bound. Will bind event only one time.
get()
| Gets the current scroll data. Returns scroll object.
setDate()
| Defines `Date.now()` for older browsers.

###Directive: ng-scroll The directive approach is similar to default angular directives. Something worth noting is that when the scroll event is triggered, the expression is not wrapped in $apply(). This approach was taken because scroll events happen frequently (every 30ms) and tests have shown that with many bindings (1000+) the events would be delayed as they had to wait for each digest. The goal is to provide efficient, useful access to scroll events without crippling the ui.

<div ng-scroll="setDimensions($event)"></div>

###The Scroll Object If you use the event object from the directive or the get() method from the factory you will receive an object with some scroll related properties and values.

key description
prev The previous scroll object
x Current scroll position along the X axis
y Current scroll position along the Y axis
type Event type
timestamp Timestamp when the event occured
timelapse Amount of time that has passed since the last scroll event
distanceX How far we have traveled along the X axis since the last scroll
distanceY How far we have traveled along the Y axis since the last scroll
velocityX Velocity of the scroll along X axis (distance / time)
velocityY Velocity of the scroll along Y axis (distance / time)
directionX Direction we are travelling along the X axis (left or right)
directionY Direction we are travelling along the Y axis (up or down)
scrollHeight Scrollable height of the page
event Original event object

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.