Basic Alpine Usage
by andyjh07
HTML
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<header x-data="{ mobileMenuOpen: false }"> <!-- Set the initial variable value -->
<nav class="transition-colors p-4" :class="mobileMenuOpen == true ? 'bg-purple-500' : 'bg-amber-500'"> <!-- :class attribute is a ternary conditional that if the mobileMenuOpen variable is true, set the background to be purple, else set it to be amber -->
<p>This is a nav bar</p>
</nav>
<button class="p-4 bg-slate-400 text-black rounded-full" x-on:click="mobileMenuOpen = !mobileMenuOpen">Toggle Nav Class</button> <!-- using x-on:click here we can set the variable to be the opposite of it's current setting -->
</header>