<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动画</title>
<style>
*{margin:0;padding:0;}
.box{width: 400px;height: 300px;background: #000;margin:40px auto;color: #fff;font-size: 18px;text-align: center;}
</style>
<script>
//获取对象样式规则信息,IE下使用currentStyle
function getStyle(obj,style){
return obj.currentStyle?obj.currentStyle[style]:getComputedStyle(obj,false)[style];
}
//原生js动画类似jquery--animate
function animate(obj,styleJson,callback){
clearInterval(obj.timer);
// 开启定时器
obj.timer=setInterval(function(){
var flag=true;//假设所有动作都已完成成立。
for(var styleName in styleJson){
//1.取当前属性值
var iMov=0;
// 透明度是小数,所以得单独处理
iMov=styleName=='opacity'?Math.round(parseFloat(getStyle(obj,styleName))*100):parseInt(getStyle(obj,styleName));
//2.计算速度
var speed=0;
speed=(styleJson[styleName]-iMov)/8;//缓冲处理,这边也可以是固定值
speed=speed>0?Math.ceil(speed):Math.floor(speed);//区分透明度及小数点,向上取整,向下取整
//3.判断是否到达预定值
if(styleJson[styleName]!=iMov){
flag=false;
if(styleName=='opacity'){//判断结果是否为透明度
obj.style[styleName]=(iMov+speed)/100;
obj.style.filter='alpha(opacity:'+(iMov+speed)+')';
}else{
obj.style[styleName]=iMov+speed+'px';
}
}
}
if(flag){//到达设定值,停止定时器,执行回调
clearInterval(obj.timer);
if(callback){callback();}
}
},30)
}
window.onload=function(){
document.getElementById('box').onclick=function(){
var oThis=this;
animate(oThis,{'width':'500'},function(){
animate(oThis,{'height':'400'},function(){alert('宽度高度都增加了')});
});
}
}
</script>
</head>
<body>
<div class="box" id="box">点击效果:宽度增加->高度增加->弹出框</div>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有