一、json 遍历

 for in  关键字

 for ( 变量 in  对象)

 { 执行语句;  }

例如:

var json = {width:200,height:300,left:50}
console.log(json.width);
for(var k in json)
{
    console.log(k);   // k 遍历的是json  可以得到的是  属性
    console.log(json[k]);  // json[k]  得到 是属性的  值
}

二、回调函数

等动画执行完毕再去执行的函数    回调函数

很简单   当定时器停止了。 动画就结束了

三、 in 运算符

in运算符也是一个二元运算符in运算符要求1个(左边的)操作数必须是字符串类型可以转换为字符串类型的其他类型,而2个(右边的)操作数必须是数组对象。只有第1个操作数的值是第2个操作数的属性名,才会返回true,否则返回false

// in 可以用用来判断 json 里面有没有某个属性

var json = {name: "刘德华",age : 55};
// in 可以用用来判断 json 里面有没有某个属性
if("andy" in json)
{
    console.log("yes");  // 返回的是 yes
}
else
{
    console.log("no");
}

四、案例

1、返回当前样式的函数

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
width: 100px;
height: 200px;
position: absolute;
top:10px;
left:20px;
background-color: yellow;
z-index: 2;
opacity: 0.4;
}
</style>
</head>
<body>
<div id="demo"></div>
</body>
</html>
<script>
var demo=document.getElementById("demo");
function getStyle(obj,attr){//谁的 属性
if(obj.currentStyle){
return obj.currentStyle[attr];//ie
}else{
return window.getComputedStyle(obj,null)[attr];//w3c
}
}
console.log(getStyle(demo,"width"));// 调用 width必须加引号
</script>

2、封装运动框架单个属性

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {
width: 100px;
height: 100px;
background-color: green;
position: absolute;
top: 50px;
left:0;
opacity: 0.5;
}
</style>
</head>
<body>
<button id="btn200">200</button>
<button id="btn400">400</button>
<div id="box"></div>
</body>
</html>
<script>
//获取CSS属性函数
function getStyle(obj,attr){//谁的 属性
if(obj.currentStyle){
return obj.currentStyle[attr];//ie
}else{
return window.getComputedStyle(obj,null)[attr];//w3c
}
}
var btn200 = document.getElementById("btn200");
var btn400 = document.getElementById("btn400");
var box = document.getElementById("box");
btn200.onclick = function() {
animate(box,"width",500);
}
btn400.onclick = function() {
animate(box,"top",400);
} //封装单个属性运动框架
function animate(obj,attr,target){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
//计算步长 盒子本身的样式+步长
var current=parseInt(getStyle(obj,attr));//获取当前样式 调用getStyle 去掉px
var step=(target-current)/10;
step=step>0?Math.ceil(step):Math.floor(step);
//开始动画
obj.style[attr]=current+step+"px";
if(current==target){
clearInterval(obj.timer);
}
},30)
}
</script>

3、封装运动框架多个属性

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {
width: 100px;
height: 100px;
background-color: green;
position: absolute;
top: 50px;
left:0;
opacity: 0.5;
border-radius: 50%;
}
</style>
</head>
<body>
<button id="btn200">200</button>
<button id="btn400">400</button>
<div id="box"></div>
</body>
</html>
<script> var btn200 = document.getElementById("btn200");
var btn400 = document.getElementById("btn400");
var box = document.getElementById("box");
btn200.onclick = function() {
animate(box,{width:500,top:200,left:400,opacity:40,zIndex:3},function(){alert("我来了")});//function()回调函数
}
btn400.onclick = function() {
animate(box,{top:200,left:200});
} //封装多个属性运动框架
function animate(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
//开始遍历json
var flag=true;//判断是否停止定时器 一定写到遍历外面
for(var attr in json){//attr属性 json[attr]属性值
var current=0;
if(attr=="opacity"){
current=parseInt(getStyle(obj,attr)*100)||0;//数值
}else{
current=parseInt(getStyle(obj,attr));//数值
//console.log(current);
}
//console.log(current); //目标位置就是属性值
var step=(json[attr]-current)/10;//步长=目标位置-当前位置/10
step=step>0?Math.ceil(step):Math.floor(step); //判断透明度
if(attr=="opacity"){//判断用户有没有输入opacity
if("opacity" in obj.style){//判断浏览器是否支持opacity
obj.style.opacity=(current+step)/100;//W3C
}else{
obj.style.filter="alpha(opacity="+(current+step)+")";//IE
} }else if(attr=="zIndex"){//判断层级
obj.style.zIndex=json[attr];
}
else{
obj.style[attr]=current+step+"px";
} //判断什么时候停止定时器
if(current!=json[attr]){//只要其中一个不满足条件 就不应该停止定时器
flag=false;
}
} if(flag){//用于判断定时器的条件
clearInterval(obj.timer);
if(fn){//定时器停止就是动画结束了 再执行fn
fn();//调用函数
}
} },30)
} //获取CSS属性函数
function getStyle(obj,attr){//谁的 属性
if(obj.currentStyle){
return obj.currentStyle[attr];//ie
}else{
return window.getComputedStyle(obj,null)[attr];//w3c
}
} </script>

3、仿360开机效果

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>仿360开机效果</title>
<style>
.box{
width: 322px;
position: fixed;
bottom:0;
right:0;
}
span{
position: absolute;
top:0;
right:0;
width:30px;
height: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="box" id="box">
<span></span>
<div class="hd" id="t">
<img src="data:images/t.jpg" alt=""/>
</div>
<div class="bd" id="b">
<img src="data:images/b.jpg" alt=""/>
</div>
</div>
</body>
</html>
<script>
var b=document.getElementById('b');
var closeAd=document.getElementsByTagName("span")[0];
closeAd.onclick=function(){
animate(b,{height:0},function(){
animate(b.parentNode,{width:0},function(){alert(11)})
})
} //封装多个属性运动框架
function animate(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
//开始遍历json
var flag=true;//判断是否停止定时器 一定写到遍历外面
for(var attr in json){//attr属性 json[attr]属性值
var current=parseInt(getStyle(obj,attr));//数值
//目标位置就是属性值
var step=(json[attr]-current)/10;//步长=目标位置-当前位置/10
step=step>0?Math.ceil(step):Math.floor(step);
obj.style[attr]=current+step+"px";
//console.log(current);
if(current!=json[attr]){//只要其中一个不满足条件 就不应该停止定时器
flag=false;
}
}
if(flag){//用于判断定时器的条件
clearInterval(obj.timer);
if(fn){//定时器停止就是动画结束了 再执行fn
fn();//调用函数
}
} },30)
} //获取CSS属性函数
function getStyle(obj,attr){//谁的 属性
if(obj.currentStyle){
return obj.currentStyle[attr];//ie
}else{
return window.getComputedStyle(obj,null)[attr];//w3c
}
}
</script> 运行效果:

第61天:json遍历和封装运动框架(多个属性)的更多相关文章

  1. [js高手之路]封装运动框架实战左右与上下滑动的焦点轮播图

    在这篇文章[js高手之路]打造通用的匀速运动框架中,封装了一个匀速运动框架,我们在这个框架的基础之上,加上缓冲运动效果,然后用运动框架来做幻灯片(上下,左右),效果如下: 1 2 3 4 5 // 0 ...

  2. 封装运动框架基本函数(多个属性包括透明度和zIndex)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. JS封装运动框架(另一种写法)

    function animate(obj, json, interval, sp, fn) { clearInterval(obj.timer); //var k = 0; //var j = 0; ...

  4. JS封装动画框架,网易轮播图,旋转轮播图

    JS封装动画框架,网易轮播图,旋转轮播图 1. JS封装运动框架 // 多个属性运动框架 添加回调函数 function animate(obj,json,fn) { clearInterval(ob ...

  5. 【repost】JavaScript完美运动框架的进阶之旅

    运动框架的实现思路 运动,其实就是在一段时间内改变left.right.width.height.opactiy的值,到达目的地之后停止. 现在按照以下步骤来进行我们的运动框架的封装: 匀速运动. 缓 ...

  6. js运动框架逐渐递进版

    运动,其实就是在一段时间内改变left.right.width.height.opactiy的值,到达目的地之后停止. 现在按照以下步骤来进行我们的运动框架的封装: 匀速运动. 缓冲运动. 多物体运动 ...

  7. 无限循环轮播图之运动框架(原生JS)

    封装运动框架 function getStyle(obj,name){ if(obj.currentStyle){ return obj.currentStyle[name]; }else{ retu ...

  8. javascript常用函数封装——运动、cookie、ajax、获取行内样式兼容写法、拖拽

    运动.cookie.ajax.获取行内样式兼容写法.拖拽封装大合集. //url,data,type,timeout,success,error function ajax(options){ //- ...

  9. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

随机推荐

  1. ChipScope Pro Inserter - "ERROR:NgdBuild:924 - bidirect pad net '<oDRAM0_A>' is driving non-buffer primitives

    解决方案: Using a IOBUF signal as a trigger for the ILA Inserter flow will cause a NGDBuild error. These ...

  2. 20145209 2016-2017-2 《Java程序设计》第4周学习总结

    20145209 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 继承是面向对象最显著的一个特性.继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性 ...

  3. 成都Uber优步司机奖励政策(3月21日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 成都Uber优步司机奖励政策(3月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. jQuery wordexport导出 word

    同事给我说了简单的导出word的插件,亲测了下,做个随笔. 这个导出插件是jQuery自带的的插件,通过调用wordexport.js来实现导出功能. 1.引入的js <script type= ...

  6. mysql索引 b+树

    1.B+树基本概念 B+树的语言定义比较复杂,简单的说是为磁盘存取设计的平衡二叉树 网上经典图,黄色p1 p2 p3代表指针,蓝色的代表磁盘,里面包含数据项,第一层17,35,p1就代表小于17的,p ...

  7. 180710-MySql插入唯一键冲突的三种可选方式

    MySql插入时唯一键冲突的几种处理方式 MySql插入一条记录,结果提示主键冲突,怎么办? 批量插入数据时,发现插入的这批数据中,有某些记录存在唯一键冲突,一个一个跳出来就比较麻烦了,有什么好的办法 ...

  8. 使用InstallShield-Limited-Edition制作安装包

    1.打开此网站,进行注册,获取序列码以及下载InstallShield-Limited-Edition 2.安装完成之后,打开VisualStudio,新建项目 3.填写基本应用信息 4.配置相关信息 ...

  9. Linux 文件的常识

    文件 文件的分类 文件 目录 链接 区分办法,ls -la 查看 十个标志符中的第一个 如:drwxrwxr-x. 2 normal normal 4096 8月 31 23:43 dir 目录是d ...

  10. 微信小程序入门学习之事件 事件对象 冒泡非冒泡事件(1)

    这关于事件的学习,可以自己复制到微信开发者工具上自己运行试试. 首先这里有两个文件.js 和.wxml 文件 首先给出.js文件下代码 // pages/news/news.js Page({ /** ...