前端JS返回顶部代码

html:

css:
top{right: 50px; bottom: 50px; position: fixed; }
js:

window.onscroll = function(){ var distance = document.documentElement.scrollTop || document.body.scrollTop; if( distance >= 300 ) document.getElementById('top').style.display = ""; } else document.getElementById('top').style.display = "none"; } var Top = document.getElementById("top"); Top.onclick = function(){ document.documentElement.scrollTop = document.body.scrollTop = 0; } }

你可能感兴趣的:(前端JS返回顶部代码)