Last active
December 15, 2015 12:39
-
-
Save charleslouis/5261910 to your computer and use it in GitHub Desktop.
Scroll to top with a small div toggling fixed in bottom-right corner only when needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Scroll to top | |
*/ | |
function scroll_to_top(ID){ | |
if (document.getElementById(ID)){ | |
$('#'+ ID).append('<a href="#top" id="scrollToTop">haut de page</a>'); | |
$(window).scroll(function() { | |
if($(window).scrollTop() == 0){ | |
$('#scrollToTop').fadeOut("fast"); | |
} else { | |
$('#scrollToTop').fadeIn("fast"); | |
} | |
}); | |
$('a[href=#top]').click(function(e){ | |
$('html, body').animate({scrollTop:0}, 'slow'); | |
event.preventDefault(); | |
}); | |
} | |
}//end of scrolltop() | |
scroll_to_top('main'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Scroll to top CSS | |
*/ | |
#scrollToTop { | |
@include transition(all 0.5s ease-in-out); | |
position: relative; | |
display: block; | |
margin-top: 1em; | |
margin-left: 1em; | |
float: right; | |
text-align:center; | |
opacity: 0.6; | |
color: $gray; | |
&:before { | |
display: block; | |
float: left; | |
font-family: $iconFontFamily; | |
content: $top; | |
color: $red; | |
font-size: 3em; | |
margin-top: -0.2em; | |
margin-right: -0.2em; | |
} | |
&:hover{ | |
opacity: 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment