Загрузка видео...

Не удалось загрузить видео

На главную

Love messin' with these UI patterns 🤙 li:hover { --active: 1; } li img { position: fixed; left: calc(var(--x) * 1px); top: calc(var(--y) * 1px); opacity: var(--active, 0); } Pass the --x/y in on pointermove for the list ⭐️

57,853 просмотров • 2 лет назад •via X (Twitter)

Комментарии: 0

Нет доступных комментариев

Здесь появятся комментарии из оригинального поста

Похожие видео

UI && CSS Tip! 🔥 You can create these dynamic glowing card effects with a single JavaScript event listener, some CSS tricks, and an HTML attribute 🤙 Here's how! 👇 You can use a combination of tricks from previous posts. Let's start with the JavaScript 📜 Use a function like this to send the pointer position via custom properties to the glows ✨ Attach this to the pointermove event 👆 const onPointermove = ({ x, y }) => { document​.documentElemen​t​.style.setProperty('--x', x) document.documentElement​.style.setProperty('--xp', x / innerWidth) document.documentElement​.style.setProperty('--y', y) document.documentElement​.style.setProperty('--yp', y / innerHeight) } Now for the styles 🎨 These custom properties define the border and spotlight size. The first trick is how you can change the color(--hue) based on the pointer position with calc 😎 [data-glow] { --border-size: 2px; --spotlight-size: 150px; --hue: calc(var(--base) + (var(--xp) * var(--spread))); } The magic is using the pointer position in a radial-gradient combined with background-attachment: fixed ✨ (Check the video for an exploding view 🎞️) [data-glow] { background-image: radial-gradient( var(--spotlight-size) var(--spotlight-size) at calc(var(--x, 0) * 1px) calc(var(--y, 0) * 1px), hsl(var(--hue) 100% 70% / 0.25), transparent ); background-attachment: fixed; } The last major piece is using mask-composite on the pseudoelements of the [data-glow] element and the same background-attachment: fixed trick 🫶 [data-glow]::before, [data-glow]::after { pointer-events: none; content: ""; position: absolute; inset: calc(var(--border-size) * -1); border: var(--border-size) solid transparent; background-attachment: fixed; background-size: calc(100% + (2 * var(--border-size))); background-position: 50% 50%; mask: linear-gradient(transparent, transparent), linear-gradient(white, white); mask-clip: padding-box, border-box; mask-composite: intersect; } This masks the elements so only a fake border is shown. That border is actually the spotlight background that moves around 👀 But now it's clipped to the border width 😎 Then it's a case of switching out the background-image for each pseudoelement based on purpose 🧐 For example, to get the white light, use your custom properties, make the light a little smaller and the color bright! [data-glow]::after { background-image: radial-gradient( calc(var(--spotlight-size) * 0.5) calc(var(--spotlight-size) * 0.5) at calc(var(--x, 0) * 1px) calc(var(--y, 0) * 1px), hsl(0 100% 100% / 1), transparent ); } These are the major pieces you need to get it done. Extras? Want the outer glow? Nest a glow! Give the inner glow a blur to create the outer glow 😅 [data-glow] > [data-glow] { filter: blur(10px); } It's worth digging in with this one. Have a play around with the demo and the custom properties. The code isn't perfect after introducing all the configuration pieces 😅 Will put together a reduced example for you! 🙏 Any questions, let me know! 💙 CodePen.IO link below! 👇

jhey ʕ•ᴥ•ʔ

382,535 просмотров • 2 лет назад

CSS Trick 🧲 You can create magnetic links with the power of custom properties and some JavaScript 💪 a { translate: calc(clamp(-1, var(--x), 1) * var(--pad-x)) ...; transition: translate var(--s, 1s) var(--ease, var(--elastic)); } a:hover { --s: 0s; } The trick here is to pad out the list items wrapping your links and use that as a translation limit 🛑 Start by using some JavaScript to calculate a value between -1 and 1 for both the x/y axis on pointermove for each list item, not the link! 🔗 If your pointer was at the center of the item, you'd get [0,0]. If it was in the top right, you'd get [1,-1] ☝️ It's worth checking out the JavaScript snippet to see how the mapping function works. Essentially, you create a function that when given a value between two bounds, will give you a mapped value back 🤙 const mapX = mapRange( item.offsetWidth * -0.5, item.offsetWidth * 0.5, 1, -1 ) Then, on pointermove, you plug the pointer position in to get the value back out and pass that into your CSS const x = mapX(item.centerX - event.x) document​.documentElement​.style.setProperty(--x, x) When the pointer leaves the list item, you make sure to reset these values back to 0 ✨ Once CSS has your values, it's the trick of updating the translation of each part You know that in each axis, you only want to translate the link by the padding amount li a { translate: calc(clamp(-1, var(--x), 1) * var(--pad-x)) calc(clamp(-1, var(--y), 1) * var(--pad-y)); transition: translate var(--speed, 1s) var(--ease, var(--elastic)); } This will translate the link within the list item by the desired amount. The cool part here is that you can set an offset for the text inside the link and have that move at a different rate ⭐️ By only updating the --pad-x/y custom properties for the inside the link, you can control how much it moves nav a span { --pad-x: 0.25rem; --pad-y: 0.25rem; } And the last piece, how do you update the behavior for transition speeds? And so it springs back like that? Again, use custom properties ✨ a:hover { --s: 0s; } a { transition: translate var(--s, 1s) var(--ease, var(--elastic)); } By default, a link will use --elastic easing via linear() and have a transition-duration of 1s. When a link is hovered that speed becomes 0s because you want the link to magnetise to your pointer. How about that little gap between when your pointer enters the item but hasn't hovered the link? Set a different transition so it transitions to being hovered 🫶 nav li:hover a { --ease: ease-out; --speed: 0.1s; } That's kinda it! 🙌 Use JavaScript (~40 loc) to get the information and then let CSS do all the lifting for you 💪 Any questions or suggestions, let me know 🙏 If you want a walkthrough video, also let me know please 🙏 CodePen.IO link below 👇

jhey ʕ•ᴥ•ʔ

164,863 просмотров • 2 лет назад