Skip to content

An Angular.js directive which allows you to perform any javascript actions (in the controller, or on the element) when an element is scrolled into, or out of, the users viewport, without any other dependencies.

License

Notifications You must be signed in to change notification settings

rpocklin/angular-scroll-animate

Repository files navigation

Angular Scroll Animate

An Angular.js directive which allows you to perform any javascript actions (in the controller, or on the element) when an element is scrolled into or out of, the users viewport, without requiring any other dependencies.

Build Status

Motivation

ngAnimate is great if you want animations based on showing or hiding elements based on some behaviour, but what if you want to trigger behaviour when an element is scrolled into, or out of the user's view?

The goal of this directive is to be small and focused around behaviour that changes when scrolled in and out of view, without requiring jQuery. You can add / remove CSS classes in the callbacks, or execute any arbitrary javascript you want such as pre-loading of data, images or anything else.

Demo

Demo

NG Docs

link

Inspiration

Installation

  1. Install the plugin into your Angular.js project, manually or via

bower install angular-scroll-animate --save

  1. Include angular-scroll-animate.js in your app:

<script src="bower_components/angular-scroll-animate/dist/angular-scroll-animate.js"></script>

  1. Add angular-scroll-animate as a new module dependency in your angular app.

var myapp = angular.module('myapp', ['angular-scroll-animate']);

  1. Ensure you have a CSS class to mask the visibility of an element eg.
.not-visible {
  visibility: hidden;
}

Add this to the elements class if you want it to be hidden initially when out of a user's view, and remove it on the animateElementIn callback and add it back on animateElementOut callback.

Example markup:

  <!-- angular view -->
  <div ng-repeat="car in cars"
    when-visible="animateElementIn"
    when-not-visible="animateElementOut" class="hidden car">
  ...
  </div>
  // controller
  $scope.cars = [ ... ];

  $scope.animateElementIn = function($el) {
  $el.removeClass('hidden');
  $el.addClass('animated fadeInUp'); // this example leverages animate.css classes
};

$scope.animateElementOut = function($el) {
  $el.addClass('hidden');
  $el.removeClass('animated fadeInUp'); // this example leverages animate.css classes
};

Notes

  • when-visible($el): [required] function (executed in the controller scope) which is called when the element is scrolled into view.

  • when-not-visible($el): [optional] function (executed in the controller scope) which is called when the element is moved out of view via scrolling.

  • delay-percent="0.50": [optional] decimal value which represents how much of the element should be in the users viewport before triggering the bound callback. 0.25 is set as a default, a lower value will make it more eager, a higher value will make it lazier.

  • bind-scroll-to=".classname": [optional] If you are using overflow: auto in a container and the elements are not appearing when they should set replace classname with the class you have applied overflow: auto to.

  • To ensure fast CSS3 transition rules are used for animations, I recommend either velocity.js or animate.css which come with many pre-built and tested CSS animation classes.

  • Default event bindings are on scroll resize and orientationchange of the document element this directive is loaded in.

Running Locally

  1. Checkout git repository locally: git clone git@github.com:rpocklin/angular-scroll-animate.git
  2. npm install
  3. bower install
  4. grunt serve
  5. View http://localhost:9000/example/ in your browser to see the example.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Beautify (grunt beautify)
  4. Ensure it passes code-checks / tests (grunt)
  5. Commit your changes (git commit -am 'Added some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create a new Pull Request

History

  • 0.9.9 Changed .hidden class in example to .not-visible to avoid CSS clashes.
  • 0.9.8 Added override attribute of bind-scroll-to to bind scroll events to custom parents - see #3).
  • 0.9.8 Optimised reflow / repaints to use requestAnimationFrame for performance reasons.
  • 0.9.4 Re-fixed when-not-visible so it is truely optional (thanks @jagged3dge)
  • 0.9.3 Changed Number.isNaN to use !angular.isNumber instead (original function not available in all browsers yet) (see #2).
  • 0.9.2 Fixed error when not defining when-not-visible attributes and updated NG Docs (see #1).
  • 0.9.1 Removed (incorrectly) namespaced events, not supported in JQ-Lite.
  • 0.9.0 Fixed event unbinding when removing bound elements from the DOM.
  • 0.8.0 Initial release

TODO

  • Get more feedback and feedback on different browsers (especially mobile / tablets).
  • Add debounce timing
  • Consider using events instead?
  • Add some tests

License

Released under the MIT License. See the LICENSE file for further details.

About

An Angular.js directive which allows you to perform any javascript actions (in the controller, or on the element) when an element is scrolled into, or out of, the users viewport, without any other dependencies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published