<div id="top_link" class="top_link"><a href="#top">Подняться вверх</a></div>
/* set variables locally for increased performance */
var scroll_timer;
var displayed = false;
var $top_link = $('#top_link');
var $window = $(window);
var top = $(document.body).children(0).position().top;
/* react to scroll event on window */
$window.scroll(function()
{
window.clearTimeout(scroll_timer);
scroll_timer = window.setTimeout(function () { // use a timer for performance
if($window.scrollTop() <= top) // hide if at the top of the page
{
displayed = false;
$top_link.fadeOut(500);
}
else if(displayed == false) // show if scrolling down
{
displayed = true;
$top_link.stop(true, true).fadeIn(500);
}
}, 100);
});
$top_link.find('a').click(function(e)
{
e.preventDefault();
$(this).parent().fadeOut(500);
$("html:not(:animated),body:not(:animated)").animate({ scrollTop : 0 }, 300);
});