MyBB Tutorials

Scroll to Top

Submitted by linkzy, , Thread ID: 43

Thread Closed
linkzy
No pressure, no diamonds
Level:
0
Reputation:
181
Posts:
5.34K
Likes:
341
Credits:
54
15-01-2015, 07:26 AM
This post was last modified: 15-01-2015, 07:32 AM by linkzy
#1
There are huge and lengthy jQuery scroll to top scripts are available throughout the web. But today we will learn to make an appearing scroll to top button with minimal jQ and CSS.

Initially, the scroll to top button will be hidden but as soon as the user will scroll down, the button will appear.

Open your footer template and add the following code at the very beginning:


PHP Code:
<script type="text/javascript">
jQuery(function($) {
$(
"#backtop").hide();
  $(
window).scroll(function () {
    if ($(
this).scrollTop() > 400) {
      $(
'#backtop').fadeIn(200);
    } else {
      $(
'#backtop').fadeOut(200);
    }
  });
  $(
'.go-top').click(function () {
    $(
'html,body').animate({
      
scrollTop0
    
}, 1000);
    return 
false;
  });
});
</
script

and at the end before two close </div> tags:

PHP Code:
<div id='backtop'>
    <
class='go-top' href='#top' title='Back to Top'><img src="BUTTON IMAGE" /></a>
</
div
Remember to change the Button Image to your needs.

Save your footer template. Open global.css and add at the end:


PHP Code:
#backtop {
    
positionfixed;
    
bottom50px;
    
right10px;
    
margin0;
    
padding0;
    
width36px;
    
height36px;
    
z-index100;

Remember to include jQ library in MyBB if it is already not. In headerinclude:


PHP Code:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<
script type="text/javascript">
jQuery.noConflict();
</
script

Enjoy scrolling back to top ... :hurr:

Credits to Linkz
| A | v4hl| Addicted | Senpai | Sui | Sensei | H | fdigl |



Users browsing this thread: 1 Guest(s)