Last active
October 11, 2023 20:07
-
-
Save sean-hill/1b9388ebec49668565e1 to your computer and use it in GitHub Desktop.
AngularJS Back To Top Directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AngularJS Back To Top Directive. | |
Uses AngularJS, jQuery, Font Awesome, and SCSS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
}); | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
How to apply this directive in my UI?