我有一個上下動畫工作正常的div。我得到的問題是,每次頁面加載div開始在頁面的最頂部,然後跳轉到動畫開始後需要的位置。在錯誤的地方動畫加載
<body id="body">
<div id="square"></div>
</body>
#body {
background: #000;
}
#square {
background-color: #fff;
margin: 0 auto;
position: relative;
width: 100px;
height: 100px;
}
var box = document.getElementById('square');
TOP = (window.innerHeight - box.offsetHeight)/2;
box.style.top = TOP;
var down = setInterval(animateDown, 15);
var up;
function animateDown()
{
TOP += 3;
box.style.top = TOP + 'px';
if(TOP > 900){
clearInterval(down);
up = setInterval(animateUp, 15);
}
}
function animateUp()
{
TOP -= 3;
box.style.top = TOP + 'px';
if(TOP <= (window.innerHeight - box.offsetHeight)/2){
clearInterval(up);
down = setInterval(animateDown, 15);
}
}
這裏是對的jsfiddle的鏈接以及>>https://jsfiddle.net/xgilmore/pLbgvc3L/
在此先感謝
適合我(不在jsfiddle中) –
您使用的是什麼瀏覽器? –