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.
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
NG Docs
Inspiration
Installation
- Install the plugin into your Angular.js project, manually or via
bower install angular-scroll-animate --save
- Include
angular-scroll-animate.jsin your app:
<script src="bower_components/angular-scroll-animate/dist/angular-scroll-animate.js"></script>
- Add
angular-scroll-animateas a new module dependency in your angular app.
var myapp = angular.module('myapp', ['angular-scroll-animate']);
- 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.25is 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 usingoverflow: autoin a container and the elements are not appearing when they should set replaceclassnamewith the class you have appliedoverflow: autoto. -
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
scrollresizeandorientationchangeof thedocumentelement this directive is loaded in.
Running Locally
- Checkout git repository locally:
git clone [email protected]:rpocklin/angular-scroll-animate.git npm installbower installgrunt serve- View
http://localhost:9000/example/in your browser to see the example.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Beautify (
grunt beautify) - Ensure it passes code-checks / tests (
grunt) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
History
- 0.9.9 Changed
.hiddenclass in example to.not-visibleto avoid CSS clashes. - 0.9.8 Added override attribute of
bind-scroll-toto bind scroll events to custom parents - see #3). - 0.9.8 Optimised reflow / repaints to use
requestAnimationFramefor performance reasons. - 0.9.4 Re-fixed
when-not-visibleso it is truely optional (thanks @jagged3dge) - 0.9.3 Changed
Number.isNaNto use!angular.isNumberinstead (original function not available in all browsers yet) (see #2). - 0.9.2 Fixed error when not defining
when-not-visibleattributes 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.