Ionic Scroll To Specific List Item

萝らか妹 提交于 2020-07-18 16:14:49

问题


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(){

}

回答1:


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




回答2:


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();

  }


来源:https://stackoverflow.com/questions/37176044/ionic-scroll-to-specific-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!