{"id":133,"date":"2016-02-29T10:56:06","date_gmt":"2016-02-29T10:56:06","guid":{"rendered":"http:\/\/muthu.co\/?p=133"},"modified":"2021-01-02T14:06:03","modified_gmt":"2021-01-02T14:06:03","slug":"scroll-to-top-angular-directive","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/scroll-to-top-angular-directive\/","title":{"rendered":"Scroll to Top Angular Directive"},"content":{"rendered":"
The below directive creates a scroll to top icon on the bottom right corner of the page.\u00a0On click it will smoothly scroll you position back to top.<\/p>\n
Angular Directive:<\/p>\n
function ScrollToTop() {\r\n return {\r\n template: \"<a ng-click='click()' class='scroll-to-top-anchor'><\/a>\",\r\n replace: true,\r\n scope: true,\r\n controller: function($scope) {\r\n $scope.click = function() {\r\n var startY = currentYPosition();\r\n var stopY = 0; \/\/top is zero\r\n var distance = stopY > startY ? stopY - startY : startY - stopY;\r\n if (distance < 100) {\r\n scrollTo(0, stopY);\r\n return;\r\n }\r\n var speed = Math.round(distance \/ 100);\r\n if (speed >= 20) speed = 20;\r\n var step = Math.round(distance \/ 25);\r\n var leapY = stopY > startY ? startY + step : startY - step;\r\n var timer = 0;\r\n if (stopY > startY) {\r\n for (var i = startY; i < stopY; i += step) {\r\n setTimeout(\"window.scrollTo(0, \" + leapY + \")\", timer * speed);\r\n leapY += step;\r\n if (leapY > stopY) leapY = stopY;\r\n timer++;\r\n }\r\n return;\r\n }\r\n for (var i = startY; i > stopY; i -= step) {\r\n setTimeout(\"window.scrollTo(0, \" + leapY + \")\", timer * speed);\r\n leapY -= step;\r\n if (leapY < stopY) leapY = stopY;\r\n timer++;\r\n }\r\n \/\/$window.scrollTo(0, 0);\r\n }\r\n\r\n function currentYPosition() {\r\n \/\/ Firefox, Chrome, Opera, Safari\r\n if (self.pageYOffset) return self.pageYOffset;\r\n \/\/ Internet Explorer 6 - standards mode\r\n if (document.documentElement && document.documentElement.scrollTop)\r\n return document.documentElement.scrollTop;\r\n \/\/ Internet Explorer 6, 7 and 8\r\n if (document.body.scrollTop) return document.body.scrollTop;\r\n return 0;\r\n }\r\n }\r\n };\r\n}\r\n<\/pre>\nInject the directive into your app.<\/p>\n
(function() {\r\n 'use strict';\r\n\r\n angular\r\n .module('myApp')\r\n .directive('scrollToTop', ScrollToTop);\r\n\r\n})();\r\n<\/pre>\nCSS style for the anchor (I have added the icon image in the css itself to avoid any external file dependencies).<\/p>\n
.scroll-to-top-anchor {\r\n visibility: visible;\r\n opacity: 1;\r\n height: 60px;\r\n width: 60px;\r\n right: 30px;\r\n bottom: 30px;\r\n display: inline-block;\r\n position: fixed;\r\n z-index: 10;\r\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);\r\n overflow: hidden;\r\n text-indent: 100%;\r\n white-space: nowrap;\r\n background: #337ab7 url('data:image\/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxNy4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB3aWR0aD0iMTZweCIgaGVpZ2h0PSIxNnB4IiB2aWV3Qm94PSIwIDAgMTYgMTYiIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDE2IDE2IiB4bWw6c3BhY2U9InByZXNlcnZlIj4NCjxwb2x5Z29uIGZpbGw9IiNGRkZGRkYiIHBvaW50cz0iOCwyLjggMTYsMTAuNyAxMy42LDEzLjEgOC4xLDcuNiAyLjUsMTMuMiAwLDEwLjcgIi8+DQo8L3N2Zz4NCg==') no-repeat center 50%;\r\n color: #e86256;\r\n text-decoration: none;\r\n}\r\n<\/pre>\nThe above CSS will create an icon as shown below<\/p>\n
<\/a><\/p>\n
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.<\/p>\n
<scroll-to-top\/><\/pre>\nDemo:\u00a0https:\/\/jsfiddle.net\/muthuwz\/a7o404o4\/17\/<\/a><\/p>\n
<\/p>\n