Scroll top, with animate button
by Imri Paloja
HTML
<a id="goTop">^</a>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
<p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
CSS
#goTop {
border: 1px solid #CCCCCC;
background-color: #EEEEEE;
color: #454545;
padding: 3px 5px;
position: fixed;
cursor: pointer;
top: -100px;
right: 10%;
transition: color 500ms ease 0s;
}
#goTop:hover {
background-color: #CCCCCC;
}
JavaScript
$(document).ready(function() {
$(window).scroll(function() {
if($(this).scrollTop() > 100){
$('#goTop').stop().animate({
top: '15%'
}, 750);
}
else{
$('#goTop').stop().animate({
top: '-100px'
}, 750);
}
});
$('#goTop').click(function() {
$('html, body').stop().animate({
scrollTop: 0
}, 500, function() {
$('#goTop').stop().animate({
top: '-100px'
}, 500);
});
});
});