Skip to content

Instantly share code, notes, and snippets.

@rickklaasboer
Last active December 16, 2019 18:11
Show Gist options
  • Save rickklaasboer/2d396233e9098441421d510ec0feaa70 to your computer and use it in GitHub Desktop.
Save rickklaasboer/2d396233e9098441421d510ec0feaa70 to your computer and use it in GitHub Desktop.

Revisions

  1. rickklaasboer revised this gist Dec 16, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion scrolltotop.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Add to HTML files (recommended place is below the opening body tag):
    Add to HTML files (recommended place is below the opening `<body>` tag):

    ```html
    <button onclick="scrollToTop()" id="scrollToTop" style="display: none;" title="Go to top">
  2. rickklaasboer revised this gist Dec 16, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion scrolltotop.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Add to HTML files (recommended place is below the opening <body> tag):
    Add to HTML files (recommended place is below the opening body tag):

    ```html
    <button onclick="scrollToTop()" id="scrollToTop" style="display: none;" title="Go to top">
  3. rickklaasboer created this gist Dec 16, 2019.
    53 changes: 53 additions & 0 deletions scrolltotop.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    Add to HTML files (recommended place is below the opening <body> tag):

    ```html
    <button onclick="scrollToTop()" id="scrollToTop" style="display: none;" title="Go to top">
    <i class="fas fa-arrow-up"></i>
    </button>
    ```

    Add to app.css (doesn't really matter where):

    ```css
    #scrollToTop {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 30px;
    z-index: 99;
    border: none;
    outline: none;
    background-color: #00d1b2;
    color: white;
    cursor: pointer;
    padding: 15px;
    padding-right: 19px;
    padding-left: 19px;
    font-size: 18px;
    transition: all 0.2s ease-in-out;
    }

    #scrollToTop:hover {
    background-color: #00b89c;
    transition: all 0.2s ease-in;
    }
    ```

    Add to app.js (doesn't really matter where):

    ```js
    window.addEventListener('scroll', () => {
    const button = document.getElementById('scrollToTop');
    const scrollHeight = document.body.scrollTop || document.documentElement.scrollTop
    if (scrollHeight > 20) {
    button.style.display = "block";
    } else {
    button.style.display = "none";
    }
    });

    scrollToTop = () => {
    window.scrollTo({ top: 0, behavior: 'smooth' })
    }
    ```