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.
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.
- Install the plugin into your Angular.js project, manually or via
bower install angular-scroll-animate --save
- Include
angular-scroll-animate.js
in your app:
<script src="bower_components/angular-scroll-animate/dist/angular-scroll-animate.js"></script>
- Add
angular-scroll-animate
as 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
};
-
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 usingoverflow: auto
in a container and the elements are not appearing when they should set replaceclassname
with the class you have appliedoverflow: 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
andorientationchange
of thedocument
element this directive is loaded in.
- Checkout git repository locally:
git clone git@github.com:rpocklin/angular-scroll-animate.git
npm install
bower install
grunt serve
- View
http://localhost:9000/example/
in your browser to see the example.
- 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
- 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
- Get more feedback and feedback on different browsers (especially mobile / tablets).
- Add debounce timing
- Consider using events instead?
- Add some tests
Released under the MIT License. See the LICENSE file for further details.