Search code examples
javascriptjqueryangularjscssonsen-ui

Make onsen-ui sliding menu to scroll to top


I am using onsen ui to build an app and have got a very long menu:

    <ons-template id="menu.html">
        <ons-page modifier="menu-page">
            <ons-toolbar modifier="transparent"></ons-toolbar>
            <ons-list class="menu-list">
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('updates.html', {closeMenu: true})">
                    <ons-icon icon="fa-updates"></ons-icon>Updates</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('about.html', {closeMenu: true})">
                    <ons-icon icon="fa-about"></ons-icon>About</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('venue.html', {closeMenu: true})">
                    <ons-icon icon="fa-venue"></ons-icon>Venue</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('agenda.html', {closeMenu: true})">
                    <ons-icon icon="fa-agenda"></ons-icon>Agenda</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('speakers.html', {closeMenu: true})">
                    <ons-icon icon="fa-speakers"></ons-icon>Speakers</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('sponsors.html', {closeMenu: true})">
                    <ons-icon icon="fa-sponsors"></ons-icon>Sponsors</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('resources.html', {closeMenu: true})">
                    <ons-icon icon="fa-resources"></ons-icon>Resources</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('feedback.html', {closeMenu: true})">
                    <ons-icon icon="fa-feedback"></ons-icon>Feedback</ons-list-item>
                <ons-list-item class="menu-item" ng-click="menu.setMainPage('register.html', {closeMenu: true})">
                    <ons-icon icon="fa-register"></ons-icon>Register</ons-list-item>
            </ons-list>
        </ons-page>
    </ons-template>

When I am in a small screen and scroll down the menu it stays in the same position and doesn't go back to the initial view.

Code for the menu:

<ons-sliding-menu menu-page="menu.html" main-page="updates.html" side="left" var="menu" type="reveal" max-slide-distance="200px" swipeable="true" ons-postclose="menutotop()"></ons-sliding-menu>

I implemented ons-postclose="menutotop()" but it doesn't seem working with jQuery.

Does anybody know how to make it scroll back to the top as soon as it closed by default?


Solution

  • First of all, create an Angular controller and add it into a div element, which will wrap the ons-sliding-menu element. If you want to use jQuery, your code will be like this (see also the working CodePen Example).

    HTML

    <div ng-controller="MyController"><ons-sliding-menu  menu-page="menu.html" main-page="updates.html" side="left" var="menu" type="reveal" max-slide-distance="200px" swipeable="true" ons-postclose="menutotop()"></ons-sliding-menu></div>

    JS

    ons.bootstrap()
      .controller('MyController', function($scope) {
        $scope.menutotop = function() {
          $('.page__content').animate({
              scrollTop: $('.ons-list-inner').height()
            })
            .animate({
              scrollTop: 0
            });
        }
      });

    I hope it helps!