代码之家  ›  专栏  ›  技术社区  ›  Gavrilo Adamovic

当网页上显示的数据发生变化时,如何使网页不滚动到顶部

  •  0
  • Gavrilo Adamovic  · 技术社区  · 8 年前

    很抱歉这个奇怪的标题,但找不到更好的解释。

    我有spring webapp,在一个页面上有类似社交网络的内容。在里面 angularjs 控制器我有一个数组,我在其中存储帖子以显示在视图中。因此 ng-repeat

    这是我的代码的缩短版本。

    html:

        <div class="main" ng-model="activities" ng-repeat="activity in activities">
            <div class="helper" >
            <center>
                <p><img alt="{{activity.user.username}}" ng-src="pictures/{{activity.user.picture}}" height="50px"> </img> </p>
                <p><a href="#" >{{activity.user.username}} </a></p>
                <br />
                <p class="date">{{activity.activity.datum}} </p>
                <p>
                {{activity.activity.tempo}}/km {{activity.activity.distance}} km</p>
                <br />
            </center>
            </div>
            <br/><br/>
        </div>
        <center><a href="#" ng-click="loadMore()">Load more</a></center>
    </div>
    

        $scope.loadMore = function(){
            $scope.getActivities(3 , $scope.currentPage+1 , $scope.loggedInUser.username).then(function(response){
                $scope.currentPage++;
    
                for(var i = 0; i<response.length; i++){
                    $scope.activities.push(response[i]);
                }
            });
        }
    

    每一次 $scope.activities

    1 回复  |  直到 8 年前
        1
  •  1
  •   Alex K    8 年前

    <a href="#"> 将您带到页面顶部。为了防止链接中出现这种行为,您可以执行以下任一操作

    • href="#!" (其中 ! 不是一个 id
    • href="javascript:void(0)"
    • 省略 href 属性,并使用CSS设置链接样式

    看见 this question