ScrollTo function in AngularJS

后端 未结 9 1891
梦如初夏
梦如初夏 2020-11-27 11:43

I\'m trying to get a quick nav to work correctly. It\'s floating on the side. When they click on a link, it takes them to that ID on the page. I\'m following this guide fro

9条回答
  •  伪装坚强ぢ
    2020-11-27 12:17

    This is a better directive in case you would like to use it:

    you can scroll to any element in the page:

    .directive('scrollToItem', function() {                                                      
        return {                                                                                 
            restrict: 'A',                                                                       
            scope: {                                                                             
                scrollTo: "@"                                                                    
            },                                                                                   
            link: function(scope, $elm,attr) {                                                   
    
                $elm.on('click', function() {                                                    
                    $('html,body').animate({scrollTop: $(scope.scrollTo).offset().top }, "slow");
                });                                                                              
            }                                                                                    
        }})     
    

    Usage (for example click on div 'back-to-top' will scroll to id scroll-top):

    
    

    It's also supported by chrome,firefox,safari and IE cause of the html,body element .

提交回复
热议问题