angular-backtop
Version:
1 lines • 1.14 kB
JavaScript
var backtop=angular.module("angular.backtop",[]);backtop.directive("backTop",[function(){return{restrict:"E",transclude:!0,replace:!0,template:'<div id="backtop" class="{{theme}}"><button><div ng-transclude></div></button></div>',scope:{text:"@buttonText",speed:"@scrollSpeed",theme:"@buttonTheme"},link:function(a,b){a.text=a.text||"Scroll top",a.theme=a.theme||"light",a.speed=parseInt(a.speed,10)||300;var c=Math.round(a.speed/100),d=function(){window.pageYOffset>0?b.showing||(b.addClass("show"),b.showing=!0):(b.removeClass("show"),b.showing=!1)};a.currentYPosition=function(){return document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:0},b.showing=!1,b.on("click",function(){window.removeEventListener("scroll",d),b.removeClass("show"),b.showing=!1;var e=a.currentYPosition(),f=Math.round(e/25),g=100>e?0:e-f,h=setInterval(function(){window.scrollTo(0,g),g||(clearInterval(h),window.addEventListener("scroll",d)),g-=f,0>g&&(g=0)},c)}),window.addEventListener("scroll",d),a.$on("$destroy",function(){window.removeEventListener("scroll",d)})}}}]);