Created
June 5, 2014 14:03
-
-
Save foru17/b0243ea6f8cccace065f to your computer and use it in GitHub Desktop.
原生JS返回顶部动画
This file contains 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
<div it="gotop"> </div> | |
<script> | |
backTop = function (btnId){ | |
var btn = document.getElementById(btnId); | |
var d = document.documentElement; | |
var b = document.body; | |
window.onscroll = btnDisplay; | |
btn.onclick = function (){ | |
btn.style.display = "none"; | |
window.onscroll = null; | |
this.timer = setInterval(function(){ | |
d.scrollTop -= Math.ceil((d.scrollTop+b.scrollTop)*0.1); | |
b.scrollTop -= Math.ceil((d.scrollTop+b.scrollTop)*0.1); | |
if((d.scrollTop + b.scrollTop) == 0) | |
clearInterval(btn.timer,window.onscroll = btnDisplay); | |
},10); | |
}; | |
function btnDisplay(){ | |
btn.style.display=(d.scrollTop+b.scrollTop>200)?'block':"none"; | |
} | |
}; | |
backTop('gotop');//返回顶部调用 | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment