首先看看演示效果
代码, 函数参数obj为当前想要变动的元素, changeData为想要变动的种类, 例如, func为元素动作结束后想要处理的函数
例如向上面那个演示, 注意: opacity要乘以100, 原因见如下代码
var div = document.getElementsByClassName('demo')[0];startMove(div, , function());
function startMove(obj, changeData, func) {clearInterval(obj.timer);var iCurValue = 0;var iSpeed = 0;var bStop;obj.timer = window.setInterval(function () {bStop = true;for(var name in changeData) {console.log(name, changeData[name]);if(name === 'opacity') {iCurValue = parseFloat(getStyle(obj, name)) * 100;}else {iCurValue = parseInt(getStyle(obj, name));}iSpeed = (changeData[name] - iCurValue) / 7;if(iSpeed > 0) {iSpeed = Math.ceil(iSpeed);}else{iSpeed = Math.floor(iSpeed);}if(name === 'opacity') {obj.style.opacity = (iCurValue + iSpeed) / 100; }else{obj.style[name] = iCurValue + iSpeed + 'px';}if(iCurValue !== changeData[name]) {bStop = false;}}if(bStop) {clearInterval(obj.timer);func();}}, 30);
}