• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

27 October - 2014 By Paal Joachim 4 Comments
Last updated on: November 10, 2016

A Back to top link

Having a back to top link is useful for longer pages.

I did a search and came across this code at https://wordpress.org/support/topic/adding-back-to-top-button-1?replies=14  One can just place the code into the child theme functions file and it should work just fine.

 

/* Back To Top */

    add_action( 'wp_footer', 'back_to_top' );
    function back_to_top() {
    echo '<a id="totop" href="#">Back to Top</a>';
    }

    add_action( 'wp_head', 'back_to_top_style' );
    function back_to_top_style() {
    echo '<style type="text/css">
    #totop {
    position: fixed;
    right: 30px;
    bottom: 30px;
    display: none;
    outline: none;
    }
    </style>';
    }

    add_action( 'wp_footer', 'back_to_top_script' );
    function back_to_top_script() {
    echo '<script type="text/javascript">
    jQuery(document).ready(function($){
    $(window).scroll(function () {
    if ( $(this).scrollTop() > 500 )
    $("#totop").fadeIn();
    else
    $("#totop").fadeOut();
    });

    $("#totop").click(function () {
    $("body,html").animate({ scrollTop: 0 }, 800 );
    return false;
    });
    });
    </script>';
    }

 

I went ahead and added some modifications.
The result is that the button looks like this.

 

back to top button

 

On hover it looks like this:

Back to top button hover

 

The code I added to the functions file:

The black arrow is actually a Mac symbol I added through the Show Emoji & Symbols utility.
Hopefully it will also work on other computers…

/* Back To Top */

add_action( 'wp_footer', 'back_to_top' );
 function back_to_top() {
 echo '<a id="totop" href="#" data-btn-alt="Top">⬆︎</a>';
 }

add_action( 'wp_head', 'back_to_top_style' );
 function back_to_top_style() {
 echo '<style type="text/css">
 #totop {
 position: fixed;
 right: 30px;
 bottom: 30px;
 display: none;
 outline: none;
 text-decoration: none;
 font-size: 26px;
 background: rgba(42, 64, 67, 0.4);
 padding: 3px 12px 3px 12px;
 border-radius: 5px;
 box-shadow: 0 0 1px #000;
 color: #fff;
 z-index: 100;
 }
 
 #totop:hover {
 background: rgba(42, 64, 67, 1);
 }
 
 #totop:hover:after{
 content: attr(data-btn-alt);
 font-size: 16px;
 color: #fff;
 padding-left: 5px;
 }
 </style>';
 
 }

add_action( 'wp_footer', 'back_to_top_script' );
 function back_to_top_script() {
 echo '<script type="text/javascript">
 jQuery(document).ready(function($){
 $(window).scroll(function () {
 if ( $(this).scrollTop() > 1500 ) 
 $("#totop").fadeIn();
 else
 $("#totop").fadeOut();
 });

$("#totop").click(function () {
 $("body,html").animate({ scrollTop: 0 }, 800 );
 return false;
 });
 });
 </script>';
 }

 

To adjust when the button shows up during scroll adjust the scrollTop 1500 to another number.
To adjust the speed from clicking the button to when the page arrives at the top adjust the 800 number.
To adjust the size of the box adjust the padding (top, right, bottom, left) 3px 12px 3px 12px.
To adjust the opacity change the background: rgba(42, 64, 67, 0.4)  –   0.4 value.

Resources:
https://wordpress.org/support/topic/adding-back-to-top-button-1?replies=14
https://getflywheel.com/layout/add-sticky-back-top-button-website/
https://www.webtipblog.com/adding-scroll-top-button-website/
.https://www.codecheese.com/2013/11/create-back-to-top-button-for-wordpress/

Back to top plugins:
https://wordpress.org/plugins/scroll-back-to-top/
https://wordpress.org/plugins/scroll-to-top-button/

Share this:

  • Email

Categories: Developer, Genesis, How to, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. rabe. says

    21 January - 2017

    Hello,

    the last three days I have been searching for code “back-to-top” avoiding plugins. Currently I am involved in redesigning the web pages of a primary school.

    And luckily I found your post.

    It worked like charm straight ahead.

    The sole draw back for us is that the “data-btn-alt” brings up a mid-blue up-arrow. We are looking for something more fitting the color design of our new page.

    On a page linked by you there was an alternate code for the symbol: “echo ”; ” where the relative location is from our folder structure.

    Just changing the line ” echo ‘⬆‘; ” to the mentioned breaks the function and nothing can be seen on a monitor.

    There is obviously an other line of code to change also.

    Could you please give us a hint which changes we should make to reference a server local *.svg file.

    Man thanks

    rabe.

    Reply
    • Paal Joachim says

      24 January - 2017

      Hi Rabe

      I did a search for echo images php. A few links came up in Google. Play with it test out various options and let me know how it goes.
      I can then also update the post with your findings..:)

      Reply
  2. Debora says

    26 August - 2016

    Hi, many thanks for this page, it’s very useful.
    I’ve just added this script and the button works. I’d like to know if it’s possible to show this button not just at the end on the page but also in the other stages of scrolling.

    Many thanks
    Debora

    Reply
    • Paal Joachim says

      26 August - 2016

      Hi Debora

      Thanks for asking! I added a few more notes to the tutorial.
      To adjust where the button shows up adjust the number:
      if ( $(this).scrollTop() > 1500 )
      to something else.

      Have a great day!

      Reply

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2022 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