Award Winning Animation With Only 20 Lines Of CSS?

Hyperplexed
5 Dec 202206:59

Summary

TLDRIn this script, the speaker is amazed by the smooth movement of a website they're working on, setting limits to their coding challenge with 10 lines of HTML, 20 of CSS, and 30 of JavaScript. They create a track for images, fix their display, and implement a panning feature based on mouse movement. The challenge involves creating an invisible slider that responds to clicks and drags, calculating the percentage of movement for a smooth parallax effect. The process involves handling mouse events, translating the track, and animating the images for a seamless user experience.

Takeaways

  • đŸ˜Č The speaker is impressed by the smoothness of the site's movement and challenges regular people to compete with it.
  • 📝 The speaker sets limits on the amount of code to be used: 10 lines of HTML, 20 lines of CSS, and 30 lines of JavaScript.
  • đŸ–Œïž The body of the site is styled to be a full-size black area with no scrollbars, using 7 out of 20 lines of CSS.
  • đŸ–Œïž Images are contained within an element called 'track', and the process of copying and pasting is repeated seven times, using up 10 out of 10 lines of HTML.
  • 🔍 The images are given a fixed width and height to maintain the aspect ratio and are centered on the page, using 14 out of 20 lines of CSS.
  • 🔄 The speaker discusses using display Flex to align images side by side and position absolute to set left and top positions, keeping the code under 20 lines.
  • 🔄 The speaker realizes the need for a panning feature and notes the challenge of images moving at different speeds compared to the track.
  • đŸ–±ïž The interaction starts with a mouse down event, and the speaker decides to store the exact X position of the mouse as the starting point for an invisible slider.
  • 📏 A mouse move listener is implemented to calculate the relative position of the mouse and convert it to a percentage to move the track.
  • 🔄 The speaker corrects the direction of the track movement by flipping the sign and stores the percentage value to continue from the last position after releasing the mouse.
  • đŸš« The speaker addresses the issue of the slider moving infinitely by setting minimum and maximum values using the percentage value.
  • 🌐 A parallax effect is implemented by adjusting the vertical and horizontal center of the images based on the percentage value of the slider.
  • 🎹 To enhance the smoothness, the speaker suggests animating the position changes of the images using a custom animation function.

Q & A

  • What is the primary challenge the speaker is addressing?

    -The speaker is addressing the challenge of creating a smooth, interactive movement effect within strict line limits for HTML, CSS, and JavaScript.

  • What are the specific line limits set for HTML, CSS, and JavaScript?

    -The specific line limits are 10 lines of HTML, 20 lines of CSS, and 30 lines of JavaScript.

  • How does the speaker intend to contain the images?

    -The speaker plans to contain the images by creating a track element where the images are copied and pasted multiple times.

  • What method is used to center the images and ensure they maintain the correct aspect ratio?

    -The images are centered and the aspect ratio is maintained using CSS properties like width, height, and position adjustments with Flexbox and absolute positioning.

  • What functionality does the speaker want to achieve with mouse movement?

    -The speaker wants to achieve a panning effect where the track moves left and right based on mouse movement, creating a smooth interaction.

  • How does the speaker calculate the distance the track should move based on mouse movement?

    -The distance is calculated by tracking the starting X position of the mouse on mousedown, then using the relative position during mousemove to determine how far the track should move, converting this to a percentage of the maximum distance.

  • Why does the speaker mention the need for an 'invisible slider'?

    -The 'invisible slider' concept helps in visualizing and implementing the interaction where the track moves in response to the mouse movement, even though there is no visible slider element.

  • What issue does the speaker encounter with the initial implementation of the click and drag feature?

    -The initial issue is that the track position resets when dragging a second time because the progress of the slider was not stored, causing it to start from zero each time.

  • How does the speaker solve the problem of the track resetting its position?

    -The problem is solved by constantly tracking and storing the percentage of the slider's position whenever the mouse is released, allowing the next drag to continue from the last position.

  • What additional effect does the speaker want to add to the images, and how is it achieved?

    -The speaker wants to add a parallax effect where the images move at different rates. This is achieved by adjusting the percentage values used to position the images and implementing animations to ensure smooth transitions.

Outlines

00:00

đŸ˜Č Mastering Web Design with Advanced CSS and JavaScript Techniques

The speaker expresses astonishment at their own proficiency in web design, setting limits on the amount of code used for HTML, CSS, and JavaScript to challenge themselves. They demonstrate creating a responsive design with a 'track' for images, adjusting their size, aspect ratio, and alignment. The speaker then introduces a panning feature based on mouse movement, explaining the technical steps to create an invisible slider that responds to mouse down and move events, using custom values and calculations to achieve smooth, continuous motion.

05:01

🔄 Implementing a Dynamic Slider and Parallax Effect with CSS Animations

After resolving issues with the initial click-and-drag functionality, the speaker discusses storing the user's progress to maintain continuity between interactions. They then address the lack of limitations in the slider's movement by implementing min and max functions. The speaker also introduces a parallax effect for images, adjusting their vertical and horizontal centering based on the slider's position. To enhance the user experience, they replace direct CSS updates with animations, ensuring a smooth transition effect that persists after the animation completes.

Mindmap

Keywords

💡HTML

HTML, or HyperText Markup Language, is the standard language used for creating the structure and content of web pages. In the video, HTML is mentioned as the starting point for the project, with a limit of 10 lines, indicating the importance of concise and efficient coding in web development.

💡CSS

CSS stands for Cascading Style Sheets, a language used for describing the presentation of a document written in HTML or XML. The script mentions a 20-line CSS limit, emphasizing the need for styling the webpage within constraints, which is a common challenge in front-end development.

💡JavaScript

JavaScript is a high-level, interpreted scripting language that is a core technology, alongside HTML and CSS, for building interactive web applications. The script sets a 30-line limit for JavaScript, highlighting the task of creating dynamic functionality within a limited code scope.

💡Aspect Ratio

The aspect ratio is the ratio of the width to the height of an image or screen. In the script, maintaining the aspect ratio is crucial for ensuring images look correct when resized, as it is mentioned when fixing the images' width and height.

💡Flexbox

Flexbox is a CSS layout mode that allows for the arrangement of elements in a single dimension, either as a row or a column. The script discusses using Flexbox to align images side by side, demonstrating its utility in creating responsive and flexible layouts.

💡Mouse Event Listeners

Mouse event listeners are used in JavaScript to detect and respond to user interactions with the mouse, such as clicks or movements. The script describes implementing mouse down and move listeners to create an interactive feature, illustrating the concept of event-driven programming.

💡Translate Property

The translate property in CSS is used to move an element from its current position without changing its position in the document flow. The script refers to using the translate property to shift the slider and track elements, showcasing its role in creating dynamic visual effects.

💡Parallax Effect

The parallax effect is a visual phenomenon where objects at different distances appear to move at different speeds when viewed from a moving vantage point. The script discusses creating a parallax effect with images, where the background moves slower than the foreground, adding depth to the web page.

💡Drag and Drop

Drag and drop is a user interface feature that allows users to move or position elements by clicking, holding, and then dragging the mouse. The script mentions issues with the default drag behavior and how to disable it, which is a common interaction pattern in web applications.

💡Animation

In web design, animation refers to the process of creating motion or the illusion of motion in the user interface. The script talks about adding animation to the movement of images for a smoother effect, demonstrating how animation can enhance user experience.

💡Custom Value

A custom value in web development often refers to a user-defined variable or property that is not part of the standard HTML or CSS specifications. The script mentions creating a custom value on the track element to store the mouse down position, illustrating the concept of state management in interactive web elements.

Highlights

Camille's impressive skills are highlighted, making it difficult for regular people to compete.

The movement's smoothness can only be truly appreciated by visiting the site and trying it out.

Setting limits on HTML, CSS, and JavaScript lines to challenge the coding process.

Starting with the body element, setting dimensions and eliminating scroll bars.

Efficiently using 7 out of 20 lines for initial CSS setup.

Images are contained within a 'track' element for organization.

Fixing image dimensions and aspect ratio with CSS to center them.

Using Flexbox to align images side by side with space between them.

Positioning images with absolute positioning to adjust their alignment.

Implementing a panning feature that moves the track left to right based on mouse movement.

Understanding the relationship between mouse movement and track movement speed.

Creating an invisible slider controlled by mouse down events.

Using mouse move events to calculate the relative position and adjust the track's movement.

Storing the mouse down position as a custom value on the track element.

Preventing the default dragging behavior by setting the draggable attribute to false.

Storing and updating the percentage value of the slider for continuous movement.

Adding a parallax effect to images by adjusting their vertical and horizontal centering.

Animating the images' movement for a smoother visual experience.

Incorporating min and max functions to limit the slider's movement range.

The final implementation combines various techniques for a dynamic and interactive user experience.

Transcripts

play00:00

Camille normal you've got to be kidding

play00:02

me you're too good at this I just don't

play00:04

know how you possibly expect us regular

play00:06

people to compete like I just can't

play00:07

explain to you guys how smooth this

play00:09

movement is unless you actually go to

play00:11

this site and try it it's insane all

play00:13

right I'm setting some limits so this

play00:14

doesn't go on too long 10 lines of HTML

play00:16

20 lines of CSS 30 lines of JavaScript

play00:19

let's see how far we can get we'll start

play00:21

with the body easy 100 this way 100 that

play00:24

way black no scroll bars 7 out of 20

play00:26

lines used these images got to be

play00:28

contained in something let's call it a

play00:30

track on splash.com copy paste repeat

play00:33

seven more times 10 out of 10 lines used

play00:36

we are done here let's fix these images

play00:38

give them a width give them a height fix

play00:40

the aspect ratio and Center it 14 out of

play00:43

20 lines yikes okay gotta line them up

play00:46

side by side display Flex add some space

play00:49

between position absolute so I can set

play00:51

the left and top positions to halfway

play00:53

okay left looks good but top doesn't so

play00:56

let's shift things up just a smidge oh

play00:58

wow that's crazy I cannot believe

play00:59

believe I somehow managed to keep that

play01:02

at 20 lines or less great job me all

play01:04

right let's make this thing work I say

play01:06

we start with a panning feature just

play01:07

making it move left to right but wait I

play01:10

can tell there's some trickiness going

play01:11

on here because the images are moving at

play01:13

a different rate of speed compared to

play01:15

the track as a whole noted I don't know

play01:17

much at this point but the one thing I

play01:19

do know is that this effect is based on

play01:21

Mouse movement when I press my mouse

play01:22

down at any given point on the screen

play01:24

and then drag in the Left Right

play01:26

Direction the entire track is moving but

play01:28

how do I know how far or how fast let's

play01:31

see how far I have to drag to get from

play01:32

one end of the track to the other aha so

play01:34

by the time the mouse is halfway across

play01:36

the screen the track has moved 100 of

play01:39

its maximum distance so it's almost like

play01:41

we can imagine there's a slider on the

play01:42

screen and dragging it back and forth

play01:44

controls our track but this slider is

play01:46

extra fancy not only because it's always

play01:48

following our Mouse around waiting for

play01:50

us to click and drag but because it's

play01:52

completely invisible as well so how do

play01:54

we go about creating our magical

play01:55

invisible slider well the very first

play01:57

step to this interaction is pressing our

play01:59

Mouse

play02:00

so let's start by listening to any Mouse

play02:02

down event that may occur when that

play02:04

happens I know I want to keep track of

play02:06

the exact X position the mouse was at so

play02:08

we can use that as the starting point

play02:09

for our invisible slider but where are

play02:11

we going to store that value what if we

play02:13

just created a new custom value on our

play02:15

track element that will update every

play02:17

time the mouse is pressed down all right

play02:19

what happens next step one was Mouse

play02:21

down step two the mouse moves so we need

play02:24

a mouse move listener we need a way to

play02:26

know how far a mouse has traveled

play02:27

relative to the starting point as well

play02:29

as where the other end of our slider

play02:31

should be the relative position is easy

play02:32

enough just subtract the current

play02:34

position from the starting point and we

play02:36

already know the max distance is one

play02:37

half of the width of the window so

play02:39

dividing the relative position by the

play02:41

max distance would convert this value to

play02:42

a decimal and multiplying the decimal by

play02:44

100 gives us a percentage oh wait my

play02:47

head hurts why were we even doing this

play02:49

again oh right because clicking and

play02:50

dragging halfway across the screen has

play02:52

to move our image track from one end all

play02:55

the way to the other only this

play02:56

functionality has to be possible no

play02:58

matter where we click on the screen so

play03:00

by track tracking the starting point and

play03:01

using it to create a virtual slider

play03:03

using math we can thereby obtain the

play03:05

value we truly need which is the

play03:07

percentage of our slider that has been

play03:08

slid now if we recall earlier we use the

play03:11

translate property to shift our slider

play03:13

up by 50 of its height well thanks to

play03:15

this value here we can perform a very

play03:17

similar manipulation in the left right

play03:18

direction for instance if we set this

play03:20

value to negative 50 percent our track

play03:22

moves fifty percent in the left

play03:24

direction if we set it to negative one

play03:26

hundred percent our track has moved all

play03:27

the way to the opposite end fortunately

play03:29

since we just spent all that time

play03:31

calculating the percentage value of our

play03:32

magic slider the next step is easy we

play03:34

simply use this value to set the Left

