Building a Parallax Header Directive
Add depth to your content with this cool header effect
PROModule Outline
- Source Code & Resources PRO
- Lesson 1: Introduction PUBLIC
- Lesson 2: The Role of Components and Directives PUBLIC
- Lesson 3: When to Use Custom Components & Directives PRO
- Lesson 4: Building a Simple Component PRO
- Lesson 5: Building a Simple Directive PRO
- Lesson 6: Setting up Components and Directives PRO
- Lesson 7: Using @Input and @Output PRO
- Lesson 8: Using ElementRef and Renderer PRO
- Lesson 9: Understanding Content Projection PRO
- Lesson 10: Understanding @ViewChild and @ContentChild PRO
- Lesson 11: Building a Complex Component PRO
- Lesson 12: Listening for Events and Binding to Properties PRO
- Lesson 13: Building a Skeleton Card Component PRO
- Lesson 14: Creating an Autogrow Directive for a Textarea PRO
- Lesson 15: Handling Error Messages with a Custom Component PRO
- Lesson 16: Building a Parallax Header Directive PRO
- Lesson 17: High Performance Accordion Component PRO
- Lesson 18: Conclusion PRO
Lesson Outline
Building a Parallax Header Directive
We're going to build another directive now, and this one will allow us to add an image to the top of our content area that will have a parallax effect added to it. As the content area scrolls, the header image will have a transformation applied that shrinks it, which gives a feeling of depth to the screen. The image appears to be in the background and moving slower than the content being scrolled, which is like the effect of looking out of your car window and the trees on the side of the road appear to move faster than the hills off in the distance.
This is a popular effect, and I've rebuilt this several times in the past. I've rebuilt it once more for this module to make it much easier to use in an application. I think this is an interesting example because we will be applying a directive to the <ion-content>
area like this:
<ion-content
[scrollEvents]="true"
appParallaxHeader
parallaxImagePath="/assets/autumn.jpeg"
[parallaxHeight]="300"
>
<div class="main-content">Normal content goes here</div>
</ion-content>
This will:
- Insert an element above the content area
- Listen for scroll events and transform the image accordingly
What I think is interesting about this example is that if you were to think about building this functionality, it would probably seem more obvious to build it as a component since we are adding a completely new element to the page. You could indeed build this functionality into a component if you wish, but I think this highlights the flexibility of directives.
Let's get into building it.
Generate the directive with the following command:
ionic g directive directives/ParallaxHeader
Thanks for checking out the preview of this lesson!
You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).