jQuery(document).ready(function($){

    

    if( $('body')){

        const body = document.querySelector('body');

        let btn = document.createElement("button");

        btn.className = "scrollToTopBtn";

        btn.id = "scrollToTopBtn";

        btn.innerHTML = '<i class="fa-solid fa-chevron-up">';

        body.appendChild(btn);

    }

    

    var scrollToTopBtn = document.querySelector(".scrollToTopBtn");

    var rootElement = document.documentElement;



    handleScroll()



    scrollToTopBtn.addEventListener("click", scrollToTop);

    document.addEventListener("scroll", handleScroll);

 

    function handleScroll() {

        // Do something on scroll

        var scrollTotal = rootElement.scrollHeight - rootElement.clientHeight;

        if (rootElement.scrollTop / scrollTotal > 0.2) {

            // Show button

            scrollToTopBtn.classList.add("showBtn");

        } else {

            // Hide button

            scrollToTopBtn.classList.remove("showBtn");

        }

    }

    

    function scrollToTop() {

    // Scroll to top logic

    rootElement.scrollTo({

        top: 0,

        behavior: "smooth"

    });

    }

});

