Animate nav on scroll - CSS-only & easy to customize

Kevin Powell
2 Apr 202426:52

Summary

TLDRThe video script discusses the process of creating a navigation bar that changes its appearance based on the scroll position of the page. It introduces the concept of using CSS custom properties and the 'animation-timeline' feature to achieve a sticky navigation bar with color shifts and a modern glass morphism style. The tutorial covers the technical aspects of setting up the navigation, making it responsive to scroll events, and customizing it with animations that feel smooth and seamless. The script emphasizes the importance of browser support and provides solutions for different scenarios, ensuring a positive user experience across various platforms.

Takeaways

  • 🌟 The video discusses creating a navigation bar that changes its appearance based on scroll position, without using JavaScript.
  • 🎨 Customizing the navigation involves using CSS custom properties to easily adjust the color and other stylistic changes.
  • πŸš€ A 'sticky nav' effect is achieved by using the 'position: sticky' property along with a 'z-index' and 'background-color'.
  • πŸ’‘ The concept of 'at-supports' is introduced to provide a progressive enhancement approach for animations, ensuring a baseline experience in unsupported browsers.
  • πŸ”„ Keyframes are utilized to animate the navigation bar, with the 'animation' and 'animation-timeline' properties controlling the timing and sequence of the animations.
  • 🎨 The 'backdrop-filter' property is used to create a glass morphism effect, adding a blurred background to the navigation bar.
  • πŸ“ The 'width' property is manipulated to change the navigation bar's width, using 'fit-content' to dynamically adjust to its content.
  • πŸ”§ Custom properties are used to control the visibility of the logo and the navigation bar, toggling between 'block' and 'none'.
  • πŸ› οΈ The video provides a method for handling non-animatable properties by changing them at the start of the animation using keyframes.
  • πŸ”„ The 'animation-name', 'animation-duration', 'animation-timing-function', and 'animation-fill-mode' properties are key in defining the animation behavior.
  • πŸ“ˆ The video includes a link to a previous video for a deeper dive into CSS animations, scroll behavior, and browser support considerations.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to teach viewers how to create a customizable navigation bar that changes its appearance, such as color and styling, based on the scroll position of the page.

  • What design trend is mentioned in the video that the navigation bar can mimic?

    -The design trend mentioned in the video is the glass morphism look, which is popular for its translucent and smooth visual effects.

  • How is the sticky navigation effect achieved without JavaScript?

    -The sticky navigation effect is achieved using CSS properties like 'position: sticky', 'z-index', and 'background-color', along with custom properties and animations.

  • What is the purpose of using an SVG for the logo in this context?

    -Using an SVG for the logo makes it easier to shift colors during the scroll-based animation, as the 'fill' attribute of an SVG can be manipulated with CSS.

  • What is the role of 'animation' and 'animation-timeline' in the script?

    -The 'animation' property is used to define the name of the keyframes animation, while 'animation-timeline' is used to control the timing and progression of the animation based on scroll position.

  • How does the video script address browser compatibility issues?

    -The script suggests using the '@supports' rule to check for browser support of certain CSS properties and features. If a browser doesn't support a feature, it provides alternatives or fallbacks to ensure a working navigation bar without advanced effects.

  • What is the significance of using custom properties in the script?

    -Custom properties, denoted with '--', are used to make the code more customizable and easier to maintain. They allow for changes in the animation and styling to be controlled from a central place, making adjustments simpler.

  • What is the purpose of changing the 'display' property of the logo within the animation?

    -Changing the 'display' property of the logo within the animation allows it to disappear at a certain scroll position, creating a seamless and smooth transition to the new navigation style without the logo.

  • How does the script handle the width of the navigation bar during the animation?

    -The script uses the 'width' property and the 'fit-content' value to make the navigation bar adapt to its content's width without causing line wraps. It also uses custom properties to control the width changes during the animation.

  • What is the role of 'backdrop-filter' in achieving the glass morphism look?

    -The 'backdrop-filter' property with a 'blur' effect is used to create the glass morphism look, giving the navigation bar a blurry, translucent appearance that matches the design trend.

  • What are the final recommendations given in the video for further exploration?

    -The video encourages viewers to experiment with the 'animation-timeline' and 'scroll-timeline' features in CSS for creating advanced scroll-based animations and effects without JavaScript, and points to additional resources for a deeper dive into these topics.

Outlines

00:00

🎨 Introducing Customizable Navigation Styling

The paragraph introduces the concept of creating a navigation bar that changes its appearance based on the user's scroll position. It discusses the idea of making the navigation customizable and mentions the popular glass morphism look. The speaker sets the stage for a tutorial on how to achieve this effect without using JavaScript, starting with a simpler version and gradually making it more customizable using CSS custom properties.

05:01

πŸš€ Setting Up the Basic Sticky Navigation

This section delves into the initial setup for a basic sticky navigation bar. The speaker explains the process of making the navigation stick to the top of the page as the user scrolls. It covers the use of the 'position: sticky' property, setting the 'z-index', and 'background-color'. The aim is to create a navigation that remains on top and transitions smoothly from one style to another as the user scrolls down the page.

10:02

🌈 Adding Color Transitions with Scroll Animation

The speaker describes how to add color transitions to the navigation bar as the user scrolls. The focus is on using keyframes and the 'animation' property in CSS to create a smooth color shift effect. The paragraph details the process of defining the end state of the animation with a box shadow and then using the 'animation' property to apply this style as the user scrolls down the page. The speaker also discusses the use of 'animation-timeline' and 'scroll' to control the timing and position of the color transition.

15:03

🎨 Customizing the Animation with CSS Variables

In this part, the speaker talks about enhancing the customization of the navigation bar's color transition using CSS custom properties. The goal is to make it easy for users to modify the animation's start and end points, as well as the color scheme. The paragraph explains how to use 'calc()' with viewport height units ('VH') and custom properties to control the scroll position where the color transition begins and ends. It also touches on browser support and provides solutions for non-supportive browsers.

20:06

πŸšͺ Implementing the Glass Morphism Effect

The paragraph discusses the implementation of the glass morphism effect for the navigation bar. This involves making the navigation bar appear as if it's sliding in and out of view with a blurred backdrop. The speaker explains how to achieve this by manipulating the 'backdrop-filter', 'opacity', and 'width' properties. The focus is on creating a modern, aesthetically pleasing effect that enhances the user interface.

25:07

πŸ”§ Final Touches and Browser Compatibility

The speaker concludes the tutorial by discussing final adjustments to the navigation bar's appearance and functionality. This includes hiding the logo and adjusting the width of the navigation bar for a better fit. The paragraph also addresses browser compatibility issues, suggesting the use of '@supports' to ensure a fallback experience for browsers that do not support certain CSS features. The speaker encourages viewers to experiment with the code and make the tutorial their own, while providing a link to a deeper dive into CSS animations and scroll-based interactions.

Mindmap

Keywords

πŸ’‘Navigation

In the context of the video, 'navigation' refers to the menu or set of links that allow users to move through different pages or sections within a website. The video discusses creating a navigation bar that changes its appearance based on the user's scroll position, enhancing user experience by providing a dynamic and interactive interface.

πŸ’‘Customization

Customization in this video script means altering the default settings or properties of the navigation bar to make it unique and tailored to specific design preferences. The video provides insights on how to make the navigation bar's behavior, such as color changes and glass morphism effects, customizable without the need for JavaScript.

πŸ’‘Sticky Navigation

A 'sticky navigation' refers to a navigation bar that remains visible at the top of the viewport as the user scrolls down the page. It provides a consistent way for users to access the website's sections, improving the overall user experience. The script explains how to create a sticky navigation bar using CSS without JavaScript.

πŸ’‘CSS Custom Properties

CSS Custom Properties, also known as CSS variables, are a feature that allows developers to define reusable values within a style sheet. These properties can be used throughout the document, making it easier to maintain consistency and make changes to the design. In the video, custom properties are used to control various aspects of the navigation bar, such as colors and animation timings.

πŸ’‘Glass Morphism

Glass morphism is a modern design trend characterized by translucent surfaces with blurred backgrounds, creating a visually appealing effect that resembles glass. In the video, the creator discusses implementing a glass morphism look for the navigation bar, making it slide in and out with a blurred backdrop filter for a stylish and contemporary appearance.

πŸ’‘Animation Timeline

An animation timeline in CSS is a way to control the sequence and timing of animations applied to an element. It allows developers to define how an animation starts, progresses, and ends over a specified duration or range of values. The video script explains using an animation timeline to control the navigation bar's color change and other visual effects based on scroll position.

πŸ’‘Scroll-linked Animation

A scroll-linked animation is an interactive animation that is triggered or modified based on the user's scroll position within a webpage. This type of animation enhances user engagement by creating a dynamic and immersive browsing experience. The video demonstrates how to create a scroll-linked animation for a navigation bar, making it change colors and styles as the user scrolls.

πŸ’‘Viewport Units

Viewport units are a type of length unit in CSS that is based on the size of the browser's viewport. One viewport unit (vh) is equal to 1% of the viewport's height. This unit is used to create responsive designs that adjust to the size of the user's screen. In the video, viewport units are used to set the start and end points for the scroll-linked animation of the navigation bar.

πŸ’‘SVG Logo

An SVG (Scalable Vector Graphics) logo is a vector-based graphic used as a logo on a website. SVGs are resolution-independent, meaning they can be scaled to any size without losing quality. In the video, the creator uses an SVG logo for the navigation bar and explains how its fill color can be easily changed with CSS, contributing to the dynamic appearance of the navigation.

πŸ’‘Performance

In web development, performance refers to how quickly and efficiently a website loads and interacts with the user. The video emphasizes the importance of performance when creating animations, noting that certain properties like box shadows can impact performance but are used here due to their link to scroll position.

πŸ’‘Browser Support

Browser support refers to the compatibility of web technologies across different web browsers. It is crucial for ensuring that all users can access and interact with a website as intended. The video discusses the current browser support for certain CSS features used in the navigation bar's design and suggests ways to ensure a good user experience across different browsers.

Highlights

Creating a navigation bar that changes color and style based on scroll position.

Introducing a glass morphism look to the navigation bar for a trendy design.

Achieving the effects without JavaScript, using only CSS.

Customizing the navigation bar using CSS custom properties for easy modification.

Utilizing the 'position: sticky' property in CSS for the navigation bar.

Creating a smooth color transition effect with CSS keyframes and animations.

Adjusting the box shadow to create a sliding effect as the user scrolls.

Using the 'animation-timeline' and 'scroll' properties to control the animation based on scroll position.

Incorporating SVG logos for easier color manipulation within the navigation bar.

Making the navigation bar width dynamic and responsive with 'fit-content'.

Hiding the logo and adjusting the navigation bar width at the start of the scroll with custom properties.

Applying a 'backdrop-filter' for the glass morphism effect on the navigation bar.

Ensuring browser compatibility with custom properties and providing fallbacks for non-supporting browsers.

Creating a toggle for the navigation bar effects for user control.

Using 'animation-name', 'animation-duration', and 'animation-timing-function' to customize the animation behavior.

Adjusting the 'top' property of the navigation bar to create a sliding effect as it sticks to the top of the viewport.

Transforming the navigation bar into a dark theme with opacity adjustments for better visual effects.

Transcripts

play00:00

hello my FR and friends if you've ever

play00:02

wanted to make a navigation like this

play00:03

one that changes depending on where you

play00:05

are so you go past a certain point it

play00:07

switches over to a different color or

play00:09

different styling and take that idea and

play00:11

maybe tweak it and make it customizable

play00:13

so you could also do something like this

play00:14

where it sort of slides in with this

play00:16

glass morphism look that's very popular

play00:18

these days and then slides back out as

play00:20

you get to the top with only one

play00:21

navigation we don't need multiple ones

play00:22

in our HTML and we don't even need any

play00:25

JavaScript for this well if that sounds

play00:27

interesting you want to know how we can

play00:28

do all of this you're in the right place

play00:30

we're going to start by building out

play00:31

this version cuz it's a little bit

play00:33

simpler going to take that code step it

play00:35

up make it very customizable using

play00:36

custom properties and turn it into

play00:38

something that allows us to do this

play00:41

version here instead so if all of that

play00:43

sounds good you're definitely in the

play00:44

right place but just before we dive into

play00:45

it I do want to let you know that I have

play00:47

new t-shirts there's this one that I'm

play00:49

wearing right now as well as one for

play00:51

grid and another one for color space so

play00:53

you can show off your love for CSS and

play00:55

let everybody know that you're also a

play00:57

front and friend if you are interested

play00:59

there should be a little card thingy on

play01:00

the video right here and there'll also

play01:01

be underneath the video as well but what

play01:04

you're really here for is learning how

play01:05

we can do the sticky navigation and so

play01:07

let's dive right into it and take a look

play01:09

very quickly at the code that uh we're

play01:11

going to be starting with here very

play01:13

simple I have a header here which is

play01:14

where most of our styling is going to go

play01:16

it's going to us the navigation that we

play01:18

can see uh right up there uh I have an

play01:20

SVG as my logo you don't need to have an

play01:23

SVG as your logo but if you do want it

play01:25

to shift colors like we're going to be

play01:26

doing it makes it a lot easier if it is

play01:29

one if not you could probably do it with

play01:30

a blending mode or something um but

play01:32

using an SVG does make it easier and

play01:34

we'll see how that works when we get to

play01:36

it and then I just have my nav here with

play01:38

a ul and all of that and obviously I

play01:41

also have some styling on here nothing

play01:42

too fancy I'm not going to Deep dive it

play01:44

if you want to take a look at my styling

play01:46

there is a link to the finished code in

play01:48

the description you will notice also

play01:50

that I'm using nesting here so I just

play01:51

have a few things that are nested if

play01:53

you're not used to nesting this is just

play01:55

a UL that is inside of my header it is

play01:57

modern CSS which is what we're going to

play01:59

be playing with to today which is why I

play02:00

felt comfortable doing it for this demo

play02:02

and right now with everything I have set

play02:04

up all this is doing is giving us a

play02:06

regular navigation that's just at the

play02:07

top of the page we want it to stick

play02:10

first of all so the first thing I'm

play02:11

going to do is I'm going to come here

play02:12

and I'm just going to paste this in

play02:13

where we're getting a position of sticky

play02:15

with a zed index of a th000 a background

play02:17

color on it uh and a top of zero so you

play02:20

can see that has all come in and now

play02:22

because we have the position sticky

play02:24

there it's working the Zed index here is

play02:26

just because if anything else has a

play02:28

position on it other than the static um

play02:31

it's the sticky stuff often ends up

play02:32

behind it so this just makes sure it's

play02:34

on my top layer I've put a big number

play02:36

and the background color I need to have

play02:38

CU it's going to be transparent if not

play02:39

which means it's kind of awkward so we

play02:41

can put that back on there now this

play02:44

first one we're going to be doing is

play02:45

when we scroll down it's going to shift

play02:47

and change colors so to be able to do

play02:49

that what we're going to do is I'm going

play02:50

to scroll all the way down to be outside

play02:52

of my selector we can't be inside of

play02:54

another selector for this to work um

play02:56

because we're using at Key frames which

play02:58

have to be on their own they can't be NE

play02:59

tested uh if you're using regular CSS if

play03:03

you're using a pre-processor it is not a

play03:05

problem uh and the key frames here I'm

play03:06

going to call it sticky nav just like

play03:08

that uh the real trick here is we just

play03:11

want to put in these styles that we're

play03:13

going to have at the end of the

play03:15

animation and so for now the only thing

play03:17

I'm going to do is put this box Shadow

play03:19

on there we're going to tweak this and

play03:20

change it a little bit more but just to

play03:21

show you uh as a quick example of how

play03:23

this works and normally box Shadows

play03:26

aren't something we want to animate

play03:27

they're not the best for performance but

play03:29

because it's link to scroll position I'm

play03:31

not too worried about the frames per

play03:32

second or anything on this um and then

play03:34

we're going to come back up on the

play03:35

original header up here and we're going

play03:37

to come we're going to say animation and

play03:39

on here we're going to do our sticky nav

play03:41

which is the name of our animation we're

play03:42

going to say that it is linear because

play03:44

we don't want we want the timing

play03:46

function just to be like from here to

play03:47

here it's based on scroll position and

play03:49

we're also then going to say that we

play03:51

want it to be forwards because when the

play03:52

animation is finished we want it to stay

play03:54

at the end and if I just do that because

play03:56

we don't have any timing on here it's

play03:58

actually already applied which is you

play04:00

can see there there's a box Shadow

play04:02

that's on there um and you know what I

play04:03

was just thinking this isn't going to be

play04:05

super obvious with just the shadow

play04:07

because we don't really see it here so

play04:08

why don't we actually invert things here

play04:10

a little bit I'm going to move um we

play04:12

have my color here I'm going to make

play04:13

this my 100 uh 900 I should say to make

play04:17

it white and then this is going to go to

play04:19

100 so we have a dark one there and what

play04:21

we can also do is invert those two uh at

play04:23

the end of our animation so if I bring

play04:26

those down here make that my one make

play04:28

that my nine let's hit save save so you

play04:30

can see it's actually we're we're at

play04:33

this point now we're at the 100% because

play04:35

the animation has just run and that's

play04:37

all it's happening if I wanted to I

play04:38

could even come here and say like 2

play04:40

seconds hit save and if I refresh the

play04:42

page it starts dark and you can see it

play04:44

switches over after 2 seconds but I

play04:46

don't want it to have a Time on the

play04:49

animation here instead I want to use

play04:51

animation timeline and I'm going to

play04:54

write in View and it it it's going to

play04:57

work now but it's not perfect but as I

play04:59

go down down here we can see if we look

play05:01

up at the top uh it's slowly shifting as

play05:03

we move all the way down to the bottom

play05:05

of our page which isn't what we want we

play05:08

want it to basically stay the same and

play05:11

then all of a sudden shift over a short

play05:13

period of time so to be able to do that

play05:16

what we're doing right now is we're

play05:17

doing it all in sort of the steps so you

play05:20

understand how it's working then we're

play05:21

going to make it a lot easier and less

play05:22

magic numbering um as we go through this

play05:24

what we're going to do is after my

play05:26

animation timeline we're going to come

play05:28

in with an animation range start and I'm

play05:31

going to duplicate that one and do an

play05:32

animation range end here and these just

play05:35

are for where does the animation start

play05:37

and end there are ways of passing values

play05:39

into here but they're kind of backwards

play05:40

and kind of weird uh if you want to know

play05:42

more about this I have as I said in the

play05:44

intro covered animation timeline and

play05:46

scroll stuff in a lot more detail in a

play05:48

previous video that really deep Dives

play05:49

all of this so I'll leave the link to

play05:51

that down below um and I just realize my

play05:54

color on this is terrible but it's okay

play05:56

well we'll get it looking better

play05:57

later this should stand out not be so

play06:00

dark I don't know what I did uh but

play06:01

that's okay um so animation view

play06:04

timeline uh is good so the start let's

play06:06

just do for now we're going to say that

play06:08

it's 100 VH for my viewport height and

play06:11

let's come here and say 120 viewport

play06:14

height um and what that means is you can

play06:16

see it's shifting in this short period

play06:18

of time right here because it's starting

play06:22

uh and the starts always an offset so it

play06:24

basically means once it's at um so like

play06:26

here if I made this 50 let's just say um

play06:29

you it's already like halfway done so I

play06:31

want it to be at 100 to be like we're

play06:34

we're starting from the top of the page

play06:35

basically um and then it's going to be

play06:38

finished here so we have like a 20%

play06:40

scroll range sort of thing um almost

play06:43

already it's not quite what I want

play06:45

though CU ideally I'd want it to start

play06:47

switching somewhere like around here

play06:49

once it's already like past this dark

play06:51

hero type area right so we'd stay dark

play06:54

stay dark stay dark then switch so this

play06:56

is where it can feel kind of magic

play06:58

numbery but I can just boost this number

play06:59

up more and then make this one even

play07:01

bigger uh and now until I've scrolled to

play07:06

about 50% of the way down my page it's

play07:08

going to stay that and as soon as I hit

play07:10

that 50% scroll Mark it's going to start

play07:12

switching and it switches over uh we're

play07:15

going from 150 to 180 so it takes us

play07:18

like 30% so the smaller the difference

play07:20

here if I do that as 160 just means that

play07:23

shift is going to happen much faster

play07:24

when it does make that shift right there

play07:27

there's a few important things that are

play07:28

happening here

play07:30

uh cuz I mentioned my logo is changing

play07:32

colors the reason my logo is changing

play07:34

color is because I have the fill of

play07:35

current color and because as I said I

play07:38

used an SVG here uh you can't use the

play07:41

SVG as an image here it has to be as an

play07:43

inline SVG if you do want to manipulate

play07:46

the fill this way so just so you know um

play07:48

that's an important thing to take into

play07:49

account depending on how you're setting

play07:51

things up if you have an image you could

play07:53

probably actually it depends on your

play07:55

colors and stuff so I don't want to say

play07:57

for sure but you might be able to use

play07:58

like a differ uh mode or something to

play08:00

create some interesting effect uh but

play08:02

ideally if you have an image that's a

play08:04

different color it'd be kind of weird if

play08:05

it's shifting anyway so just be careful

play08:07

with with logo colors but it's following

play08:10

my current color which is my text color

play08:12

and my links are also set to inherent so

play08:14

that's why I can change it on the parent

play08:15

and everything is Shifting along with it

play08:18

but otherwise it it's actually working

play08:20

pretty good um the thing that's kind of

play08:22

awkward with it when it does this shift

play08:24

right now uh and as I said we're gaining

play08:27

the Box Shadow is coming in the Box

play08:29

shadow I'm not concerned about because

play08:31

it is just based on scroll position um

play08:34

but the thing that's really awkward

play08:35

right now is that we have these sort of

play08:38

magic numbery setups here and I don't

play08:40

like that they're magic numbery and I

play08:42

want to make it easy for people to

play08:43

modify and to change and what about the

play08:45

colors and everything else that's going

play08:46

on I want all of that to be actually

play08:48

really easy to be able to handle so

play08:50

that's where custom properties come into

play08:51

the mix so what i' ideally have is

play08:55

animation starts after and we could say

play08:58

like 500 pixel so once we've scrolled

play09:00

down 500 pixels we're going to start our

play09:02

animation and then we're also going to

play09:04

come in with a custom property here

play09:05

called animation and I guess like

play09:08

distance like how long maybe we could if

play09:09

you have a better name for that let me

play09:11

know um but animation distance and we'll

play09:13

say 250 so we're going to scroll 500

play09:16

pixels down the page start my animation

play09:19

and the animation range of scrolling is

play09:21

250 pixels the other thing I'm going to

play09:23

do here and I'm breaking this off on its

play09:25

own little tangent because people always

play09:27

ask me about it is I'm going to make

play09:28

these have an underscore on them just to

play09:31

indicate these aren't things you find up

play09:33

in the like the root these are locally

play09:35

scoped custom properties so I'm sort of

play09:37

setting them apart a little bit um this

play09:39

is an idea that Leia veru had it's sort

play09:41

of like private properties in JavaScript

play09:43

there's a little bit more to it than

play09:44

what she was talking about um but I just

play09:46

like doing this as a way to delimit if

play09:48

something is a locally scoped property

play09:50

versus if something is you know I go and

play09:52

find it in my rout so that's how I'm

play09:54

setting it up here uh and just why

play09:56

you'll see me using underscores on them

play09:58

so how can we use those numbers let's

play09:59

come down here and on these I'm actually

play10:02

going to use a Cel so we can say Cel of

play10:05

100 V VH cuz viewport height and you

play10:07

could use dvh here if you wanted to as

play10:09

well Dynamic viewport height um I think

play10:11

VH is actually very safe to use in this

play10:13

case but if you wanted the dvh you could

play10:15

use it as well and with that we're going

play10:17

to say plus uh 100 VH plus my

play10:24

VAR and we're just going to copy that

play10:26

cuz it's faster and paste it in so our

play10:29

anim starts after and then I'm actually

play10:32

going to take this one and copy it again

play10:34

and paste it on this line let's make

play10:35

this a lot bigger so you can see what

play10:37

I'm doing uh so we're going to say this

play10:40

plus my VAR and this one is my animation

play10:44

distance so that's just you know we're

play10:47

going from this point to that point

play10:50

basically and this math will work and if

play10:51

I hit save on that now we come back to

play10:53

here as I go down we can see that it's

play10:56

hitting exactly what I asked it to so

play10:58

we're scrolling down that distance then

play11:00

it's going to do the shift and it's

play11:01

going to go to there uh I'm actually

play11:03

going to make one change here cuz I

play11:05

don't like how it looks um my after

play11:08

color my background here I'm going to

play11:09

make this Pure

play11:11

White um and I think that little that

play11:13

grayed out zone is going to look a

play11:14

little bit nicer uh and what I like

play11:17

about this now is I could say okay after

play11:19

I scroll 900 and I can use pixels here

play11:21

or I can use viewport units or I can use

play11:22

whatever I want and just for fun I would

play11:24

never do this but we could say 900 and

play11:26

900 so that means if I scroll down we

play11:28

have to go all the way way down and then

play11:29

it's a really slow shift or we could

play11:31

come in here and make this like 50

play11:33

pixels here so now instead of a really

play11:35

long transition it's going to be a

play11:37

really short transition when it does

play11:38

happen right and that's probably more of

play11:40

what you're after uh and again this

play11:42

could be set with viewport viewport

play11:44

height so after we scroll 50 viewport

play11:46

height which would be about 50% of the

play11:48

way down then all of a sudden we're

play11:50

going to get a nice fast transition to

play11:51

having the shadow whatever you want we

play11:53

can play around with those numbers and

play11:54

we can actually make a lot more of this

play11:56

customizable as well if you want to you

play11:58

don't have to to add these all in but I

play12:00

sort of like having it like this uh

play12:02

where I'm also saying my Surface color

play12:04

start surface ends that's the background

play12:05

colors and then I can also do the same

play12:07

for my text color start and text color

play12:09

end and so that just means I need to use

play12:11

those within what I'm doing down below

play12:14

but it's just adding all of that

play12:15

customization here so a really quick

play12:17

edit there I've just switched all of

play12:18

those out to those custom properties and

play12:20

then at the end of my animation I've put

play12:21

this here um so we have my color and my

play12:24

background color going to the end values

play12:26

over here and what I like about that is

play12:28

it means I don't have to come down here

play12:29

and make a change I'm controlling the

play12:31

entire look of what's Happening from

play12:33

here and I just the more controls I put

play12:36

as as custom property sort of the more I

play12:38

like it so we can see that it's still

play12:39

working except I'm getting uh very

play12:41

transparent which wasn't uh what I was

play12:43

after cuz I put a low opacity right

play12:46

there we can fix that um and play around

play12:48

with my numbers and there we go that's

play12:50

looking a little bit better I fixed my

play12:51

colors up and maybe this can go up to

play12:53

like 120 pixels or something uh just so

play12:55

our shift is happening so if you brought

play12:57

this into a project you could set it all

play12:58

up here

play12:59

uh and and everything would be working

play13:01

uh really good now one thing you'll

play13:03

notice here is I doing a surface end

play13:04

start and end I'd find it easier because

play13:06

I can control it all just through here

play13:09

uh there would be a way to do this with

play13:10

a single custom property and you just

play13:11

change the value of the custom property

play13:14

down here instead I personally like this

play13:17

other method more just because I don't

play13:18

need to go and look for my key frames

play13:20

after everything is just stored right

play13:21

here but it would be possible the only

play13:23

thing is you'd also have to register it

play13:25

as with like an app property if you

play13:27

wanted to change the value of the custom

play13:29

property and have it animated um so yeah

play13:32

if you've never done that that would

play13:33

just be another thing that you'd have to

play13:34

look into it's Houdini at propy it's not

play13:37

hard to do but I just find this this

play13:39

much easier and it's also going to help

play13:40

us a little bit with browser support um

play13:43

because if a browser doesn't support

play13:44

this what are we going to do right uh

play13:47

there's there is a polyfill that I

play13:49

haven't experimented with in this I you

play13:51

know the polyfill is a little bit

play13:52

Limited in what it can do so it would

play13:55

definitely be worth playing with and

play13:56

seeing if the polyfill Works uh but I'm

play13:59

going to suggest is just coming in uh

play14:01

and you have to decide do you want it to

play14:03

be a sticky nav or not uh if browsers

play14:06

don't support this so right now it's

play14:08

only in chromium browsers but it's over

play14:10

60% support uh and it's behind a flag in

play14:14

Firefox I believe I put links to browser

play14:16

support tables in the bottom um but what

play14:19

you could do is just come right here and

play14:21

you could say that at supports and this

play14:24

would just be

play14:25

animation timeline of view and then wrap

play14:30

that entire block let's make this bigger

play14:31

so we can actually see what I'm doing uh

play14:33

take this entire thing here and wrap it

play14:36

within the the at supports and what this

play14:40

means is uh you know is this supported

play14:42

or not and if the browser does not

play14:44

support this then it's not going to run

play14:47

through this but we will have a sticky

play14:48

navigation that means the color won't

play14:50

actually change so if I come and take a

play14:52

look in Firefox now it's just staying as

play14:54

a dark navigation everything is working

play14:56

no broken experience it just doesn't

play14:57

have that shift going on

play14:59

or maybe you don't like that you want it

play15:01

to be at the end State because you know

play15:03

this only works when it's at the very

play15:04

top you want it to stay sticky of course

play15:07

if you don't even want it to stick then

play15:08

you just move all of this into here

play15:10

right if I do that then in this one it's

play15:12

not a sticky nav it just works the same

play15:14

way it did before so that's obviously an

play15:16

option whereas here because it is

play15:18

supported it's working and everything is

play15:20

fine uh or the other option is you

play15:22

actually move this sticky nav linear

play15:25

forwards outside of the supports and I'm

play15:27

just doing an ALT up and down to move

play15:29

code around um just in case you didn't

play15:31

know that shortcut in BS code and as we

play15:34

saw when we first set this up what this

play15:36

means is it sees this animation and

play15:38

there's no time on it so the time is

play15:40

basically zero so it automatically jumps

play15:41

to the end and that means we always have

play15:43

the end state so we always have it like

play15:45

this and so this is probably going to be

play15:47

project dependent a little bit in

play15:49

context dependent on what colors you

play15:51

prefer do you want it with the light one

play15:53

I sort of like this with this simple

play15:54

demo that I made here um but it really

play15:57

depends on your use case and what you

play15:58

want uh but browser support is still an

play16:00

issue with this but I think it's one of

play16:02

those things that um is as a progressive

play16:05

enhancement is perfectly fine and

play16:07

there's different ways that we can

play16:08

progressively enhance it which is

play16:09

perfect you can decide on do you just

play16:11

don't want a sticky nav or do you want

play16:13

it to be the beginning Styles or do you

play16:15

want it to always be the end Styles so

play16:17

with that said let's look at how we can

play16:18

make this even better so for the better

play16:21

version I'm actually going to make this

play16:22

a dark theme and I'll see you in just

play16:23

one second all right so now we have a

play16:25

dark Theme website you can see my text

play16:26

looks better now as well the animated

play16:28

sticky nav is sticking out because of

play16:30

the color shift that I did uh both the

play16:33

first and second version of this the

play16:34

finished versions will be linked down

play16:36

below um just in case you want one or

play16:38

the other um and in this case I don't

play16:40

really want this to happen we want to

play16:41

get sort of that new trendy glass

play16:43

morphism style instead so for this

play16:46

there's a couple of fun little tricks

play16:48

and things along the way and we're going

play16:49

to sort of step it up slowly as we go

play16:51

through all of this and so for this uh

play16:53

to get this started the first thing I'm

play16:55

going to do is the end color I'm I'm

play16:57

going to make this uh

play16:59

we're going to do I'm trying to think I

play17:01

think I want it black still so 5% but

play17:03

we're going to give it like an opacity

play17:05

of 02 or something so it's darker you we

play17:08

it's there but it's it's very dark and

play17:10

my color at the end is going to stay

play17:12

light and uh I think we're actually do

play17:15

light here as well there we go so we

play17:16

just get like an opacity that's coming

play17:18

through on

play17:19

it uh which is going to work better for

play17:21

the effect that we're going to be after

play17:22

now the real trick here is when I first

play17:25

start scrolling I want the nav to go off

play17:27

with it but when we reach a certain

play17:29

point I want it to slide in that's the

play17:32

trick for here and the way we're going

play17:34

to do that is actually coming down here

play17:36

to the top and I'm going to show we're

play17:39

going to later come on and make a toggle

play17:41

so this could be like a feature that you

play17:42

could turn on and off if you want um but

play17:44

I'm just going to come to the top and

play17:45

make it -5 R uh and the reason for that

play17:49

is before we have any scrolling it's

play17:52

going to just be where it is in a normal

play17:54

flow and then as I scroll it will stick

play17:57

but and it CHS five cuz it's big enough

play18:00

that our element will be off the screen

play18:01

and it's sticking but it's sticking five

play18:04

REM out of our viewport so we don't

play18:06

actually see it cuz it's a negative 5

play18:07

REM just like if I did this at five REM

play18:10

it's it's sticking but it's it's five

play18:12

REM down so I'm doing the opposite

play18:14

negative 5 R and if ever like a little

play18:16

bit is sticking out you can make this a

play18:17

bigger value kind of magic numbery but

play18:19

whatever it's not the end of the world

play18:22

and it's sticking but it's sticking just

play18:23

out of our viewport um as we're

play18:25

scrolling down and that means if it's

play18:27

sticking here I can actually change

play18:30

where this Top Value is and again top

play18:32

isn't normally something you'd want to

play18:33

animate but I'm not after super buttery

play18:36

smooth animations now cuz it's scroll

play18:38

link position um so I don't think it's

play18:40

not something that's like animating in

play18:42

in different ways I think it's okay we

play18:45

could technically translate it in um so

play18:48

I think using the top here is okay

play18:50

though so I'm going to say that my top

play18:52

is now going to be say one REM down and

play18:56

that means you can see it it already

play18:57

worked going to start here and it's

play19:00

there it goes out it sticks and then as

play19:03

I continue going then it's going to

play19:05

animate in and slide back in except now

play19:08

it doesn't look very good so a couple of

play19:10

other things we're going to do let's

play19:11

come back up here and then I'm also

play19:13

going to add in I'm going to do this

play19:14

within my at supports cuz I don't really

play19:16

want this on there unless this is

play19:18

supported but I'm going to add a

play19:19

backdrop filter on there because that

play19:20

gives us a little bit of our glass

play19:22

morphism look uh that we're after and I

play19:24

think that looks a lot better so we'll

play19:26

put in that um and again that's just a

play19:28

backdrop filter of

play19:31

blur and then the other thing let's come

play19:33

back up to here and change this color a

play19:35

little bit just maybe a

play19:37

05 um would actually be enough opacity

play19:40

so it just gives us a little bit of a

play19:42

look to it uh and it blurs out so I

play19:44

think that's okay uh the other thing is

play19:47

I don't want it to be as wide and I

play19:49

actually want the logo to be gone and

play19:51

these two things that I'm going to do

play19:53

now aren't really animatable properties

play19:55

so we sort of need to trick things a

play19:58

little bit here to pull this one off uh

play20:00

and to make it look good and the reason

play20:03

I say that is and let's only put it

play20:06

here um we'll do the width first because

play20:09

that one's easier so we're going to say

play20:11

that the width is fit

play20:13

content and fit content just means like

play20:15

go as small as you can go without any

play20:17

line wraps and if it runs out of room

play20:19

then it will line wrap um if you want to

play20:21

make sure there's never any line wraps

play20:23

you could also use max content but then

play20:24

you would get horizontal overflow so it

play20:26

depends which one you want I'm going to

play20:28

go with fit content just cuz it's

play20:29

slightly safer and of course I've made

play20:31

this not mobile friendly um you you know

play20:33

I'm leaving that one up to you we're

play20:34

looking at the stickiness right now uh

play20:37

so there we go that's already looking a

play20:39

little bit better I guess we could also

play20:40

come up to here and add a border radius

play20:43

on all of

play20:45

this um I'll just do it here border

play20:48

border radius of 100 VW which means it's

play20:51

always going to be a pill shape so right

play20:54

away you can see it's here it's stuck or

play20:56

it's not stuck it's sitting there it

play20:58

Scrolls away and then it comes in but

play21:01

notice how it does this weird shift

play21:03

right here where the width is kind of

play21:05

like here it's okay but then it it

play21:07

basically instantly goes from its full

play21:09

width like let's I'll show you let's

play21:11

make this like 300 or 500 pixels or

play21:14

something really big so the distance

play21:17

should be long but it's basically like

play21:19

one it's instantly wide or instantly

play21:21

narrow and that's because we can't

play21:22

animate to a fit content um right from

play21:26

Auto so with can I do this I don't think

play21:28

I can no same idea we can't go from the

play21:31

width to a fit content to an auto or

play21:33

width 100% that's not going to work so

play21:36

we need to come up with a different way

play21:37

of doing that uh but I also need to hide

play21:39

my logo so we're going to do both of

play21:41

those at the same time because it's a

play21:42

very similar thing that we're going to

play21:44

do little wardrobe change Kevin from the

play21:46

future here I was watching the my edited

play21:49

version and then realized I did a bit of

play21:50

a contrived solution here it could be a

play21:52

lot easier so we're going to look at the

play21:53

simpler solution uh that I should have

play21:55

done in the first place uh for get the

play21:59

that problem um with the sizes and

play22:01

everything uh to work so what we're

play22:04

going to do is let's do the we as I said

play22:07

we'll do both at the same time and what

play22:10

it basically involves is let's find my

play22:11

where I have my logo set up here where I

play22:13

just have the fill and I have a height

play22:15

on it because it sort of makes sense for

play22:16

a logo uh I'm going to do a display on

play22:18

here as well and my display is going to

play22:21

be a custom property that I'm not going

play22:23

to have defined which is going to be uh

play22:25

logo display and I'm just going to do a

play22:28

comma and actually I put none there but

play22:30

I want block so what that means is um my

play22:34

my logo display is set to block by

play22:36

default because this is an undefined

play22:38

custom property so just means if the

play22:41

browser can't find a value for that it's

play22:43

going to use this as the value and the

play22:46

reason I want to do that is because then

play22:48

I can actually switch it within my

play22:50

animation because we can't I I don't

play22:51

want to have to have multiple different

play22:53

animations I want this one animation to

play22:55

be for everything and so by having as a

play22:58

custom property that makes it a little

play23:00

bit easier and I don't even need to

play23:02

worry about um doing this as a

play23:04

registered custom property with like an

play23:05

app property or anything like that

play23:07

because we're not animating this it's

play23:09

just going to instantly change right

play23:10

we're going from block To None instantly

play23:13

so that's going to be set there and

play23:15

we're going to do the same thing for the

play23:17

width where I can come up um and I don't

play23:19

have a width set anywhere um on my

play23:21

header right now but we can do the same

play23:23

thing where on this we can come up with

play23:25

a width and this one doesn't technically

play23:27

need to be a custom property cuz the

play23:29

animation is directly on here uh but it

play23:31

just makes it a little bit more obvious

play23:34

what's happening I think so I'm just

play23:35

going to call this one header width and

play23:37

the default will just be Auto which it

play23:38

already is this is the the value or what

play23:41

we have right now um and we're going to

play23:43

save that and the reason I'm doing that

play23:44

is because I want to come down here and

play23:46

change them there is a catch to this

play23:48

though and we'll see here um and well we

play23:51

saw that already with the width where it

play23:52

wasn't really working properly right cuz

play23:53

if we have

play23:54

the uh when we change I can change my

play23:57

custom properties here so if I did the

play23:59

header width and we'll do the logo

play24:02

display as well uh so here I can change

play24:05

this to be my FIT content like we were

play24:07

just playing with and then I'm also

play24:08

going to do my header display and make

play24:11

that

play24:12

none and that just means that the my

play24:16

header display is not working oh not

play24:18

header display that should be logo

play24:19

display

play24:20

sorry and so it's that same problem we

play24:23

have where it's like halfway through the

play24:25

animation we're switching from this to

play24:28

the other one

play24:29

so instead of doing these here we're

play24:30

going to do them somewhere else because

play24:32

we can have different key frames set up

play24:34

and here I'm going to do a 0% 100%

play24:37

instead and throw them in there and that

play24:39

just means that these are going to

play24:41

switch as soon as the animation starts

play24:45

and why this is going to work is because

play24:46

the animation is starting when our logo

play24:49

is stuck five REM off the top of the

play24:51

page so because it's happening off the

play24:53

page it just seems to slide in and

play24:55

everything is perfectly fine and good uh

play24:58

and there's a few extra things we could

play25:00

do here just to make it a little bit

play25:01

more customizable as well so if we go

play25:04

all the way back up to here um just like

play25:06

I have these set as my custom properties

play25:09

but what we could do is set uh things

play25:11

here that would be sort of like settings

play25:13

so again we wouldn't have to jump down

play25:14

to the other area down below right this

play25:17

is our settings that we want to keep so

play25:19

I could say uh logo after animation is

play25:24

it uh so we could have it stay block or

play25:28

we could could have it go to Hidden and

play25:30

then because we have this here what we

play25:32

could do is we could come down all the

play25:34

way to inside the animation where I have

play25:37

the none here and I could have that look

play25:39

at that other one so this would be my

play25:41

logo after

play25:43

animation um or maybe you can come up

play25:45

with a better name for it we can do the

play25:46

same thing for the fit content for the

play25:48

width uh you can do the same thing with

play25:50

your border radius and and other things

play25:52

like that so You' sort of pass off all

play25:54

the different things that would end up

play25:55

being down there um and I put hidden

play25:57

here but we want to have it as none um

play25:59

right so if we do that it should still

play26:01

work perfectly fine you can see there we

play26:03

go so it's switching over to this but

play26:04

only in the second half and I like doing

play26:07

that as much as possible again so I

play26:09

don't have to look up here for some

play26:10

stuff and then go and find a key frames

play26:12

animation somewhere else the more I'm

play26:13

controlling here the better so I'd

play26:15

encourage you to play with this a little

play26:16

bit make it more of your own you can add

play26:18

some more customization to it and so if

play26:20

you did enjoy this video and you'd want

play26:22

to do a bigger Deep dive into the

play26:24

different types of things you can do

play26:25

with the animation timeline and we have

play26:26

a scroll and a view and there's a lot of

play26:28

different controls and different things

play26:30

we can do with it and really fun effects

play26:31

that we can create without any

play26:33

JavaScript now which is always very

play26:34

exciting especially for someone like me

play26:36

but if you're interested in that that

play26:37

video is right here for your viewing

play26:39

pleasure and with that I would like to

play26:40

thank my enablers of awesome Philip

play26:42

Andrew Simon Tim and Johnny as well as

play26:44

all my other patrons for their monthly

play26:45

support and of course until next time

play26:47

don't forget to make your corner of the

play26:48

internet just a little bit more awesome

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
CSS NavigationColor TransitionGlass MorphismWeb DesignFront-End DevelopmentCustom PropertiesScroll AnimationUI/UXResponsive DesignCode Tutorial