Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Ian Schneider
Added: Feb 7, 2018 5:26 PM
Modified: Feb 8, 2018 4:50 PM
Views: 35
Tags: jquery scroll
Fade in when scrolled down, scroll page to top when clicked.
  1. $(document).ready(function() {
  2.     var offset = 250;
  3.     var duration = 300;
  4.     $(window).scroll(function() {
  5.         if ($(this).scrollTop() > offset) {
  6.             $('#back_to_top').fadeIn(duration);
  7.         } else {
  8.             $('#back_to_top').fadeOut(duration);
  9.         }
  10.     });
  11.     $('#back_to_top').click(function(event) {
  12.         event.preventDefault();
  13.         $('html, body').animate({
  14.             scrollTop: 0
  15.         }, duration);
  16.         return false;
  17.     });
  18. });