JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-app="myApp">
<p>Top</p>
<div>
<a scroll-to="section1">Goto Section 1</a>
</div>
<div style="padding-top:1000px">...</div>
<div id="section1">
<h2>Section1</h2>
<a scroll-to="">Back to Top</a>
</div>
</div>
JavaScript
// Version 0.0.2
// AngularJS simple hash-tag scroll alternative
// this directive uses click event to scroll to the target element
//
// <div ng-app="app">
// <div ng-controller="myCtrl">
// <a scroll-to="section1">Section 1</a>
// </div>
// ...
// <div id="section1">
// <h2>Section1</h2>
// <a scroll-to="">Back to Top</a>
// </div>
// </div>
//
// angular.module('app', ['ngScrollTo']);
angular.module("ngScrollTo",[])
.directive("scrollTo", ["$window", function($window){
return {
restrict : "AC",
compile : function(){
var document = $window.document;
function scrollInto(idOrName) {//find element with the give id of name and scroll to the first element it finds
if(!idOrName)
$window.scrollTo(0, 0);
//check if an element can be found with id attribute
var el = document.getElementById(idOrName);
if(!el) {//check if an element can be found with name attribute if there is no such id
el = document.getElementsByName(idOrName);
if(el && el.length)
el = el[0];
else
el = null;
}
if(el) //if an element is found, scroll to the element
el.scrollIntoView();
//otherwise, ignore
}
return function(scope, element, attr) {
element.bind("click", function(event){
scrollInto(attr.scrollTo);
});
};
}
};
}]);
angular.module("myApp", ["ngScrollTo"]);