end:元素最终的样式
dur:动画持续多长时
fx:效果插件
*/
function animate(o ,end, dur, fx) {
var curTime=0;
var start = {};//元素的初始样式
var alter = {};//元素的增量样式
var t=setInterval(function () {
if (curTime>=dur) clearTimeout(t);
for (var i in end) {
if(! (i in start))//注意加括号
{
//不能用 parseInt.有透明度时会出问题
start[i] = parseFloat(getStyle(o, i));
}
if(! (i in alter))
{
alter[i] = end[i] - start[i];
}
var val = fx(start[i],alter[i],curTime,dur);
if(i == 'opacity')
{
/**
o.style.filter, o.style.opacity 火狐下都为空字符串
只能用 o.style.opacity 检测
注意:ietester下无法测试透明度
*/
if(typeof o.style.opacity == "undefined")
{
o.style.filter = "alpha(opacity="+val*100+")";
}else{
o.style[i] = val;
}
}else{
o.style[i] = val+'px';
}
}
curTime+=13; //jquery 中也为 13
},13);
}
/**
获取元素样式
处理透明度、元素浮动样式的获取 ,结果带有单位
*/
function getStyle(elem, name) {
var nameValue = null;
if (document.defaultView) {
var style = document.defaultView.getComputedStyle(elem, null);
nameValue = name in style ? style[name] : style.getPropertyValue(name);
} else {
var style = elem.style,
curStyle = elem.currentStyle;
//透明度 from youa
if (name == "opacity") {
if (/alpha(opacity=(.*))/i.test(curStyle.filter)) {
var opacity = parseFloat(RegExp.$1);
return opacity ? opacity / 100 : 0;
}
return 1;
}
if (name == "float") {
name = "styleFloat";
}
var ret = curStyle[name] || curStyle[camelize(name)];
//单位转换 from jqury
if (!/^-?d+(?:px)?$/i.test(ret) && /^-?d/.test(ret)) {
var left = style.left,
rtStyle = elem.runtimeStyle,
rsLeft = rtStyle.left;
rtStyle.left = curStyle.left;
style.left = ret || 0;
ret = style.pixelLeft + "px";
style.left = left;
rtStyle.left = rsLeft;
}
nameValue = ret;
}
return nameValue === 'auto' ? '0px' : nameValue;
}
function camelize(s) {//将CSS属性名转换成驼峰式
return s.replace(/-[a-z]/gi,function (c) {
return c.charAt(1).toUpperCase();
});
}
function Fid(id)
{
return document.getElementById(id);
}
})
</script>
</head>
<style>
.main{ border:1px solid blue; height:350px;}
.pos {position:absolute; left:0px;top:50px; border:5px solid red; background:green;width:100px; height:100px;}
</style>
<body>
<div class="main">
<div id="song" class="pos" style="display:block;">song</div>










