Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HTML:
- <button id=scrollme type="button">To the top</button>
- js:
- function runScroll() {
- scrollTo(document.body, 0, 600);
- }
- var scrollme;
- scrollme = document.querySelector("#scrollme");
- scrollme.addEventListener("click",runScroll,false)
- function scrollTo(element, to, duration) {
- if (duration <= 0) return;
- var difference = to - element.scrollTop;
- var perTick = difference / duration * 10;
- setTimeout(function() {
- element.scrollTop = element.scrollTop + perTick;
- if (element.scrollTop == to) return;
- scrollTo(element, to, duration - 10);
- }, 10);
- }
- //Another script for the Back To Top
- $('#backToTop').each(function(){
- $(this).click(function(){
- $('html,body').animate({ scrollTop: 0 }, 'slow');
- return false;
- });
- });
- For the Button :
- if (scroller < 100) { $("#backToTop").css('transform', 'translate(0, 100px)'); }
- else { $("#backToTop").css('transform', 'translate(0, 0)'); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement