ScrollTo function in AngularJS

后端 未结 9 1831
梦如初夏
梦如初夏 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 11:55

    An angular solution using $anchorScroll http://www.benlesh.com/2013/02/angular-js-scrolling-to-element-by-id.html:

    app.controller('MainCtrl', function($scope, $location, $anchorScroll) {
      var i = 1;
    
      $scope.items = [{ id: 1, name: 'Item 1' }];
    
      $scope.addItem = function (){
        i++;
        //add the item.
        $scope.items.push({ id: i, name: 'Item ' + i});
        //now scroll to it.
        $location.hash('item' + i);
        $anchorScroll();
      };
    });
    

    And here is a plunk: http://plnkr.co/edit/xi2r8wP6ZhQpmJrBj1jM?p=preview

    And if you care for a pure javascript solution, here is one:

    Invoke runScroll in your code with parent container id and target scroll id:

    function runScroll(parentDivId,targetID) {
        var longdiv;
        longdiv = document.querySelector("#" + parentDivId);
        var div3pos = document.getElementById(targetID).offsetTop;
        scrollTo(longdiv, div3pos, 600);
    }
    
    
    function scrollTo(element, to, duration) {
        if (duration < 0) return;
        var difference = to - element.scrollTop;
        var perTick = difference / duration * 10;
    
        setTimeout(function () {
            element.scrollTop = element.scrollTop + perTick;
            if (element.scrollTop == to) return;
            scrollTo(element, to, duration - 10);
        }, 10);
    }
    

    Reference: Cross browser JavaScript (not jQuery...) scroll to top animation

    0 讨论(0)
  • 2020-11-27 12:03

    Here is a simple directive that will scroll to an element on click:

    myApp.directive('scrollOnClick', function() {
      return {
        restrict: 'A',
        link: function(scope, $elm) {
          $elm.on('click', function() {
            $("body").animate({scrollTop: $elm.offset().top}, "slow");
          });
        }
      }
    });
    

    Demo: http://plnkr.co/edit/yz1EHB8ad3C59N6PzdCD?p=preview

    For help creating directives, check out the videos at http://egghead.io, starting at #10 "first directive".

    edit: To make it scroll to a specific element specified by a href, just check attrs.href.

    myApp.directive('scrollOnClick', function() {
      return {
        restrict: 'A',
        link: function(scope, $elm, attrs) {
          var idToScroll = attrs.href;
          $elm.on('click', function() {
            var $target;
            if (idToScroll) {
              $target = $(idToScroll);
            } else {
              $target = $elm;
            }
            $("body").animate({scrollTop: $target.offset().top}, "slow");
          });
        }
      }
    });
    

    Then you could use it like this: <div scroll-on-click></div> to scroll to the element clicked. Or <a scroll-on-click href="#element-id"></div> to scroll to element with the id.

    0 讨论(0)
  • 2020-11-27 12:03

    In order to animate to a specific element inside a scroll container (fixed DIV)

    /*
        @param Container(DIV) that needs to be scrolled, ID or Div of the anchor element that should be scrolled to
        Scrolls to a specific element in the div container
    */
    this.scrollTo = function(container, anchor) {
        var element = angular.element(anchor);
        angular.element(container).animate({scrollTop: element.offset().top}, "slow");
    }
    
    0 讨论(0)
  • 2020-11-27 12:06

    What about angular-scroll, it's actively maintained and there is no dependency to jQuery..

    0 讨论(0)
  • 2020-11-27 12:09

    I used andrew joslin's answer, which works great but triggered an angular route change, which created a jumpy looking scroll for me. If you want to avoid triggering a route change,

    myApp.directive('scrollOnClick', function() {
      return {
        restrict: 'A',
        link: function(scope, $elm, attrs) {
          var idToScroll = attrs.href;
          $elm.on('click', function(event) {
            event.preventDefault();
            var $target;
            if (idToScroll) {
              $target = $(idToScroll);
            } else {
              $target = $elm;
            }
            $("body").animate({scrollTop: $target.offset().top}, "slow");
            return false;
          });
        }
      }
    });
    
    0 讨论(0)
  • 2020-11-27 12:09

    Another suggestion. One directive with selector.

    HTML:

    <button type="button" scroll-to="#catalogSection">Scroll To</button>
    

    Angular:

    app.directive('scrollTo', function () {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                element.on('click', function () {
    
                    var target = $(attrs.scrollTo);
                    if (target.length > 0) {
                        $('html, body').animate({
                            scrollTop: target.offset().top
                        });
                    }
                });
            }
        }
    });
    

    Also notice $anchorScroll

    0 讨论(0)
提交回复
热议问题