play03:36

Right percentage on our track and whoa

play03:38

whoa hang on the mouse move listener has

play03:41

no way of knowing when the mouse is

play03:42

actually down so the track is moving

play03:44

before it's supposed to well since I

play03:46

know the mouse down at position is zero

play03:48

before the mouse is pressed we could

play03:50

just say that if that's the case return

play03:52

immediately and skip the code that's

play03:54

telling the track to move but as soon as

play03:56

the user clicks the value will no longer

play03:58

be zero so we'll have to set it back

play04:00

back to zero when they release the mouse

play04:01

okay so now when I click an image it

play04:05

just drags It Off The Track stupid

play04:07

default Behavior fine we'll just set

play04:09

draggable to false okay no more weird

play04:11

dragging but we're clearly going in the

play04:13

wrong direction here so I guess we gotta

play04:15

flip the sign all right looking good

play04:17

here wait why is the position resetting

play04:19

when I try to drag a second time are you

play04:21

kidding since we're not keeping track of

play04:23

how far we've dragged the slider instead

play04:25

of continuing from where it was left off

play04:27

it just resets back to zero fine okay so

play04:30

just like we stored the mouse down

play04:32

position with the track we're gonna have

play04:33

to store the percentage there too I'm

play04:35

guessing that way we can take the

play04:37

current percentage slid and add it to

play04:39

the last percentage to get the new

play04:41

percentage so we need to store this

play04:43

value every time they release the mouse

play04:45

but how do we get that value from the

play04:47

mouse move listener okay what if we

play04:49

constantly update the percentage here

play04:51

and then use it purely as a way to

play04:52

transfer the value between the two aha

play04:55

there we go now when I click and drag a

play04:56

second time it continues from where we

play04:58

left off rather than resetting back back

play05:00

to the default position of zero boy we

play05:02

really got off in the weeds again there

play05:04

what the heck just happened right so our

play05:06

click and drag was technically working

play05:08

but only if it was the first time the

play05:10

user had ever moved the track what we

play05:11

failed to account for was that the next

play05:13

time the user decides to click and drag

play05:15

their progress wasn't stored anywhere so

play05:17

the track had no way of knowing it

play05:18

should no longer start at zero by

play05:20

constantly tracking the percentage and

play05:22

storing it when they released the mouse

play05:23

we can use that value as the new

play05:25

starting point when determining the

play05:27

percentage the next time phew okay now

play05:29

our movement's solid but I'm noticing

play05:31

our slider doesn't exactly have any

play05:33

limitations at the moment as in I can

play05:35

keep dragging it infinitely in either

play05:36

direction so I'm thinking we gotta take

play05:38

our next percentage value in combination

play05:40

with the min max functions to set a

play05:43

maximum value of 0 and a minimum value

play05:45

of negative one hundred wait how many

play05:47

lines are we at 24 not bad what's left

play05:50

uh oh right that Parallax effect with

play05:53

the images okay so here's what I'm

play05:55

thinking as of now our images are

play05:57

positioned in the center which

play05:58

technically we can split into a vertical

play06:00

Center and a horizontal Center and

play06:03

really these values are just a nickname

play06:04

for 50 but what if we start this one at

play06:07

100 and then use the exact same

play06:09

percentage value that we used for the

play06:11

track to slide the image positions from

play06:13

100 down to zero so all we have to do to

play06:15

implement this is Loop over our images

play06:17

replace the first value with next

play06:19

percentage and since next percentage

play06:21

runs from 0 to negative 100 we can just

play06:23

add 100 to it to correct the range okay

play06:25

it's definitely working correctly but it

play06:27

just doesn't quite have that buttery

play06:29

smooth feeling that Camille's did so

play06:30

what if instead of updating the CSS

play06:32

directly we gave them a little animation

play06:34

instead let's replace the style updates

play06:36

with the dot animate function we'll set

play06:37

the duration to 1200 and the fill to

play06:40

forwards so the animation is persisted

play06:42

rather than the default behavior of

play06:44

resetting after completion all right I

play06:46

think we've passed our last line limit

play06:47

so we better stop there take that

play06:49

Camille just kidding you're awesome

play06:51

thank you for inspiring us foreign

play06:56

[Music]

Rate This
★
★
★
★
★

5.0 / 5 (0 votes)

Étiquettes Connexes
Web DesignCSS TricksJavaScriptInteractive TutorialMouse TrackingParallax EffectDynamic ContentFrontend DevelopmentUser InteractionWeb AnimationCoding Challenge
Besoin d'un résumé en anglais ?