• Stars
    star
    1,482
  • Rank 31,713 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Scrollspy, animated scrollTo and scroll events for angular.js

angular-scroll

Angular is only dependency (no jQuery). 8K minified or 2K gzipped.

Example

Check out the live demo or the source code.

Install

With bower:

$ bower install angular-scroll

With npm (for use with browserify):

$ npm install angular-scroll

You can also download the production version or the development version.

If you prefer a CDN hosted version (which might speed up your load times), check out cdnjs.com/libraries/angular-scroll.

Don't forget to add duScroll to your module dependencies.

angular.element Scroll API

This module extends the angular.element object with a few jQuery like functions. Note that $document is an angular.element, for usage example see below. In case of name collisions existing jQuery or jqlite functions will be preserved, just use the prefixed version: ie .duScrollTo() instead of .scrollTo().

.scrollTo( left, top [, duration [, easing ] ] )

Scrolls the element/window to the specified left/top position. If duration is specified the scrolling is animated for n milliseconds. If easing is ommited the animation will default to the duScrollEasing function.

.scrollTo( element [, offset, [, duration [, easing ] ] ] )

Alias of .scrollToElement.

.scrollToElement( element [, offset, [, duration [, easing ] ] ] )

Scrolls to the specified element, if offset is passed it will be subtracted from the elements position which is good if one uses floating menus.

.scrollToElementAnimated( element [, offset, [, duration [, easing ] ] ] )

Convenience function. Works exactly the same as scrollToElement but uses the default values from duScrollOffset, duScrollDuration and duScrollEasing unless otherwise specified.

.scrollTop|scrollLeft( )

Returns current scroll position.

.scrollTop|scrollLeft( top [, duration [, easing ] ] )

Scrolls to specified position in either axis, with optional animation.

.scrollTopAnimated|scrollLeftAnimated( top [, duration [, easing ] ] )

Convenience function like scrollToElementAnimated but for scrollTop/scrollLeft.

Promises

Animated scrolling returns a $q promise, it will resolve when the scrolling has finished or be rejected if cancelled (by starting another scroll animation before it finished).

Example

angular.module('myApp', ['duScroll']).
  controller('myCtrl', function($scope, $document) {
    var top = 400;
    var duration = 2000; //milliseconds

    //Scroll to the exact position
    $document.scrollTop(top, duration).then(function() {
      console && console.log('You just scrolled to the top!');
    });

    var offset = 30; //pixels; adjust for floating menu, context etc
    //Scroll to #some-id with 30 px "padding"
    //Note: Use this in a directive, not with document.getElementById 
    var someElement = angular.element(document.getElementById('some-id'));
    $document.scrollToElement(someElement, offset, duration);
  }
);

The above example can be achieved by configuration instead of arguments:

angular.module('myApp', ['duScroll'])
  .value('duScrollDuration', 2000)
  .value('duScrollOffset', 30)
  .controller('myCtrl', function($scope, $document) {
    $document.scrollTopAnimated(400).then(function() {
      console && console.log('You just scrolled to the top!');
    });

    var someElement = angular.element(document.getElementById('some-id'));
    $document.scrollToElementAnimated(someElement);
  }
);

Directives

du-smooth-scroll

Provides smooth anchor scrolling.

<a href="#anchor" du-smooth-scroll>Scroll it!</a>

If you, for some reason, do not want to use the href attribute as fallback, just use the du-smooth-scroll attribute instead but without leading #. Example: <a du-smooth-scroll="anchor">.

du-scrollspy

Observes whether the target element is at the top of the viewport (or container) and adds an active class if so. Takes optional offset and duration attributes which is passed on to .scrollTo,

<a href="#anchor" du-scrollspy>Am i active?</a>

or together with Bootstrap

<ul class="nav navbar-nav">
  <li du-scrollspy="anchor"><a href="#anchor">Link</a></li>
</ul>

du-spy-context

Enables multiple sets of spies on the same target element. Takes optional offset attribute to

<ul du-spy-context class="nav navbar-nav">
  <li du-scrollspy="anchor"><a href="#anchor">Link</a></li>
</ul>
<ul du-spy-context class="nav navbar-nav">
  <li du-scrollspy="anchor"><a href="#anchor">Link</a></li>
</ul>

du-scroll-container

Modifies behavior of du-scrollspy and du-smooth-scroll to observe/scroll within and element instead of the window/document. Good for modals/elements with overflow: auto/scroll.

<div du-scroll-container>
  <p id="top">This is the top</p>
  <p id="anchor">Scroll to me, or <a href="#top" du-smooth-scroll>the top</a></p>
</div>

If your links lie outside of the scrollable element, wrap them with the du-scroll-container directive and send the element id as argument:

<ul du-scroll-container="scroll-container">
  <li><a href="#anchor" du-smooth-scroll>Link</a></li>
</ul>
<div id="scroll-container">
  [...]
</div>

All in together now

The directives play well together, try the demo or inspect its source code.

<ul du-spy-context du-scroll-container="scroll-container">
  <li><a href="#anchor" offset="30" du-smooth-scroll du-scrollspy>Link</a></li>
</ul>
<ul du-spy-context du-scroll-container="scroll-container">
  <li><a href="#anchor" offset="30" du-smooth-scroll du-scrollspy>Link</a></li>
</ul>
<div id="scroll-container">
  [...]
</div>

Observing Scroll Position

NOTE: the $duScrollChanged event and the scrollPosition service are deprecated. Use angular.element().on() together with .scrollTop() instead.

angular.module('myApp', ['duScroll']).
  controller('MyCtrl', function($scope, $document){
    $document.on('scroll', function() {
      console.log('Document scrolled to ', $document.scrollLeft(), $document.scrollTop());
    });
    var container = angular.element(document.getElementById('container'));
    container.on('scroll', function() {
      console.log('Container scrolled to ', container.scrollLeft(), container.scrollTop());
    });
  }
);

Configuration

Scroll speed

Duration is defined in milliseconds.

To set a scroll duration on a single anchor:

<a href="#anchor" du-smooth-scroll duration="5000">Scroll it!</a>

To change the default duration:

angular.module('myApp', ['duScroll']).value('duScrollDuration', 5000);

Scroll easing

Set the duScrollEasing value to a function that takes and returns a value between 0 to 1. Here's a few examples to choose from.

function invertedEasingFunction(x) {
  return 1-x;
}
angular.module('myApp', ['duScroll']).value('duScrollEasing', invertedEasingFunction);

You can also pass a custom easing function as the fourth argument in scrollTo.

Debounce Scroll Events

Set the duScrollSpyWait value in milliseconds to debounce the handler and prevent it from triggering frequent events and increase performance for large pages and/or navigations with expanding nodes.

angular.module('myApp', ['duScroll']).value('duScrollSpyWait', 1000);

Greedy option

Set the duScrollGreedy value to true if the elements you are observing are not wrapping the whole section you want to observe, but merely the first one in the section (such as headlines).

angular.module('myApp', ['duScroll']).value('duScrollGreedy', true);

Offset

To change default offset (in pixels) for the du-smooth-scroll directive:

angular.module('myApp', ['duScroll']).value('duScrollOffset', 30);

When to cancel scroll animation

Specify on which events on the container the scroll animation should be cancelled by modifying duScrollCancelOnEvents, set to false to disable entirely as shown below. Defaults to scroll mousedown mousewheel touchmove keydown.

angular.module('myApp', ['duScroll']).value('duScrollCancelOnEvents', false);

Bottom spy

To make the last du-scrollspy link active when scroll reaches page/container bottom:

angular.module('myApp', ['duScroll']).value('duScrollBottomSpy', true);

Active class

Specify the active class name to apply to a link when it is active, default is active.

angular.module('myApp', ['duScroll']).value('duScrollActiveClass', 'custom-class');

Events

The duScrollspy directive fires the global events duScrollspy:becameActive and duScrollspy:becameInactive with an angular.element wrapped element as first argument and the element being spied on as second. This is nice to have if you want the URL bar to reflect where on the page the visitor are, like this:

angular.module('myApp', ['duScroll']).
  run(function($rootScope) {
    if(!window.history || !history.replaceState) {
      return;
    }
    $rootScope.$on('duScrollspy:becameActive', function($event, $element, $target){
      //Automaticly update location
      var hash = $element.prop('hash');
      if (hash) {
        history.replaceState(null, null, hash);
      }
    });
  });

Building

$ npm install
$ bower install
$ gulp

Tests

Unit tests

$ npm test

End to end tests

$ npm run update-webdriver
$ npm run protractor

More Repositories

1

react-native-vector-icons

Customizable Icons for React Native with support for image source and full styling.
JavaScript
17,427
star
2

react-native-animatable

Standard set of easy to use animations and declarative transitions for React Native
JavaScript
9,838
star
3

react-native-progress

Progress indicators and spinners for React Native
JavaScript
3,628
star
4

hush

🀫 Noiseless Browsing – Content Blocker for Safari
JavaScript
3,264
star
5

react-native-keychain

πŸ”‘ Keychain Access for React Native
Kotlin
3,194
star
6

react-native-lightbox

Images etc in Full Screen Lightbox Popovers for React Native
JavaScript
2,818
star
7

react-native-collapsible

Animated collapsible component for React Native, good for accordions, toggles etc
JavaScript
2,450
star
8

loki

πŸ‘ Visual Regression Testing for Storybook
JavaScript
1,778
star
9

react-native-image-progress

Progress indicator for networked images in React Native
JavaScript
1,723
star
10

react-native-performance

πŸ“ Monitor and measure React Native performance
TypeScript
929
star
11

react-native-store-review

Rate on App/Play Store directly in your React Native app
Java
751
star
12

react-native-shimmer

Simple shimmering effect for any view in React Native
Java
700
star
13

react-native-esbuild

Fast bundler and dev server for react-native using esbuild
JavaScript
595
star
14

react-native-parallax

Parallax effects for React Native using Animated API
JavaScript
585
star
15

react-native-vector-image

iOS/Android native vector assets generated from SVG
JavaScript
310
star
16

react-native-pinchable

Instagram like pinch to zoom for React Native
Java
225
star
17

angular-parallax

Lightweight & performant parallax scrolling for angular.js.
JavaScript
203
star
18

angular-lazytube

Lightweight, responsive, lazy loaded YouTube videos that degrades gracefully.
JavaScript
45
star
19

diglett

Keep your JS project lean by detecting duplicate dependencies
JavaScript
44
star
20

esbuild-server

Fast, lightweight and powerful development server for esbuild
TypeScript
38
star
21

react-native-oxc

10x your react-native builds with OXC
TypeScript
28
star
22

oblador.github.io

Landing page for my GitHub projects
HTML
10
star
23

oblador

5
star
24

node-cloud-imager

Powerful yet simple way to apply filters/crop/resize images and upload to cloud providers (Amazon S3, Rackspace, Azure).
JavaScript
3
star
25

node-ups-sdk

JavaScript
3
star
26

sovra

🀏 Rust-based Test Decider for JavaScript using Oxc
Rust
2
star
27

expo-requestidlecallback-issue

TypeScript
1
star
28

react-native-android-autolinking-bug-repro

TypeScript
1
star
29

.github

1
star
30

react-native-fabric-image-unicode-issue

TypeScript
1
star