【问题标题】:$location.hash() breaking my function$location.hash() 破坏了我的功能
【发布时间】:2017-06-07 02:41:44
【问题描述】:

我即将完成我的页面,但我遇到了这个奇怪的错误。

当我使用 ng-include 时,页面加载正常,但它们会从之前的位置继续滚动,因此它们不会在每次点击时从顶部开始。

我求助于使用锚滚动,但它不能正常工作。我必须再次单击该链接,它才能加载内容,如果我再次单击同一个链接,它会将页面偏移到某个奇怪的位置。

这是我在控制器中的代码:

    $scope.toPage = function (index, id) {
    $scope.missiveIndex = index;
    $scope.contentsDown();
    $location.hash(id);
};

这是 HTML 部分:

<article id="{{articles.ids}}" class="stories-anim" ng-repeat="articles in stories" ng-hide="!isMissiveSlideIndex($index)" ng-include="articles.content" [autoscroll]>
</article>

id是通过ng-click="ng-click="toPage($index, button.ids);"传入的

有没有办法将 location.hash 放在 [onload] 表达式中并以这种方式自动滚动?

谢谢

【问题讨论】:

    标签: angularjs autoscroll


    【解决方案1】:

    可能锚滚动不起作用,因为调用时页面内容没有完全加载。

    你可以通过使用超时来解决这个问题。

    $timeout(function() {
      $anchorScroll();
    },500);
    

    【讨论】:

    • 谢谢。对于第一次加载,我设法通过实际使用 0 秒超时来破解它。如果回到上一篇,它会保留滚动位置
    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多