最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ionic Scroll To Specific List Item - Stack Overflow

programmeradmin1浏览0评论

Is there a way to scroll to a specific item in a ion-list?

For example in this codepen:

When I Go to test button, I want to scroll to the list item with the text "Text".

<button ng-click="goTo()">Go to test</button>

<ion-list class="item">Test</ion-list>

I didn't find any examples so goTo is just blank:

$scope.goTo = function(){

}

Is there a way to scroll to a specific item in a ion-list?

For example in this codepen: https://codepen.io/anon/pen/grEBQJ

When I Go to test button, I want to scroll to the list item with the text "Text".

<button ng-click="goTo()">Go to test</button>

<ion-list class="item">Test</ion-list>

I didn't find any examples so goTo is just blank:

$scope.goTo = function(){

}
Share Improve this question asked May 12, 2016 at 2:03 Sumama WaheedSumama Waheed 3,6093 gold badges21 silver badges33 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

You have to set an id to list element like :

<ion-item id="item{{item.id}}" ng-repeat="item in items">
      Item {{ item.id }}
</ion-item>

And then, $scope.goTo() method must modify location hash and call anchorScroll() method from $ionicScrollDelegate service :

$scope.goTo = function(id){
     $location.hash('item'+id);
     $ionicScrollDelegate.anchorScroll();
}

Check $ionicScrollDelegate documentation for more information.

Update with your codepen : https://codepen.io/anon/pen/RadXqL

I implemented this functionality with JavaScript. After nesting each ion-item in a div element, passed the div element id to the JS function below.

scrollTo(element:string) {

      document.getElementById(element).scrollIntoView();

  }
发布评论

评论列表(0)

  1. 暂无评论