in Angularjs, Javascript

Scroll to Top Angular Directive

The below directive creates a scroll to top icon on the bottom right corner of the page. On click it will smoothly scroll you position back to top.

Angular Directive:

function ScrollToTop() {
    return {
        template: "<a ng-click='click()' class='scroll-to-top-anchor'></a>",
        replace: true,
        scope: true,
        controller: function($scope) {
            $scope.click = function() {
                var startY = currentYPosition();
                var stopY = 0; //top is zero
                var distance = stopY > startY ? stopY - startY : startY - stopY;
                if (distance < 100) {
                    scrollTo(0, stopY);
                    return;
                }
                var speed = Math.round(distance / 100);
                if (speed >= 20) speed = 20;
                var step = Math.round(distance / 25);
                var leapY = stopY > startY ? startY + step : startY - step;
                var timer = 0;
                if (stopY > startY) {
                    for (var i = startY; i < stopY; i += step) {
                        setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
                        leapY += step;
                        if (leapY > stopY) leapY = stopY;
                        timer++;
                    }
                    return;
                }
                for (var i = startY; i > stopY; i -= step) {
                    setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
                    leapY -= step;
                    if (leapY < stopY) leapY = stopY;
                    timer++;
                }
                //$window.scrollTo(0, 0);
            }

            function currentYPosition() {
                // Firefox, Chrome, Opera, Safari
                if (self.pageYOffset) return self.pageYOffset;
                // Internet Explorer 6 - standards mode
                if (document.documentElement && document.documentElement.scrollTop)
                    return document.documentElement.scrollTop;
                // Internet Explorer 6, 7 and 8
                if (document.body.scrollTop) return document.body.scrollTop;
                return 0;
            }
        }
    };
}

Inject the directive into your app.

(function() {
    'use strict';

    angular
        .module('myApp')
        .directive('scrollToTop', ScrollToTop);

})();

CSS style for the anchor (I have added the icon image in the css itself to avoid any external file dependencies).

.scroll-to-top-anchor {
    visibility: visible;
    opacity: 1;
    height: 60px;
    width: 60px;
    right: 30px;
    bottom: 30px;
    display: inline-block;
    position: fixed;
    z-index: 10;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    text-indent: 100%;
    white-space: nowrap;
    background: #337ab7 url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxNy4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB3aWR0aD0iMTZweCIgaGVpZ2h0PSIxNnB4IiB2aWV3Qm94PSIwIDAgMTYgMTYiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDE2IDE2IiB4bWw6c3BhY2U9InByZXNlcnZlIj4NCjxwb2x5Z29uIGZpbGw9IiNGRkZGRkYiIHBvaW50cz0iOCwyLjggMTYsMTAuNyAxMy42LDEzLjEgOC4xLDcuNiAyLjUsMTMuMiAwLDEwLjcgIi8+DQo8L3N2Zz4NCg==') no-repeat center 50%;
    color: #e86256;
    text-decoration: none;
}

The above CSS will create an icon as shown below

sr2

Now for the last part! All you need to create a scroll to top icon is to place the below code somewhere at the end of your html page.

<scroll-to-top/>

Demo: https://jsfiddle.net/muthuwz/a7o404o4/17/

 

Adapted from the Anchor Smooth Scroll – Angular Directive by Justin.
https://gist.github.com/justinmc/d72f38339e0c654437a2

 

 

Write a Comment

Comment