Animating a background moving between grid elements

I made this simple switch but I wanna animate the background moving from left to right instead of having it disappear and reappear. Right now the background is just a css class applied when the item is active, but I was thinking of creating a grid item that overlaps the other two with the same width to achieve this. Couldn't get it working though, any tips on how I can do this? This is my current component
<script lang="ts" setup>
defineProps<{
first: string
second: string
}>()
const enabled = ref(false)

function toggle() {
enabled.value = !enabled.value
}
</script>

<template>
<Switch
v-model="enabled"
class="relative grid grid-cols-2 bg-grey-900 gap-2 border-box-border rounded-full border p-1 cursor-pointer"
@click="toggle"
>
<span
class="select-none text-center rounded-full px-2 py-1 text-xs transition-all"
:class="enabled ? 'bg-purple-500' : ''"
>{{ first }}</span>
<span
class="select-none text-center rounded-full px-2 py-1 text-xs transition-all"
:class="!enabled ? 'bg-purple-500' : ''"
>{{ second }}</span>
</Switch>
</template>
<script lang="ts" setup>
defineProps<{
first: string
second: string
}>()
const enabled = ref(false)

function toggle() {
enabled.value = !enabled.value
}
</script>

<template>
<Switch
v-model="enabled"
class="relative grid grid-cols-2 bg-grey-900 gap-2 border-box-border rounded-full border p-1 cursor-pointer"
@click="toggle"
>
<span
class="select-none text-center rounded-full px-2 py-1 text-xs transition-all"
:class="enabled ? 'bg-purple-500' : ''"
>{{ first }}</span>
<span
class="select-none text-center rounded-full px-2 py-1 text-xs transition-all"
:class="!enabled ? 'bg-purple-500' : ''"
>{{ second }}</span>
</Switch>
</template>
G
Giuliopime13d ago
Basically something like this but for the background
M
Mike13d ago
I don't know that you need to use grid for this (the example below doesn't use grid).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #1a1a1a;
}
button{
color: rgba(255,255,255, 0.7);
border: 2px solid rgba(255,255,255, 0.7);
background-color: transparent;
width: 100px;
height: 50px;
font-size: 20px;
border-radius: 16px;
cursor: pointer;
z-index: 1;
}
.slider{
position: absolute;
width: 100px;
height: 50px;
background-color: #d946ef;
transition: 0.15s ease-in-out;
left: 0px;
border-radius: 16px;
}
.wrapper{
border: white;
display: inline-flex;
position: relative;
gap: 5px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="slider"></div>
<button>
one
</button>
<button>
two
</button>
<button>
three
</button>
</div>
<script>
const buttons = document.querySelectorAll('button');
const slider = document.querySelector('.slider');
buttons.forEach((button, index) => {
button.addEventListener('click', () => {
slider.style.left = `${(index * 105)}px`;
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #1a1a1a;
}
button{
color: rgba(255,255,255, 0.7);
border: 2px solid rgba(255,255,255, 0.7);
background-color: transparent;
width: 100px;
height: 50px;
font-size: 20px;
border-radius: 16px;
cursor: pointer;
z-index: 1;
}
.slider{
position: absolute;
width: 100px;
height: 50px;
background-color: #d946ef;
transition: 0.15s ease-in-out;
left: 0px;
border-radius: 16px;
}
.wrapper{
border: white;
display: inline-flex;
position: relative;
gap: 5px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="slider"></div>
<button>
one
</button>
<button>
two
</button>
<button>
three
</button>
</div>
<script>
const buttons = document.querySelectorAll('button');
const slider = document.querySelector('.slider');
buttons.forEach((button, index) => {
button.addEventListener('click', () => {
slider.style.left = `${(index * 105)}px`;
});
});
</script>
</body>
</html>
No description
M
Mike13d ago
hopefully that's helpful 😄
G
Giuliopime12d ago
that's fixed width unfortunately but thank you
M
Mike12d ago
yep, it's fixed width right now, but can you think of any way to make this work for dynamic width buttons?
Y
?.@y.wwa11d ago
Manu Arora
Navbar Hover - Code Snippet
A collection of code snippets – including serverless functions, Node.js scripts, and CSS tricks.
Want results from more Discord servers?
Add your server
More Posts
Is Theo's new tutorial any good for a Vue Dev?Truth be told I have never touched React or any .tsx before. However I have been doing vue for the lEmail exist verificationdoes anyone knows a way to verify if an email exists? like is there such a package or services with Issues with Clerk and Unit testingI am working on a project for work and we have a strict requirement for unit tests. I decided to go Drizzle + SQLite updated at fieldIs the only way to have an updated at field in SQLite just setting the updated at by hand on each upHow can I replicate the vanilla behavior of NextJS API routing in T3 stack?I am currently working on a project in which i would like to have my server side requests to an endpWhat is this app?this works but nope?I'm new to Suspense, this works great but I get the error: Did you forget to use 'await'? It works,Micro Front End best practicesHello! I recently landed a job within a stack centered around micro front ends (react + node), whichUsing Turso in T3-CreateHi, I am trying to use Turso for T3. I am using drizzle. The connection .env is slightly different No file route found for slug in deploymentThis issue arose when I started my deployment on Render , which simply read as No file route found Will server components be refeteched on mutation?If I have a page using the server trpc and then a client component that updates the list, will the sNextjs parallel routing questionsI have this routing setup right, ```bash { (main) { about {} contact {} (illustrationsHow can I subscribe to updates in an RSS feed?I want to subscribe to changes on a shopify store’s RSS feed for their products. It seems extremely How can I configure tRPC to function as a REST API (single-route)?I'm new to tRPC and not sure why this produces an error when an external POST request is sent to theT3 Stack: tRPC not extending type definitions to clientHey, I'm currently trying to work on getting my 'next build' script to work and ran into a bunch of Total noob! Supabase and the t3 stackIm really struggling with configuring my create-t3-app with supabase. Can anyone point me in the dir