Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created May 28, 2021 19:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Braunson/e5f369b1c2a54cac609b0f0abb3cef64 to your computer and use it in GitHub Desktop.
Save Braunson/e5f369b1c2a54cac609b0f0abb3cef64 to your computer and use it in GitHub Desktop.
Alpine.js + Tailwind scroll to top snippet
<div x-data="{scrollBackTop: false}" x-cloak>
<button
x-show="scrollBackTop"
x-on:scroll.window="scrollBackTop = (window.pageYOffset > window.outerHeight * 0.5) ? true : false"
x-on:click="$scroll('#top')"
aria-label="Back to top"
class="fixed bottom-0 right-0 p-2 mx-10 my-10 text-white bg-gray-800 hover:bg-gray-700 focus:outline-none">
<svg viewBox="0 0 20 20" fill="currentColor" class="w-6 h-6">
<path fill-rule="evenodd" d="M14.707 12.707A1 1 0 01-1.414 0L10 9.414l-3.293 3.293a1 1 0 01-1.414-1.414l4-4a1 1 0 011.414 0l4 4a1 1 0 010 1.414z" clip-rule="evenodd"></path>
</svg>
</button>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment