I found a few problems with your code, specially when binding the scroll event, in angular, you make use of angular.element
to bind events to the DOM similarly to jQuery (it's jQLite, which is a light implementation of jQuery).
angular.element($window).bind('scroll', function () {});
Also, as per you are using $scope
out of angular context, you have to surround scope operations with $scope.$apply(function () {});
, which will grantee the digestion of that part of the code that is outside of angular.
angular.element($window).bind('scroll', function () {
$scope.$apply(function () {
// do scope stuff here
});
});
Finally, you can use the pageYOffset
property to toggle the visibility of your back to top button.
I've made an example using your code in order to demonstrate the usage of what I described before.
angular.module('app', [])
.controller('appCrtl', [
'$scope',
'$window',
AppCtrl
]);
function AppCtrl($scope, $window) {
$scope.goToTop = function() {
$window.scrollTo(0, 0);
};
angular.element($window).bind('scroll', function(event) {
$scope.$apply(function() {
var scroll = $scope.scroll = this.pageYOffset;
if (scroll > 100 || scroll == undefined) {
$scope.showUpArrow = true;
} else {
$scope.showUpArrow = false;
}
});
});
};
.floating {
position: fixed;
margin: 10px;
bottom: 0;
right: 0;
}
.display {
position: fixed;
margin: 0;
padding: 2px 5px;
top: 0;
width: 100%;
background-color: gray;
color: white;
}
.spacer {
padding: 10px 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.js"></script>
<div ng-app="app" ng-controller="appCrtl">
<div class="display">scrollY: {{ scroll }}</div>
<button class="floating" ng-show="showUpArrow" ng-click="goToTop()">^</button>
<div class="spacer">
<h1>HTML Ipsum Presents</h1>
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci,
sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
<h2>Header Level 2</h2>
<ol>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ol>
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus
turpis elit sit amet quam. Vivamus pretium ornare est.</p>
</blockquote>
<h3>Header Level 3</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li>Aliquam tincidunt mauris eu risus.</li>
</ul>
<pre><code>
#header h1 a {
display: block;
width: 300px;
height: 80px;
}
</code></pre>
</div>
</div>