您的位置:首页 > 其它

基础 - 动画函数的初步封装

2016-08-17 10:30 267 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WeWeZ/article/details/52228560

小数的取整

  WHY?  JS不能精准的表示小数

  Math.round()   四舍五入取整
  Math.ceil()    向上取整
  Math.floor()   向下取整

属性的访问

  点语法

  [""]法

得到内嵌和外链的CSS

  IE Opera  currentStyle

  其他     getComputedStyle第二个参数为伪类

  兼容写法

  function funGetStyle(obj,attr)
  {
  return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,null)[attr];
  }
JSON的遍历
var JSON = {"name":"WeWeZhang","hobby":"money"};
for (var key in JSON) {
console.log("键"+key+"值"+JSON[key])
}

in运算符
var bIsContain = string in array;

初步封装动画函数


function funGetStyle(obj,attr) {
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,null)[attr];
}
function funAnimateStart(obj,JSON,funFinish) {
funAnimateStop(obj);
funMove(obj,JSON,funFinish);
obj.timer = setInterval(function () {
funMove(obj,JSON,funFinish);
},30);
}
function funAnimateStop(obj) {
if(obj.timer) clearInterval(obj.timer);
}
function funMove(obj,JSON,funFinish) {
obj.bIsStop = true;
for(var key in JSON) {
funMoveDetial(obj,key,JSON[key]);
}
if (obj.bIsStop) {
funAnimateStop(obj);
if (funFinish) funFinish();
}
}
function funMoveDetial(obj,attr,target) {
if (attr=="opacity") {
obj.value = Math.round(parseFloat(funGetStyle(obj,"opacity"))*100);
}else{
obj.value = parseInt(funGetStyle(obj, attr));
}
if (target == obj.value) return;
var nChaZhi = (target- obj.value) / 50;
obj.step = nChaZhi > 0 ? Math.ceil(nChaZhi) : Math.floor[nChaZhi];
obj.value += obj.step;
if (attr=="opacity") {
if ("opacity" in obj.style) {
obj.style.opacity = obj.value*0.01
}else{
obj.style.filter = "alpha(opacity = "+obj.value+")";
}
}else{
obj.style[key] = obj.value + "px";
}
if (target == obj.value) return;
obj.bIsStop = false;
}

 

 

  

  


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: