var SCROLL_SPEED = 400; var ready = function(callback) { if (document.readyState != "loading") callback(); else document.addEventListener("DOMContentLoaded", callback); } ready(function() { jQuery(window).scroll(function() { if (jQuery(this).scrollTop() > 50) { jQuery('#back-to-top').fadeIn(); } else { jQuery('#back-to-top').fadeOut(); } }); jQuery('#back-to-top').click(function() { jQuery('body,html').animate({ scrollTop: 0 }, SCROLL_SPEED); return false; }); });