【问题标题】:How to scroll to div in angularjs如何在angularjs中滚动到div
【发布时间】:2017-08-04 15:24:54
【问题描述】:

当我点击一个按钮时,我试图滚动到我的标签栏

我的 html(标签)

<div class="col-xs-12" id="tabs">
  ---All the code here----  
</div>

我的js,

  function ScrollTopForTabs(){
    $timeout(function () {
         $('html, body').animate({
            scrollTop: 650
        }, 1000);
    }, 1000);
  }

到目前为止这还不错,但给了我一个固定的高度。谁能建议我如何直接滚动到div。谢谢。

【问题讨论】:

标签: javascript html angularjs scroll mean


【解决方案1】:

是 Angular 中的一项特殊服务,AnchorScroll。

这是 Angular 文档中的一个示例:

<div id="scrollArea" ng-controller="ScrollController">
  <a ng-click="gotoBottom()">Go to bottom</a>
  <a id="bottom"></a> You're at the bottom!
</div>

angular.module('anchorScrollExample', [])
.controller('ScrollController', ['$scope', '$location', '$anchorScroll',
  function($scope, $location, $anchorScroll) {
    $scope.gotoBottom = function() {
      // set the location.hash to the id of
      // the element you wish to scroll to.
      $location.hash('bottom');

      // call $anchorScroll()
      $anchorScroll();
    };
  }]);

【讨论】:

    【解决方案2】:

    试试这个代码

    $("#moveup").click(function(){
      $("html, body").stop().animate({
        scrollTop: $('#tabs').offset().top - 40
      }, '500', 'linear');
    });
    #tabs{
      height:900px;
      background-color:#ccc;
    }
    #moveup{
      position:fixed;
      font-size:24px;
      bottom:25px;
      right:25px;
      height:10px;
      width:10px;
      color:red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    <div class="col-xs-12" id="tabs">
      ---All the code here----  
      <a id="moveup">up</a>
    </div>

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 1970-01-01
      • 2017-03-02
      • 2014-05-25
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多