search results:

    • Standard
    • React
    Pricing Learn Community
    • + D
    • Light
    • Dark
    • System
    logo TW Elements
    • Getting started
      • Quick start
      • Dark mode
      • Theming
      • Changelog
    • CommercialNew
      • Pricing
      • License
      • Installation
      • Git & repository
      • Premium Support
    • Integrations
      • Next
    • Content & styles
      • Animations
      • Animations extended
      • Colors
      • Dividers
      • Figures
      • Headings
      • Hover effects
      • Icons
      • Images
      • Mask
      • Shadows
      • Typography
    • Navigation
      • Breadcrumbs
      • Footer
      • Pagination
      • Pills
      • Tabs
    • Components
      • Accordion
      • Alerts
      • Avatar
      • Badges
      • Button group
      • Buttons
      • Cards
      • Carousel
      • Collapse
      • Dropdown
      • Link
      • List group
      • Modal
      • Paragraphs
      • Placeholders
      • Popover
      • Progress
      • Rating
      • Scroll back to top button
      • Social buttons
      • Spinners
      • Timeline
      • Toasts
      • Tooltip
      • Video carousel
    • Forms
      • Checkbox
      • File input
      • Form templates
      • Inputs
      • Login form
      • Radio
      • Range
      • Registration form
      • Search
      • Select
      • Switch
      • Textarea
    • Data
      • Charts
      • Tables
    • Methods
      • Ripple
    • ResourcesNew
      • Playground
      • YouTube Channel
      • Private FB Group
      • Newsletter
      • UI Design course New

    Scroll back to top button

    Tailwind CSS React Scroll Back To Top Button

    Use responsive scroll back to top component with helper examples for scroll back to top button animation, appear after scroll & more. Open source license.


    Basic example

    Button to return to the top of the page allows the user to quickly return to the top of the page without making too much effort. This can be very useful when the page has a lot of content.

    Use the code below to create button that scrolls back to the beginning of the page.

    • JSX
            
                
          import React, { useState, useEffect } from "react";
          import { TERipple } from "tw-elements-react";
          
          export default function BasicExample(): JSX.Element {
            const [showButton, setShowButton] = useState(false);
          
            const handleScroll = () => {
              if (
                document.body.scrollTop > 20 ||
                document.documentElement.scrollTop > 20
              ) {
                setShowButton(true);
              } else {
                setShowButton(false);
              }
            };
          
            const backToTop = () => {
              document.documentElement.style.scrollBehavior = "smooth";
              document.body.scrollTop = 0;
              document.documentElement.scrollTop = 0;
            };
          
            useEffect(() => {
              window.addEventListener("scroll", handleScroll);
              return () => {
                window.removeEventListener("scroll", handleScroll);
              };
            }, []);
          
            return (
              <>
                {/* <!-- Explanation --> */}
                <div className="container mt-4 text-center text-gray-800">
                  <p className="mb-4">
                    Start scrolling the page and a strong{" "}
                    <strong>"Back to top" button </strong> will appear in the{" "}
                    <strong>bottom right corner</strong> of the screen.
                  </p>
                  <p>Click this button and you will be taken to the top of the page.</p>
                </div>
          
                {/* <!-- Back to top button --> */}
                {showButton && (
                  <TERipple rippleColor="light">
                    <button
                      type="button"
                      onClick={backToTop}
                      className={` ${
                        showButton ? `inline-block` : `hidden`
                      } fixed bottom-[40px] right-[40px] p-3 bg-red-600 text-white font-medium text-xs leading-tight uppercase rounded-full shadow-md hover:bg-red-700 hover:shadow-lg focus:bg-red-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-red-800 active:shadow-lg transition duration-150 ease-in-out`}
                    >
                      <svg
                        aria-hidden="true"
                        focusable="false"
                        data-prefix="fas"
                        className="w-4 h-4"
                        role="img"
                        xmlns="http://www.w3.org/2000/svg"
                        viewBox="0 0 448 512"
                      >
                        <path
                          fill="currentColor"
                          d="M34.9 289.5l-22.2-22.2c-9.4-9.4-9.4-24.6 0-33.9L207 39c9.4-9.4 24.6-9.4 33.9 0l194.3 194.3c9.4 9.4 9.4 24.6 0 33.9L413 289.4c-9.5 9.5-25 9.3-34.3-.4L264 168.6V456c0 13.3-10.7 24-24 24h-32c-13.3 0-24-10.7-24-24V168.6L69.2 289.1c-9.3 9.8-24.8 10-34.3.4z"
                        ></path>
                      </svg>
                    </button>
                  </TERipple>
                )}
              </>
            );
          }      
          
            
        

    Hey there 👋 we're excited about TW Elements for React and want to see it grow! If you enjoy it, help the project grow by sharing it with your peers. Every share counts, thank you!

    • Basic example
    Get useful tips & free resources directly to your inbox along with exclusive subscriber-only content.
    Join our mailing list now
    © 2023 Copyright: MDBootstrap.com