码迷,mamicode.com
首页 > 其他好文 > 详细

变速动画函数封装增加任意一个属性

时间:2018-12-02 22:36:16      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:UNC   inter   gets   增加   封装   tin   target   类型   time   

//计算后的样式属性---- 一个元素的任意的一个样式属性值
function getStyle(element,attr) {
//判断这个浏览器是否支持这个方法
return window.getComputedStyle?window.getComputedStyle(element,null)[attr]:element.currentStyle[attr];
}
//匀速动画
function animate(element,attr,target) { //element--元素 attr--属性名字 target--目标位置
//清理定时器
clearInterval(element.timeId);
element.timeId=setInterval(function () {
//获取元素当前位置
var current=parseInt(getStyle(element,attr));//数字类型
//移动的步数
var step=(target-current)/10;
step=step>0?Math.ceil(step):Math.floor(step);
current+=step;
element.style[attr]=current+"px";
if(current==target){
//清理计时器
clearInterval(element.timeId);
}22 变速动画函数封装增加任意一个属性

变速动画函数封装增加任意一个属性

标签:UNC   inter   gets   增加   封装   tin   target   类型   time   

原文地址:https://www.cnblogs.com/lujieting/p/10055289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!