Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atefBB/41acec1db8a69003a8b8d7c7e57bb5a4 to your computer and use it in GitHub Desktop.
Save atefBB/41acec1db8a69003a8b8d7c7e57bb5a4 to your computer and use it in GitHub Desktop.
AngularJS Back To Top Directive
// 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment