前端中animation控制動畫定位到動畫結束,並使用js控制3s消失

  1. 代碼
<!DOCTYPE html>
<html>
<head>
	<title>home</title>
	<meta charset="utf-8">
	<style type="text/css">
		.fix_tip{
			width: 500px;
			height: 48px;
			left: 50%;
			margin-left: -250px;
			font-size: 24px;
			line-height: 48px;
			position: fixed;
			background: red;
			color: white;
			text-align: center;
			z-index: 1;
			border: 1px solid red;
			animation:myfirst 3s forwards;
			-moz-animation:myfirst 3s forwards; /* Firefox */
			-webkit-animation:myfirst 3s forwards; /* Safari and Chrome */
			-o-animation:myfirst 3s forwards; /* Opera */
		}
		@keyframes myfirst{
			from{top:0px}
			to{top:100px}
		}
		/* Firefox */
		@-moz-keyframes myfirst {
			from{top:0px}
			to{top:100px}
		}
		/* Safari and Chrome */
		@-webkit-keyframes myfirst {
			from{top:0px}
			to{top:100px}
		}
		 /* Opera */
		@-o-keyframes myfirst{
			from{top:0px}
			to{top:100px}
		}
	</style>
</head>
<body>
	<div class="fix_tip" id="fix_tip">
		<!-- <b>數據格式錯誤,插入失敗</b> -->
	</div>
	<!-- /錯誤提示 -->
	<script type="text/javascript">
		setTimeout("document.getElementById('fix_tip').style.display='none'",6000);
	</script>
</body>
</html>
  1. 動畫結束後停止在結束位置

    網上搜到的都是設置

    animation-fill-mode: forwards;
    

    但是事實上單獨這樣設置是沒有效果的,正確寫法是

    animation:myfirst 3s forwards;
    
  2. div在3s後消失

    1. 獲取控制的div
      document.getElementById('fix_tip')

    2. 設置消失
      這裏是設置該div爲none
      setTimeout("document.getElementById('fix_tip').style.display='none'",6000);
      我這裏設置6s消失是因爲我的h5動畫需要執行3s。

    3. js運行說明
      一種是寫成function,在body中調用;
      另一種就是我在上面所顯示的,直接在body結束標籤之上,body中其他內容填充完畢,再寫js方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章