1

Trying to manage for angular to scroll automatically to a specific div element when this element becomes visible.

Already searching and trying for hours (no joke) with no success.

So far tried a couple of modules

  • 'angular-scroll'
  • 'angular-ui-scroll'
  • some others i already forgot

And coudn't get one of them to work (or only on ng-click).

How far did I get?

For this question to answer; I found an example witch basically scrolls but is not what I'am trying to get tho.

e.g.

<button ng-click="scrollToHash()">Scroll</button>
..
..
<div id="#div">
  ..
</div>

-

function scrollController ($scope, User, $location, $anchorScroll) {
  $scope.scrollToHash = function () {
    $scope.hash('div');
    $anchorScroll();
  };
};

This way I can't gat any 'animation duration' on it. What I know what should work is to set a $watch on the element and call the function if the element is shown.

Not getting anywhere so I ask you guys for help.

Still new on Angular so please don't blame this newbie!

Thanks

2 Answers 2

1

I created a service to get my scroll going. You can try this.

Controller:

app.controller("scrollController", function ($scope, scrollToTopService) {
"use strict";

    $scope.scrollToHash= function () {
       scrollToTopService.scrollToTop();
    };
});

Service:

app.service('scrollToTopService', function ($location, $anchorScroll) {
"use strict";    
    this.scrollToTop =  function () {
       $anchorScroll.yOffset = 80; //I want it top drop 80px from id. You can remove this.
       $location.hash("IdWhereIWantToScroll");
       $anchorScroll();
    };
});

And here is your view:

<button ng-click="scrollToHash()">Scroll</button>
..
..
<div id="IdWhereIWantToScroll">
..
</div>

Hope this helps. Good luck.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to show/hide the elements using nh-show or ng-hide and then put a watch on the property used to show/hide the element. About scrolling, you already have a solution for that.

3 Comments

for a reason I don't understand, ng-show/ng-hide lags at me (on height: auto). Therefor I made a directory for slide-toggeling an element. So I guess your comment isn't a solution on that one?
What do you mean by 'ng-show/ng-hide lags at me (on height: auto)' ? Can you make a plunker ?
Eumh, not really, I dont have that code any more. The transformation duration was lagging on height: auto. And I needed height auto because I'am using this toggle on multiple contains (with different heights). I can make a plunker for the directive I made for that one tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.