I never thought of using CSS animations like this before!

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ธ.ค. 2024

ความคิดเห็น • 129

  • @RobinPoort
    @RobinPoort 7 หลายเดือนก่อน +24

    Another great use for an animation with forwards is when you add a class with JS to make an element go from display: none; to display: block;. A transition won’t work here since the display value changes however an animation using forwards will kick in nicely. Great for a dropdown of any kind. 👌

  • @RandomGeometryDashStuff
    @RandomGeometryDashStuff 7 หลายเดือนก่อน +8

    03:34 here is my attempt solving (assuming not allowed to edit javascript):
    .hover-box::before{
    /*offset-path can be simpler if allowed to edit javascript*/
    offset-path:polygon(50% 50%,calc(var(--x,0px) + 50%) calc(var(--y,0px) + 50%));
    offset-rotate:0rad;
    offset-distance:0%;
    transition:offset-distance 500ms;
    }
    .hover-box:hover::before{
    offset-distance:50%;
    }

    • @ste-fa-no
      @ste-fa-no 7 หลายเดือนก่อน +1

      How elegant!! 🤩 I love it! ⭐

    • @lgasc
      @lgasc 7 หลายเดือนก่อน +1

      Creative!

  • @spicybaguette7706
    @spicybaguette7706 7 หลายเดือนก่อน +27

    That's so interesting! What's also cool is that if you move your mouse in a line at a constant speed, and it enters the box, the dot traces out a quadratic curve. Because the CSS animation linearly interpolates between (0, 0) and (--x, --y), and you moving the mouse is also a linear motion

    • @nikilragav
      @nikilragav 7 หลายเดือนก่อน

      are you sure that's a quadratic??

  • @maximebarzel9701
    @maximebarzel9701 7 หลายเดือนก่อน +10

    Great solution! To simplify further, I think you can omit both "translate: 0 0" since they represent the default state. Just include an "enter" to and an "exit" from.

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน +3

      Yup, definitely could have :)

  • @refeals
    @refeals 7 หลายเดือนก่อน +2

    pretty clever solution! css animations are awesome
    1:40 my first thought here was to add a selector without the hover to set the transition off, meaning the transition works one way when there's a :hover and another way when there's not. I already did some things that way, so I know it could work. but i'm not sure it would work for this problem exactly.

  • @ismailcreatvt
    @ismailcreatvt 7 หลายเดือนก่อน +6

    Awesome! You never cease to amaze me with your elegant solutions. ❤

  • @eythandavid6897
    @eythandavid6897 4 หลายเดือนก่อน

    I love this video! Is all I was needing to improve my animations skills

  • @UltraMarlin
    @UltraMarlin 7 หลายเดือนก่อน +4

    This is a pretty neat technique, thanks for sharing!
    This might be nitpicky, but unfortunately the animation still jumps if you exit the hover state before the enter animation has finished playing. You can see this if you quickly move your mouse in and out of the square in the "Hover follow finished version" codepen.
    In most cases this probably won't be noticeable, but it would still be cool to figure out a solution to this.

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน +1

      Yeah I know. I don't think there's a solution to that though, at least with this technique. Like you said, in most actual use cases you wouldn't notice this.

    • @goodshiro10
      @goodshiro10 7 หลายเดือนก่อน +1

      So, in the end, should we care about that? Or leave it as it is because it's not noticeable? or it depends?

    • @CottidaeSEA
      @CottidaeSEA 7 หลายเดือนก่อน +1

      @@goodshiro10 I don't think there's a good solution outside of JS for now, so if it matters then using JS animations is probably the way to go.

    • @ste-fa-no
      @ste-fa-no 7 หลายเดือนก่อน +1

      @RandomGeometryDashStuff 's answer doesn't have this issue 😲

  • @helleye311
    @helleye311 5 หลายเดือนก่อน

    Just used this, great trick for basically any reversible animation. Thank goodness I saw this video, or I'd have to make a mess of the css to actually make things work otherwise.

  • @artursavchuk2363
    @artursavchuk2363 6 หลายเดือนก่อน

    looks grate, thank you for this example

  • @LasTCursE69
    @LasTCursE69 7 หลายเดือนก่อน +9

    Kevin is a combination of a calm pleasant voice + great explanation + vast extensive knowledge of the subject

  • @ajgubi19
    @ajgubi19 7 หลายเดือนก่อน

    You are such a good educator, dude.

  • @Rtzoor
    @Rtzoor 7 หลายเดือนก่อน

    i fell like i leveled up with a really powerful technique, thanks kevin!

  • @ApprendreSansNecessite
    @ApprendreSansNecessite 7 หลายเดือนก่อน +2

    8:22 this got me bang my head against the wall out of frustration

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน +1

      Yeah, definitely one of those animation gotchas, to go along with 0 requiring a unit for the timing, unlike in most use cases in CSS

  • @LaysarOwO
    @LaysarOwO 7 หลายเดือนก่อน

    thats beautiful. never fully understood css animations (still dont), but this gave me a bit more insight

  • @Diamonddrake
    @Diamonddrake 7 หลายเดือนก่อน

    This is a useful concept! Thanks!

  • @nikilragav
    @nikilragav 7 หลายเดือนก่อน +1

    Cool. This is actually slightly broken - it's obvious if you slow down the animation to 1000ms. If you enter, exit, enter before the animation is fully finished, you'll see some jumping. I think you need to save the last x,y of the circle upon exit via js and upon entry
    enter animation
    from last x y,
    to current x y
    exit animation
    from last x y
    to 0 0

  • @levsonc
    @levsonc 7 หลายเดือนก่อน

    CSS animations are great for things like animated appearance of elements. In order to use transitions one need add an element to DOM with initial state, and then change it properties in the next moment, so that there would be a transition between states. Animations help to avoid that extra step - they just play from the start.
    However, there is one caveat with them: I don't recommend to mix animations with transitions - Safari starts flickering in that case. Use only one approach on the same element or nested elements.

  • @Ma1ne2
    @Ma1ne2 7 หลายเดือนก่อน

    So much potential with this one!

  • @Nekroido
    @Nekroido 7 หลายเดือนก่อน

    Ohh, Imma do a cyberpunk style one. Thanks for the video! Really interesting effect with an elegant solution ❤

  • @pineappleanimates
    @pineappleanimates 7 หลายเดือนก่อน +62

    only those with notifications on know the thumbnail originally said "animtion" lol

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน +25

      oops!

    • @rodneymkay
      @rodneymkay 7 หลายเดือนก่อน +3

      Yea, saw the idea for the solution in the thumbnail, so it wasn't really that much of a challenge :p

    • @iamtharunraj
      @iamtharunraj 7 หลายเดือนก่อน +2

      I had notification but didn't nice it lol

    • @blauergrashalm4201
      @blauergrashalm4201 7 หลายเดือนก่อน +3

      well, and those who read your comment, i guess.

  • @ahmad-murery
    @ahmad-murery 7 หลายเดือนก่อน +1

    Very interesting concept.
    Thanks Kevin!

  • @SvenThielen
    @SvenThielen 7 หลายเดือนก่อน +1

    A really nice animation effet. The only issue with this code is when you move very quickly in and out of the hover box. Instead of moving back to the edge from where it was when it was returning, it will instead jump back to 0,0 then move to the edge again. So a quick move in and out of the box cause some glitching.

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน +2

      Yeah, I don't think there's a way around that with this method, but for more subtle versions it's okay. If you need it to be more like I did here, some more JS would be needed, I think

  • @Risha1792
    @Risha1792 7 หลายเดือนก่อน

    That's was awesome, I didn't know about "forward/reverse"😍

  • @austinkern8358
    @austinkern8358 7 หลายเดือนก่อน

    Thank you, Kevin!

  • @MrSofazocker
    @MrSofazocker 7 หลายเดือนก่อน

    Hm... Transition would still be preferred, if you move your mouse outside the boxy before the animation finished playing, the exit animation will always begin with the mouse position.
    So after a short hover, it jumps from its current position to your mouse, then animates back to the center/starting position.
    That's why we use js animation since css doesn't retain state.
    Better solution:
    Have the naive implementation of a transition translate 500ms. on the before element.
    And --x, --y on the :hover state.
    To get rid of the transition/lag once the ball reached your mouse, aka the durtation of the transition.
    Make the "enter" animation, animation to transition 0ms.
    You're welcome.

  • @billyvg
    @billyvg 7 หลายเดือนก่อน

    This is very cool! Definitely going to use this

  • @LokiDaFerret
    @LokiDaFerret 7 หลายเดือนก่อน

    This is extremely handy to know thank you!

  • @cyberprompt
    @cyberprompt 7 หลายเดือนก่อน

    pretty good timing. i'm working on an app that uses canvas (fabric.js) that is a specific editor to load background and transparent foreground images. This part could be used to augment the current "eraserbrush circle" that follows the cursor when active. Once i get all the bugs out... I might sweeten the UX with this tidbit. thx.

  • @FjorimDerHuene
    @FjorimDerHuene 7 หลายเดือนก่อน +1

    Very cool! A funny thing, though, is that the original solution (the one the guy "stole" it from from whom you "stole" it from) says in its code "do not share this!" and in its title it's a "secret" ;-)

  • @TristanGrimaux
    @TristanGrimaux 7 หลายเดือนก่อน

    Really cool animation! Only one little thing: You don't need the "forwards" key on the exit animation

  • @danielmadison4451
    @danielmadison4451 7 หลายเดือนก่อน

    Kevin, very nice! Thanks

  • @KeithGrant
    @KeithGrant 7 หลายเดือนก่อน

    This is a fantastic technique!

  • @Voidstroyer
    @Voidstroyer 7 หลายเดือนก่อน

    Eventhough I recently learned that you are saying "Hello my frontend friends", I still can't unhear "Hello my friend and friends" lol

  • @zsteinkamp
    @zsteinkamp 8 หลายเดือนก่อน +3

    Very cool. Thanks!

  • @anandv1391
    @anandv1391 7 หลายเดือนก่อน

    You are my css master❤

  • @omrividal
    @omrividal 7 หลายเดือนก่อน

    Wow what a great solution! Might be very useful for me thank you 🙏🏼

  • @LePhenixGD
    @LePhenixGD 7 หลายเดือนก่อน +1

    3:40 I would've used the Animation web API to handle the behavior, I have no idea on how to do it in CSS lol

  • @penguinxed
    @penguinxed 7 หลายเดือนก่อน

    that's pretty cool and interesting

  • @3dfactor
    @3dfactor 7 หลายเดือนก่อน

    You should really check out more of Ana Tudor stuff. She's a CSS wizard.

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน

      I've followed her for a long time ☺️

  • @RobertMcGovernTarasis
    @RobertMcGovernTarasis 7 หลายเดือนก่อน

    So dang nice!

  • @aaaidan
    @aaaidan 7 หลายเดือนก่อน

    Can you use a transition to drive animation-delay? If so, it might be possible to have the “playhead” between 0 and var(--x) be smoothly interpolated when you hover and leave. Not sure if this can work.

  • @Tom-og3fr
    @Tom-og3fr 7 หลายเดือนก่อน +1

    Brilliant!

  • @XCanG
    @XCanG 7 หลายเดือนก่อน

    I wonder what the reason for other styles being placed within @layer?

  • @mixxxer
    @mixxxer 7 หลายเดือนก่อน

    I’m more intrigued with these variable values. Can css vars be updated directly with js???

    • @kiliknp5141
      @kiliknp5141 6 หลายเดือนก่อน

      U Can Do anything with js

  • @TheMrArmbull
    @TheMrArmbull 7 หลายเดือนก่อน

    If it was css only, it would be incredible. Love when css replace js in some popular cases😊)

  • @nadavgover6017
    @nadavgover6017 7 หลายเดือนก่อน

    Learned something new today

  • @MaxFunoff
    @MaxFunoff 7 หลายเดือนก่อน

    Interesting solution, might be useful for me, I thought about applying transition on non ::hover and then unset it on ::hover but then it jumps when u hover in but does not jump when u hover out, so yours is better.

  • @pimredrum9375
    @pimredrum9375 7 หลายเดือนก่อน

    Beautiful

  • @vasyaqwe2087
    @vasyaqwe2087 7 หลายเดือนก่อน

    I was trying to solve this with js and didn't even know how to google this type of thing😅Thanks!!

  • @bizmich_
    @bizmich_ 7 หลายเดือนก่อน

    Hi Kevin! I have a question regarding CSS layout. I have a card element with overflow: hidden. However, a rounded close icon within this card is partially cut off. How can I solve this issue?

  • @andrejwalilko634
    @andrejwalilko634 7 หลายเดือนก่อน

    Reminds me of the tunnel bear login page :)

  • @lucavogels
    @lucavogels 7 หลายเดือนก่อน

    Wouldn’t a simple transition as exit work instead of an extra exit animation?

  • @slowpankha
    @slowpankha 7 หลายเดือนก่อน +1

    .hover-box:not(:hover)::before {
    transition: translate 250ms ease;
    translate: 0;
    }
    This works for me. Is this correct or am I missing something? This feels simpler than adding keyframes.

  • @veemy23
    @veemy23 7 หลายเดือนก่อน

    the solution was always 'forward' ..
    thank you very much for the video

  • @gorilla-san
    @gorilla-san 7 หลายเดือนก่อน

    Cool stuff. I would assume that variables would be replaced with their current values at the start of the animation and the animation would stay at those numbers, and not continue to be connected with the variables

  • @AScribblingTurtle
    @AScribblingTurtle 7 หลายเดือนก่อน

    What I'd like to know is. what happens under the hood, when you use "forward".
    Does CSS just replay that last frame over and over again or does it only replay it, if one of the variables change.

  • @jonmayer
    @jonmayer 7 หลายเดือนก่อน

    Very cool trick.

  • @tristan_nolan
    @tristan_nolan 7 หลายเดือนก่อน

    Is it possible to avoid the glitchiness when briefly running the cursor across the corner of the box? The ball begins animating to the xy coordinates but doesn't complete it in time. So then when the animation completes it instantly jumps to those coordinates so that it can animate back to the zero position. I can't quick think of a way to make this possible.

  • @honestfxpredator7903
    @honestfxpredator7903 หลายเดือนก่อน

    Forwards, nice.

  • @treedruids5776
    @treedruids5776 7 หลายเดือนก่อน

    sooo satisfying

  • @Dakingsworld
    @Dakingsworld 7 หลายเดือนก่อน

    Amazing ❤❤

  • @aamiramin6112
    @aamiramin6112 7 หลายเดือนก่อน

    Kevin why the coordinates of X and Y is change (its not working like its when I remove display:grid? I want to know why its giving correct axis only in display:flex and display:grid?

  • @onkelhoy1
    @onkelhoy1 7 หลายเดือนก่อน

    Wouldn’t “backwards” work? I mean you demonstrated forwards and compared it with reverse but they are not the same, forwards is fill mode and reverse is direction. I think even reverse with forwards could work or simply just backwards (full mode)

    • @onkelhoy1
      @onkelhoy1 7 หลายเดือนก่อน

      But I haven’t checked, just top of my head

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน

      If you don't change the name of the animation, it won't run again, sadly. Or well, I don't think so, that's off the top of my head too ☺️

  • @rrd_webmania
    @rrd_webmania 7 หลายเดือนก่อน

    I just did something similar a few days ago!

  • @MaximilianoBernasconi
    @MaximilianoBernasconi 7 หลายเดือนก่อน

    i LOVE your shirt

  • @MichaelHitzelberger
    @MichaelHitzelberger 7 หลายเดือนก่อน

    There's only one type of situation that makes the dot jump: If you leave the square before the dot has reached the mouse pointer, the dot jumps to the edge of the square to then return to the middle via the animatíon.
    Would there be a CSS-only way of eliminating that effect? (I know it's a very small edge case :) )

  • @andreasnulein782
    @andreasnulein782 7 หลายเดือนก่อน

    There is no way to do it completely without JS right? I.e. css doesn't have a crazy hidden mouse pointer var right?

  • @krateskim4169
    @krateskim4169 7 หลายเดือนก่อน

    you are A GEM

  • @aaronlink127
    @aaronlink127 7 หลายเดือนก่อน

    I tried my hand at this and ended up with a totally different solution. I basically made an intermediate "cursor influence" property that I then can transition. But, it needs additional math, plus @property.
    @property --cursor-influence {
    syntax: "";
    inherits: true;
    initial-value: 0;
    }
    .hover-box {
    transition: --cursor-influence 500ms;
    &:hover {
    --cursor-influence: 1;
    }
    }
    .hover-box::before {
    translate: calc((var(--x) * var(--cursor-influence))) calc((var(--y) * var(--cursor-influence)));;
    }

  • @utjok
    @utjok 7 หลายเดือนก่อน

    thank you

  • @sthanuchowdhury8440
    @sthanuchowdhury8440 7 หลายเดือนก่อน

    Sir, i am a beginner in Css and html, i am only 40-50 days in css and html after my boards exams.
    So i am having trouble with Css Focus selector, i have tried searching about this topic distinctively on youtube, but only your guide on Css focus, i have got.
    So i request you to make a full video on Css focus selector with its real life application to animations and stuffs like that.

  • @DennisHoskins
    @DennisHoskins 7 หลายเดือนก่อน

    Why use :before for the animation instead of the element itself? Is there a reason or was this an implementation thing?

    • @asappi1
      @asappi1 7 หลายเดือนก่อน

      I was wondering about this as well.

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน

      In this case, the element is a div with the blue border on it, and the red thing in the middle is the pseudo-element that I created with ::before, so the animation in on the thing I'm animating :)

  • @ptolemny
    @ptolemny 7 หลายเดือนก่อน

    I'm new to HTML/CSS, so this might be a dumb question, but I'm wondering if there is a way to make it so that the animated element will follow the mouse, but only within a certain radius, say 50px or something like that, and once the mouse pointer moves outside the radius from where it "picked up" the animated element, the element will then return to its starting position? Hopefully I explained that correctly. Thanks!

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน

      You'd probably have to do most, if not all of that, through JS.... So yes, it's possible, but with CSS, :hover is :hover, so it's either on or off and we can't have thresholds like you're describing. We can use clamp() to prevent it from "escaping" beyond a certain point, but it won't reset when the mouse goes beyond that point without the help of more JavaScript (here's an example of using clamp() - codepen.io/kevinpowell/pen/oNOVjgE )

    • @ptolemny
      @ptolemny 7 หลายเดือนก่อน

      @@KevinPowell Thanks for the reply. I'm trying to do a parallax effect-type thing, but not using scrolling. Anyways, your videos are very helpful and I just realized I wasn't subbed, so I corrected that. Cheers!

  • @aamiramin6112
    @aamiramin6112 7 หลายเดือนก่อน

    Kevin, when I remove the display grid it's not getting the correct coordinates why? I mean only display:flex; or display:grid is working I want to know why? Why not the default display:block works?

    • @KevinPowell
      @KevinPowell  7 หลายเดือนก่อน

      It's how I set it up, since I've got it to be working from the center. If you don't have it centered (which it won't be if you take off grid), then you probably have to update the JS to remove the /2 stuff

    • @aamiramin6112
      @aamiramin6112 7 หลายเดือนก่อน

      @@KevinPowell Its still not working.
      I removed the display:grid; and in js remove the /2; Its like:
      const centerX = hoverBox.offsetWidth;
      const centerY = hoverBox.offsetHeight;
      and css:
      .hover-box {
      /* display: grid; */
      width: min(600px, 70%);
      margin: auto;
      aspect-ratio: 1;
      border: 3px solid hsl(200, 100%, 50%);
      border-radius: 0.5rem;
      box-shadow: 0 0 0.5rem hsl(200, 100%, 70%),
      inset 0 0 0.5rem hsl(200, 100%, 70%);
      overflow: hidden;
      }

    • @aamiramin6112
      @aamiramin6112 7 หลายเดือนก่อน

      @@KevinPowell I'm struggling to find out the reasons why its working for only grid and flex? like whats the difference between in calculation if we use flex or grid and if we use block there is probably a difference in calculating the offsetWidth or offsetX

  • @InYourFace7861
    @InYourFace7861 6 หลายเดือนก่อน

    I'm probably missing something obvious here, but can't you just constantly have translate: var(--x) var(--y); and then set --x and --y to 0 when the element isn't hovered? As you're already using js to update the vars

  • @smolboye1878
    @smolboye1878 7 หลายเดือนก่อน

    .animate() and fill: forwards makes this easy

  • @vivekv4250
    @vivekv4250 7 หลายเดือนก่อน

    Great .. very Cool

  • @DWEEB-FIX
    @DWEEB-FIX 7 หลายเดือนก่อน

    At the risk of sounding lamen, what software are you using for coding Kevin. I don't recognise that terminal

    • @kiliknp5141
      @kiliknp5141 6 หลายเดือนก่อน

      Its on codepen

  • @SXsoft99
    @SXsoft99 7 หลายเดือนก่อน

    very nice

  • @ogbuleo2048
    @ogbuleo2048 7 หลายเดือนก่อน

    I learned this in a meta front-end professional course

    • @ogbuleo2048
      @ogbuleo2048 7 หลายเดือนก่อน

      Especially in the html & css indebt , thanks for refreshing my memories

  • @kacCid
    @kacCid 7 หลายเดือนก่อน

    I gave this a Power Like by pressing my mouse button really hard while clicking the Like button.

  • @sheldonfourie5959
    @sheldonfourie5959 7 หลายเดือนก่อน +1

    Wow

  • @anandv1391
    @anandv1391 7 หลายเดือนก่อน

    Cool

  • @nocluebruh3792
    @nocluebruh3792 7 หลายเดือนก่อน

    whoa:0

  • @ianantonius7287
    @ianantonius7287 7 หลายเดือนก่อน +1

    what is your greeting at the beginning of the videos, I can't understand the words, "hi there my friend and friends?" It is beginning to get on my nerves. Please help me 😮

    • @seanthesheep
      @seanthesheep 7 หลายเดือนก่อน +1

      Frontend friends

    • @ianantonius7287
      @ianantonius7287 7 หลายเดือนก่อน

      @@seanthesheep Thank you so much!!!

  • @aspirine17
    @aspirine17 7 หลายเดือนก่อน

    lol, css has signals when all around only trying to implement them

  • @faiyazrasul2050
    @faiyazrasul2050 7 หลายเดือนก่อน

    cAn i learn cSS In 1 month can you give me a PlAylsit

    • @RabahTaib-mn4fs
      @RabahTaib-mn4fs 7 หลายเดือนก่อน

      That playlist doesn't exist.
      You learn by doing, and every day you'll learn something new.

  • @Dyanosis
    @Dyanosis 7 หลายเดือนก่อน +2

    I'm curious - what did your face add to this video? Why is any meaningful fraction of the video?

    • @Tikolu
      @Tikolu 7 หลายเดือนก่อน

      I agree, I don't understand what the point is

    • @aesthet1k_
      @aesthet1k_ 7 หลายเดือนก่อน

      Weird thing to say.

  • @TommyLikeTom
    @TommyLikeTom 7 หลายเดือนก่อน

    the moment you said java-script I lost interest. Obviously with java-script you can do anything

  • @FatheringLessons
    @FatheringLessons 7 หลายเดือนก่อน

    Please don't use hyphens and underscores in names. It's ugly, confusing for noobs, and just bad style. I recommend just going PascalCase for everything. Cool trick though.

    • @miguelcabaero5843
      @miguelcabaero5843 7 หลายเดือนก่อน +1

      PascalCase is usually for class names thats why underscores and dashes are used

    • @miguelcabaero5843
      @miguelcabaero5843 7 หลายเดือนก่อน

      Either way it doesnt really matter

    • @FatheringLessons
      @FatheringLessons 7 หลายเดือนก่อน

      @@miguelcabaero5843 It's best to use the same convention for all names. Rather than:
      thisForOneThing
      ThisForAnother
      _thenThis
      _AndThis
      _or-even-this
      and-this-too
      Just messy and ridiculous.

  • @mohammadalam9936
    @mohammadalam9936 7 หลายเดือนก่อน

    Man, i am just learning css for now.
    Not easy.
    Next will learn js.
    How you gonna learn all these advance stuff?
    It is overwhelming.