簡體   English   中英

當網頁上顯示的數據更改時,如何使網頁不滾動到頂部

[英]How to make webpage not scroll to the top when data displayed on it changes

對不起,標題怪異,但找不到更好的解釋。

我有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>

JS

    $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 ,該更改都會自動顯示在視圖上,但也會將頁面滾動到頂部。

<a href="#">將您帶到頁面頂部。 為了防止您的鏈接出現這種情況,您可以執行以下任一操作

  • 使用href="#!" (其中!不是頁面上任何元素的id
  • 使用href="javascript:void(0)"
  • 完全省略href屬性,並使用CSS設置鏈接的樣式

有關更多信息,請參見此問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM