Within an <ion-scroll>
, this directive creates sticky item headers that get bumped out of the way by the next item.
Demo: http://cl.ly/2u2X390s0H1a
Requirements:
- Needs UnderscoreJS for its
_.throttle
utility. - Must be used within an
<ion-scroll>
or<ion-content>
- You must group each header and contents together within a container element (this container element defines the area in which the header should stay).
- Not tested with
collection-repeat
-- only withng-repeat
(please let me know if it works and I'll update) - This directive works by cloning the "sticky header" and appending it between the outer scroll container and inner scroll container -- as a sibling of the scrollbar, for reference. Thus, you might need to edit your CSS if it doesn't already apply correctly to the cloned header element.
Example: If you want to render a list of posts with sticky post titles, you could create markup like this:
<article class="post" ng-repeat="post in posts track by post.id">
<h1>Post Title</h1>
<main>
<p>Long post body with many paragraphs...</p>
</main>
</article>
Then all we need to do is make sure it's within an <ion-scroll>
, and add the affix-within-container=".post"
attribute to the <article>
. (See affixWithinContainer.html
below)
Where affix-within-container=".post"
tells the <h1>
that it is the sticky header for everything within it's closest .post
ancestor. i.e. $(element).closest('.post')
.
This is extremely helpful and it is the only solution I have found working after trying iOSList, StickySectionHeader etc., most would not work well with mobile interface, or hard to integrate with Angular. Your solution is also simple enough that it does not even depend on any external CSS.
I have noticed that when I used the directive in ion-nav-view, somehow
position: fixed
does not work, but by changing it toposition: absolute
, it works like a charm.