當頁面有滾動條並且滾動條不位於頂部時,出現返回頂部的圖片或者按鈕,返回頂部後自動消失,求大神指點
<style>#toTop{position:fixed;right:0;bottom:20px;display:none}</style>
<div style="height:2000px"></div>
<a href="#" id="toTop">top↑</a>
<script>
function winScroll() {
var st = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
document.getElementById('toTop').style.display=st>=100?'block':'none'
}
var timer
window.onscroll = function () {
clearTimeout(timer);
timer = setTimeout(winScroll, 10);
}
</script>