javascript - Angular Scroll to specific item in the list created using ng-repeat - Stack Overflow

In my AngularJS app I have two lists created using ng-repeat. List one lists all the food categories

In my AngularJS app I have two lists created using ng-repeat.

List one lists all the food categories like:

Meat, Fruits, Vegetables, Poultry, Dry Fruits ... etc.  

List two lists all the foods like:

Eggs, Chicken, Oranges, Apple, Banana ... etc.  

List number two lists food items in groups like, all the Meat items first and then all Fruits.

I have created both of these lists using div and ng-repeat, in one page.
Each element in the both of the lists has unique ID.

What I want to achieve:

When user select one of the food category from list one, then list two should scroll to that particular category related food items.

For example if user selects Fruits in list one then, list two should scroll to Oranges item.

UPDATE

Also is it possible to select item in list one when user scrolls to specific group? For example if user scrolls to Chicken in list two then in list one Meat should be selected.

In my AngularJS app I have two lists created using ng-repeat.

List one lists all the food categories like:

Meat, Fruits, Vegetables, Poultry, Dry Fruits ... etc.  

List two lists all the foods like:

Eggs, Chicken, Oranges, Apple, Banana ... etc.  

List number two lists food items in groups like, all the Meat items first and then all Fruits.

I have created both of these lists using div and ng-repeat, in one page.
Each element in the both of the lists has unique ID.

What I want to achieve:

When user select one of the food category from list one, then list two should scroll to that particular category related food items.

For example if user selects Fruits in list one then, list two should scroll to Oranges item.

UPDATE

Also is it possible to select item in list one when user scrolls to specific group? For example if user scrolls to Chicken in list two then in list one Meat should be selected.

Share edited Nov 20, 2017 at 7:43 gyc 4,3606 gold badges36 silver badges55 bronze badges asked Jun 16, 2016 at 5:51 User7723337User7723337 12k30 gold badges114 silver badges202 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I personnally would use Angular's $anchorScroll

    this.scrollTo = function(id) {
        $anchorScroll(id);
    }

Fiddle

What about something like this for a list1 item:

<div ng-repeat="itemType in itemTypes">
  <li ng-click="$scope.scrollThere({{"."+itemType}})">{{itemType}}</li>
</div>

With something like this for list2:

<div id="list2" style="overflow-y: scroll; height: 30px;" ng-repeat="item2 in theItems">
  <li class="{{item2.itemType}}">{{item2.itemName}}</li>
</div>

And then keep track of which element to scroll to somehow like this just goes to the next item in the fruits when fruit is clicked?:

$scope.scrollThere = scrollThere;
$scope.count = { fruit: 0 };
function scrollThere(foodType){
    angular.element('#list2').animate({
      scrollTop: angular.element(foodType)[count[foodType]++].offset().top
    }, 1000);
}

And making sure the y overflow is set to scroll in the css.

Or instead of keeping count with an object you could embed the index logic into an id using "#" + {{itemType}} + $index as the selector on the list1 item.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851150a4597133.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信