Skip to content

Instantly share code, notes, and snippets.

@sean-hill
Last active October 11, 2023 20:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save sean-hill/1b9388ebec49668565e1 to your computer and use it in GitHub Desktop.
Save sean-hill/1b9388ebec49668565e1 to your computer and use it in GitHub Desktop.
AngularJS Back To Top Directive
AngularJS Back To Top Directive.
Uses AngularJS, jQuery, Font Awesome, and SCSS.
// Back to top Directive
angular.module('app.directives')
.directive('backToTop', function(){
return {
restrict: 'E'
, replace: true
, template: '<div class="back-to-top"><i class="fa fa-chevron-up"></i></div>'
, link: function($scope, element, attrs) {
$(window).scroll(function(){
if ($(window).scrollTop() <= 0) {
$(element).fadeOut();
}
else {
$(element).fadeIn();
}
});
$(element).on('click', function(){
$('html, body').animate({ scrollTop: 0 }, 'fast');
});
}
}
})
// Back to top styles
.back-to-top {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 1;
width: 50px;
height: 50px;
background: #DDD;
opacity: 0.5;
display: none;
&:hover {
opacity: 1;
cursor: pointer;
}
i {
font-size: 25px;
padding: 12px;
color: #FFF;
}
}
@alexdbondoc
Copy link

How to apply this directive in my UI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment