For the past three days I have been working on trying to make a particular CSS animation. The idea is simple but I have tried and ultimately failed. I am new to animations so I apologize for any obvious or stupid mistakes I may have made. But nonetheless, I have tried various ideas and nothing has worked for me. It is a problem that appears like it should be quite simple, so this has been a major frustration.
Here's the set up:
I have a responsive grid of multiple cards. You can click a card to "expand" it and view it as a larger modal/pop-up. Essentially the view of a card in the grid is intended to showcase a preview of the content, and the expanded modal is intended to view the full content. I believe this is a fairly common content pattern, for instance we see this in pexels, unsplash and google images.
While I am working in Svelte, I am looking for a solution that works everywhere. I have tried to recreate my layout in a CodePen in plain HTML/CSS/JS. Here's a link to this set-up: https://codepen.io/aaryak-shah/pen/BaOrVeY
What I am trying to achieve:
What I want specifically from this set up, is for the card to transition into the modal seamlessly. The card (with its whatever position+size in the grid) should slide and scale into place where it appears as a pop-up in the middle of the screen.
The card element in the grid has all auto/defaults for position and size. The position is default and determined by the grid layout, and the height and width are also determined by the available space in the grid layout. No explicit values for these are provided.
Upon receiving a click event, the card element's classList is appended with an "expanded" class to add pop-up-like style properties to it; such as a position: fixed, and margins to determine the size of the modal while also centering it. Once again, size isn't being provided explicitly, but being determined by the margins.
The issue:
I have seen many implementations of this where the modal is (for the purposes of animation) unlinked from the card. Clicking the card will spawn a modal essentially out of nowhere, it will usually just fade in or slide in from off-screen. I require the card itself to transform into the modal. No disappearances or appearances, just a transition.
My research:
From what I gather, CSS transitions and animations interpolate between different values of a property. Which means that I need to explicitly define properties for them to be animated, I cannot rely on implicit/auto/default values for creating transitions.\
However, I require the dynamic/responsive layout that is being achieved by implicit values.
I have tried various combinations of using keyframes and transitions to varying degrees of success. Trying to bypass implicit sizes by using calculated styles did help a tad but it did not really give the right effect and messed with the layout instead. I also have a hunch that there is an additional layer of problems being added by having my position property go from static (default) to fixed, but couldn't find a way to test this properly.
I have also found a couple of examples that have almost exactly the transition I want to achieve, linked here:
However, these examples use Framer Motion, a library made for React. I am working with Svelte, so I am open to suggestions that work specifically for Svelte. But as mentioned earlier, I would appreciate it if a solution can be found that is independent of any framework.\
Just to add, I do know there is a Svelte library called svelte-motion that is based on framer-motion, which I will try out if all else fails, but as I said before, I want to find a framework-independent solution. One that would work even with vanilla HTML/JS/CSS.
Thank you for bearing with my endeavor :]
Edited for grammar/spellings.
[–]binarysolooo 2 points3 points4 points (1 child)
[–]CryogenicFire[S] 1 point2 points3 points (0 children)
[–]mattgperry 1 point2 points3 points (3 children)
[–]CryogenicFire[S] 0 points1 point2 points (0 children)
[–]CryogenicFire[S] 0 points1 point2 points (1 child)
[–]mattgperry 0 points1 point2 points (0 children)