html>
shangxiabody {
margin: 0;
padding: 0;
}
#div1 {
width: 100px;
height: 100px;
background: red;
margin: 0 auto;
}
#div1 {
width: 100px;
height: 100px;
position: absolute;
right: 50%;
margin-right: -50px;
}
window.onload = function() {
var oDiv = document.getElementById('div1');
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var t = scrollTop + (document.documentElement.clientHeight - oDiv.offsetHeight) / 2;
var timer = 0;
starMove(100)
function starMove(target) {
clearInterval(timer)
timer = setInterval(function() {
var speed = (target - oDiv.offsetTop) / 8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if (oDiv.offsetTop == target) {
clearInterval(timer)
} else {
oDiv.style.top = oDiv.offsetTop + speed + 'px';
};
txt1.value = oDiv.offsetTop + ',目标:' + target;
}, 30)
}
}
chrome/firefox中打开都可以随滚动条移动;
“下拉默认位置Div居中”,这是一个什么描述? 没懂。
做了两个样式,见#div1,两个都可以将div放置到屏幕中央。