“回到顶部”

来源:互联网 发布:mysql选择数据库 编辑:程序博客网 时间:2024/04/28 03:24
<button id="gotoTop" class="gotoTop" type="button">To the top</button>


window.onload = function () {    document.getElementById('gotoTop').onclick = function () {        scrollTo(document.body, 0, 100);    }} 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;        scrollTo(element, to, duration - 10);    }, 10);}

.gotoTop {    position:fixed;    right:20px;    bottom:20px;}

Du_A http://zhidao.baidu.com/question/1667937056094404787.html?push=keyword

原创粉丝点击