关于元素添加animation动画问题的思考

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<style>
.flayer{
width: 200px;
height: 200px;
position: relative;
overflow: hidden;
background-color: blue;
}
.masklayer{
position: absolute;
left: 0;
top: 0;
/*display: none;*/
opacity: 0;
width: 200px;
height: 200px;
color: #fff;
line-height: 200px;
background-color: pink;
}
.maskshow{
position: absolute;
left: 0;
top: 0;
width: 200px;
height: 200px;
color: #fff;
line-height: 200px;

-webkit-animation: show 1s linear;
-webkit-animation: show 1s linear;
-moz-animation: show 1s linear;
animation: show 1s linear;

animation-fill-mode: forwards;
background-color: pink;
}
.maskhide{
position: absolute;
left: 0;
top: 0;
width: 200px;
height:200px;
color: #fff;
line-height: 200px;
/*display: none;*/
/*如果设置display:none元素会直接消失没有动画
可以通过给元素添加监听事件addEventLister("webkitAnimationEnd",func,false)
判断动画是否完成*/
-webkit-animation: hide 1s linear;
-moz-animation: hide 1s linear;
animation: hide 1s linear;
-webkit-animation-fill-mode: forwards;
-moz-animation-fill-mode: forwards;
animation-fill-mode: forwards;
/*保存最后一次状态*/
background-color: pink;
}
@keyframes show{
from{-webkit-transform: scale(2);-moz-transform: scale(2);-ms-transform: scale(2);transform: scale(2);opacity: 0.5}
to{-webkit-transform: scale(1);-moz-transform: scale(1);-ms-transform: scale(1);transform: scale(1);opacity: 1;}
}
@keyframes hide{
from{-webkit-transform: scale(1);-moz-transform: scale(1);-ms-transform: scale(1);transform: scale(1);opacity: 0.5;}
to{-webkit-transform: scale(2);-moz-transform: scale(2);-ms-transform: scale(2);transform: scale(2);opacity: 0;}
}
</style>
<script>
window.onload=function(){
var flayer=document.getElementsByClassName("flayer")[0];
var masklayer=document.getElementsByClassName("masklayer")[0];
flayer.onmouseover=function(){
masklayer.className="maskshow";
/*通过css控制动画,如果直接用js给元素添加animation属性,会出现重复动画bug,可能是因为没有删除动画操作。*/
}
flayer.onmouseout=function(){
masklayer.className="maskhide";
}
}
</script>
</head>
<body>
<div class="flayer">first layer
<div class="masklayer">由大到小显示|由小到大消失</div>
</div>
</body>
</html>

posted @   枫若  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示