Add a Scroll-to-top button to the bottom of your Backlight pages

Do you have any really long pages on your site? Like big albums? Want to provide a way for your visitors to quickly get back to the top?

Here’s how to add a Back-to-Top button to the bottom of your pages. How it works is when the viewer scrolls down the page a certain amount, then the Back-to-Top button appears in the lower right of the browser.

The code below is slightly modified from code posted by Monte Trumbell in the CE4 PHPlugins forum. The only changes from Monte’s code is in using a Backlight phplugins hook and adding some css so that the button will actually show up on a Backlight page. So a big thanks to Monte Trumbull.

If you’re not familiar with using and enabling phplugins or custom css, see the TTG Documentation: Phplugins, custom css. And here’s my own post on using custom css in Backlight.

When you copy the code below, be sure to paste it into a plain text editor. If you use a word processor, the formatting for things like quote marks will change and the code won’t work.

If you’re already using the ttg_script hook in a phplugins file, then add this to it:

NOTE: the phplugins functions have changed since Backlight 2. First start by using the phplugins-pangolin-sample.php file from the latest Backlight-2-Installer-xxx folder you received. Next, the ttg_ portion of the function name is no longer needed. Nor is ($style, $path), use () instead. See the sample file for the entire list of hooks.

NOTE 2: In Backlight 5, the Kookaburra modules do not support jQuery so none of the code in this post will work. See this post in the forum for an alternative.

<a href="#top" class="btn-scroll-to-top" title="Top of page"><i class="fa fa-arrow-up fa-lg"></i></a>

<script>
$(document).ready(function() {
$(".btn-scroll-to-top").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$(".btn-scroll-to-top").fadeIn();
} else {
$(".btn-scroll-to-top").fadeOut();
}
});
});
</script>

The first part of the code above is the html to create the button (the css below will place the button on the page and style it). The first part of script monitors the button class (.btn-scroll-to-top) and if it’s clicked, then the page scrolls to the top. The next part of the script monitors the page for scrolling. When the viewer scrolls a certain amount from the top (200 in this case), then the button fades in to view. If less than 200, it fades out.

If you’re not yet using the script hook, here’s the full function. Add this in your phplugins file:

NOTE: as noted above, there is a change in Backlight 2. In Backlight 2 and later, this function begins with function scripts() {

Before Backlight 2

function ttg_scripts( $style, $path ) {
echo '
<a href="#top" class="btn-scroll-to-top" title="Top of page"><i class="fa fa-arrow-up fa-lg"></i></a>

<script>
$(document).ready(function() {
$(".btn-scroll-to-top").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$(".btn-scroll-to-top").fadeIn();
} else {
$(".btn-scroll-to-top").fadeOut();
}
});
});
</script>

';

}

For Backlight 2 and later:

function scripts() {
echo '
<a href="#top" class="btn-scroll-to-top" title="Top of page"><i class="fa fa-arrow-up fa-lg"></i></a>

<script>
$(document).ready(function() {
$(".btn-scroll-to-top").click(function() {
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
$(window).scroll(function() {
if ($(this).scrollTop() > 200) {
$(".btn-scroll-to-top").fadeIn();
} else {
$(".btn-scroll-to-top").fadeOut();
}
});
});
</script>

';

}

Here’s the CSS for the button. Add this to your custom css file. This code sets the position of the button along with its width, height and other attributes. Change these to your liking.

/* Scroll to top button styling
==================================================*/
.btn-scroll-to-top {
    background-color: transparent;
    display: none;
    position: fixed;
    bottom: 5px;
    right: 5px;
    border: 1px solid;
    height: 32px;
    width: 32px;
    border-radius: 50%; 
    text-align: center;
    line-height: 29px;
    vertical-align: middle;
    text-decoration: none;
    color:#6f7c80;
	
}
.btn-scroll-to-top:hover {
    text-decoration: none;
    color:#000000;
}

a.btn-scroll-to-top {
	z-index: 3;
}

 

1 thought on “Add a Scroll-to-top button to the bottom of your Backlight pages”

Leave a 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.

About this site

This site is: a repository of time-savers and best practices from a user's perspective, picked up from several years of using TTG plugins.
This site is not: a support site.
Read more here.

Subscribe to new posts

Tip Jar

If this site has saved you some time, kept your hair attached, or otherwise prevented you from tossing the mouse through the monitor, feel free to donate.

Categories


Highly recommended. This is my go-to Lightroom book. Click on the book image. (affiliate link)

Affiliate link!

Current TTG versions

Backlight  4 & 5

See the Backlight Modules page on your site for latest Backlight and module versions and changelogs.

You’ll also find the latest version number and download link for the Lightroom Publisher plugin.
(BL 3 and later)

 

Backlight 1 Versions

Backlight 1.2.4
Pages module 1.2.4
Cart Add-on 4.1.7
Client Response Add-on 7.1.3
Theater Add-on 1.2.6
Galleria Add-on 1.0.0
WordPress Add-on 1.2.6
Publisher 3.2.3

Backlight 1 has seen its end of life. See this post.

CE4 Versions

note: CE4 is no longer being developed or sold. See this post.

TTG-BE: 2.0.5a
Autoindex: 7.0.8
Cart: 3.1.4a
CRG: 6.1.3a
Gallery: 6.1.10
Pages: 7.0.15
Publisher: 2.3.3
Stage: 6.1.6
Theme for WordPress: 3.1.2