Last active
May 7, 2016 08:31
-
-
Save khairulhasanmd/792df6646b54e6efc3c8b1168bd4ac98 to your computer and use it in GitHub Desktop.
Scroll to top and Scroll to bottom
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
<html> | |
<head> | |
<script> | |
$( document ).ready(function() { | |
$('#scrolltotop').fadeOut(); | |
$('#scrolltotop').on("click", function () { | |
var percentageToScroll = 100; | |
var percentage = percentageToScroll / 100; | |
var height = $(document).scrollTop(); | |
var scrollAmount = height * (1 - percentage); | |
console.log('scrollAmount: ' + scrollAmount); | |
$('html,body').animate({ | |
scrollTop: scrollAmount | |
}, 'slow', function () { | |
console.log("reached top"); | |
}); | |
}); | |
$('#scrolltobottom').on("click", function () { | |
var percentageToScroll = 100; | |
var percentage = percentageToScroll / 100; | |
var height = $(document).height() - $(window).height(); | |
var scrollAmount = height * percentage; | |
console.log('scrollAmount: ' + scrollAmount); | |
jQuery("html, body").animate({ | |
scrollTop: scrollAmount | |
}, 900); | |
}); | |
$(window).scroll(function () { | |
if ($(this).scrollTop() > 100) { | |
$('#scrolltotop').fadeIn(); | |
} else { | |
$('#scrolltotop').fadeOut(); | |
} | |
if ($(this).scrollTop()<($(document).height() - ($(window).height()+100))) { | |
$('#scrolltobottom').fadeIn(); | |
} else { | |
$('#scrolltobottom').fadeOut(); | |
} | |
}); | |
}); | |
</script> | |
<style> | |
#scrolltotop { | |
position: fixed; | |
top: 10px; | |
right: 0; | |
width: 40; | |
height: 40; | |
z-index: 2; | |
} | |
#scrolltobottom { | |
position: fixed; | |
bottom: 10px; | |
right: 0; | |
width: 40; | |
height: 40; | |
z-index: 3; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="scrolltobottom"><img src="<?php echo base_url();?>interface/img/scrolltobottom.png"></div> | |
<div id="scrolltotop"><img src="<?php echo base_url();?>interface/img/scrolltotop.png"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment