[英]My “div” is moving from top to bottom (i.e 200px height from top) and fix on a top(starting postion). but I want to fix on a bottom.How can I do it?
我的HTML div
元素從top
移動bottom
並固定在top
,但我想固定在bottom
,我的意思是,我想將div
從top
固定200px
。
@keyframes mymove {
from {top: 0px;}
to {top: 200px;}
}
</style>
</head>
<body >
<input type="button" id="mybtn" onclick="myfunction()" value="click
me">
<div id="abc" style="display:none" >
<table>
<tr>
<td>Name:</td><td><input type="text"></td>
</tr>
</table>
</div>
</body>
</html>
<script>
function myfunction(){
document.getElementById("abc").style.display="block";
document.getElementById("abc").style.width="500px";
document.getElementById("abc").style.height="100px";
document.getElementById("abc").style.border="2px solid red";
document.getElementById("abc").style.position="absolute";
document.getElementById("abc").style.animation="mymove 1s";
document.getElementById("abc").style.marginTop="200px";
document.getElementById("mybtn").style.display="none";
}
</script>
我希望我div
元素從出來top
至bottom
和固定在bottom
您必須將forwards屬性添加到動畫中。 因此,您的腳本將如下所示。
<script>
function myfunction(){
document.getElementById("abc").style.display="block";
document.getElementById("abc").style.width="500px";
document.getElementById("abc").style.height="100px";
document.getElementById("abc").style.border="2px solid red";
document.getElementById("abc").style.position="absolute";
document.getElementById("abc").style.animation="mymove 1s forwards"; //Here you add forwards
//document.getElementById("abc").style.marginTop="200px";
document.getElementById("mybtn").style.display="none";
}
</script>
您需要指定希望動畫對象保持動畫的最后位置。
將下一行添加到您的代碼中:
document.getElementById("abc").style.animationFillMode = "forwards";
或者,如果您已經有該元素的CSS定義,則可以將其直接添加到CSS中:
#abc {
animation-fill-mode: forwards;
}
您可以在MDN上找到更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.