#content {
width: 100px;
height: 100px;
background: red;
position: absolute;
right: 0px;
bottom: 0px;
cursor: pointer;
}
window.onscroll = function () {
var cont = document.getElementById('content');
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
startMove(document.documentElement.clientHeight - cont.offsetHeight + scrollTop);
}
var timer
function startMove(target) {
var cont = document.getElementById('content');
clearInterval(timer);
timer = setInterval(function () {
var speed = (target - cont.offsetTop) / 5;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if (cont.offsetTop == target) {
clearInterval(timer);
} else {
cont.style.top = cont.offsetTop + speed + 'px';
}
}, 20)
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史