iominh / ng-scrollbars

Angular wrapper of Malihu's jQuery Custom Scrollbar
http://iominh.github.io/ng-scrollbars/
MIT License
202 stars 62 forks source link

Add scrollTo parameter #48

Closed natete closed 7 years ago

natete commented 7 years ago

Hi @iominh I added a new parameter to your directive

ngScrollTo

With this parameter the user can set a point (defined as a string, number, array or function, check malihu's documentation here) to scroll to and the directive will handle it using

mCustomScrollbar("scrollTo",position);

I find it an interesting feature and I needed in my project. Maybe there's another way to do it but this is the one I came up with.

Thanks! :)

iominh commented 7 years ago

Thanks for the PR! With the current version 0.0.11, there's an "ng-scrollbars-update" https://github.com/iominh/ng-scrollbars/tree/0.0.11#updating-scrollbars, http://iominh.github.io/ng-scrollbars/18_update_scrollbars.html that would be somewhat similar to what you're trying to achieve I think? So, it would be something like the following:

app.controller('mainCtrl', function ($scope, $timeout) {
    $timeout(function() {
        $scope.updateScrollbar('scrollTo', 10);
    });
});
<div class="container" ng-scrollbars ng-scrollbars-update=updateScrollbar ng-controller="mainCtrl">
    <h1>Title</h1>
    <p>A bunch of content</p>
</div>

With this it also exposes the other functions like "destroy", "delete", "stop", "disable".

However, I like where you're going since your approach would require less code. But instead of "scrollTo" do you think it'll make sense to call it "initialScrollTo" to separate it from the ng-scrollbars-update runtime functionality since this scrollTo only occurs at the initial render?

Also thanks for your https://github.com/iominh/ng-scrollbars/issues/47 issue! I think we'll likely bump this PR to 0.0.12 if things sound good to you

natete commented 7 years ago

Thank you for the quick response!!

Actually ng-scrollbars-update works for me. Sorry I didn't get it at first sight. My implementation works both for initial scroll and to scroll wherever you want at any given time (not only at the initial render). You can keep it as a shorthand for scrollTo or just close the PR.

iominh commented 7 years ago

No worries and thanks for your contribution! Ah, I didn't know that you could pass a function into your scrollTo - that's very nice!

I'll see if I can add your change in an upcoming release (might change it to 0.1.0) but will also change the existing ng-scrollbars-update functionality as I've found it odd having to do a timeout before being able to invoke the function. I think having the scope.$watch may fix that.