1

I have a view where I need to implement a sticky menu inside of the Ionic framework.

Looking for an Angular not jQuery solution I found ngSticky. Great library works perfect, the problem however is that it won't work inside of the Ionic directive <ion-content>.

I asked a previous question about it here, and then figured out what was going on:


Because the parent container that ion-content creates has the following style on it:

<div class="scroll" style="transform: translate3d(0px, 0px, 0px) scale(1);">

It's a fix for an iPad scrolling bug as described here.

So I can't remove that style and removing it would involve making changes to the core ionic.bundle.js file anyways.

The only thing I can think of now at the moment, is to write some code to detect when the sticky header hits the top, then remove that block of HTML out of ion-content and replace it above the ion-content container.

Is there any other way around this?

Here is a Plunker http://plnkr.co/edit/DiVfOzjJevSFOtVitYty?p=preview There is a jQuery hack in place that removes the style tag so you can see how it should work.

Community
  • 1
  • 1
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

2 Answers2

1

I wanted to create something similar.

I had an image on top and a button below. As I'm scrolling down the page I wanted the button to stay on top.

(sticky menu === sticky button === all the same)

Code below isn't working reliably and is full of hacks so I'm hesitant to share it... In fact I've actually removed it from the codebase, posting here for illustrational purposes only:

  // INITIALISATION SETUP 
  var $button = $(".button-middle-wrapper");
  var buttonOffset;

  var _measureOffset = function() {
    buttonOffset = $button.offset().top;
  };

  setTimeout(_measureOffset, 500); // HACK: during initialisation image is not loaded - therefore image offset is not correct - wait for image to load?

  $(window).resize(_measureOffset);

  // METHOD AVAILABLE ON THE SCOPE
  $scope.fixButtonToTop = function() {
    var scrollPosition = $ionicScrollDelegate.getScrollPosition().top;

    if (scrollPosition > buttonOffset - 30) {
      $button.css({"top": scrollPosition + 30}); // normally it is enough to set position to "fixed" - in Ionic case we do not scroll browser window - we do tranlations and setting positions - so we manually calculate position
    } else {
      $button.css({"top": ""});
    }
  };

And then in the view: <ion-content on-scroll="fixButtonToTop">


Not only the code is terrible but I've also committed one of the common mistakes described here: https://www.toptal.com/ionic/most-common-ionic-development-mistakes

Common Mistake #8: Binding Events to onscroll, and Forgetting About requestAnimationFrame

Mars Robertson
  • 12,673
  • 11
  • 68
  • 89
0

I know its not a good solution, but I'd simply go for applying CSS rule with !important to override the current style

.pane {
   transform: none !important;
   -webkit-transform: none; /* Safari and Chrome */
   -moz-transform: none; /* Firefox */
   -ms-transform: none; /* IE 9 */
   -o-transform: none; /* Opera */
}

Plunkr Here

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Thanks, but yeah that won't be ideal... also inside my app, when I do that.. Ionic loses the scroll ability, which Plunkr does not lose. However I think I found the perfect solution here! http://market.ionic.io/plugins/ionic-scroll-sista – Leon Gaban Aug 27 '15 at 19:40
  • 1
    @LeonGaban That's cool.. you should go for it then..Cheers.. :) – Pankaj Parkar Aug 27 '15 at 19:42
  • This is still super frustrating, cannot get this library to work at all... may just have to resort to some unfortunate jQuery magic. – Leon Gaban Aug 27 '15 at 20:17
  • @LeonGaban don't think so..Wondering what logic will be there to do magic? – Pankaj Parkar Aug 27 '15 at 20:32
  • I literally would have to rip out my `
    ` and place it out of the `` container... then change some css, I think I can get this done with just vanilla javascript though. Just frustrated that I tried 2 plugins specifically built for this exact problem, and both failed :(
    – Leon Gaban Aug 27 '15 at 20:36
  • Would you mind a look at this Plunkr? I recreated it with the correct use of `ion-content` as in my current app. The problem continues here: http://plnkr.co/edit/u7T5YES7NLKkLoqAPzm7?p=preview – Leon Gaban Aug 27 '15 at 22:08