一、链式运动

首先,要改进运动框架

function getStyle(obj,attr){
  if(obj.currentStyle){
    return obj.currentStyle[attr];
  }else{
    return getComputedStyle(obj,false)[attr];
  }
}
 function startMove(obj,attr,iTarget,fn){//多div运动
//var obj = document.getElementsByClassName('menu');

clearInterval(obj.timer);
obj.timer=setInterval(function(){
  var iCur=0;
  if(attr=='opacity'){
    var iCur=parseInt(parseFloat(getStyle(obj,attr))*100);
  }else{
    var iCur=parseInt(getStyle(obj,attr));
}

var iSpeed=(iTarget-iCur)/8;
iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);console.log(iSpeed+'/'+iCur);
  if(iCur==iTarget){
    clearInterval(obj.timer);

    if(fn){fn()};
  }else{
    if(attr=='opacity'){
        obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
      obj.style.opacity=(iCur+iSpeed)/100;
    }else{
      obj.style[attr]=iCur+iSpeed+'px';console.log(obj.style.attr);
    }

  }
},30);
  }

然后引用即可

var oBtn = document.getElementById('btn1');
var oDiv = document.getElementById('div1');

oBtn.onmouseover=function(){
  startMove(oDiv,'width',300,function(){
    startMove(oDiv,'height',333,function(){
      startMove(oDiv,'opacity',100);
    });
  });
}
oBtn.onmouseout=function(){
  startMove(oDiv,'opacity',50,function(){
    startMove(oDiv,'height',20,function(){
      startMove(oDiv,'width',0);
    });
  });
}

二、用json打造完美框架

function getStyle(obj,attr){
  if(obj.currentStyle){
    return obj.currentStyle[attr];
  }else{
    return getComputedStyle(obj,false)[attr];
  }
}
 function startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var bStop=true;//如为真,这次运动结束,所有值都达到目标
for(var attr in json){
//1.取当前的位置
var iCur=0;
if(attr=='opacity'){
var iCur=parseInt(parseFloat(getStyle(obj,attr))*100);
}else{
var iCur=parseInt(getStyle(obj,attr));
}
//2.算速度
var iSpeed=(json[attr]-iCur)/8;
iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);//console.log(iSpeed+'/'+iCur);

//3.检测停止
if(iCur!=json[attr]){
bStop=false;
}
if(attr=='opacity'){
  obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
obj.style.opacity=(iCur+iSpeed)/100;
}else{
obj.style[attr]=iCur+iSpeed+'px';
}
}
if(bStop){
clearInterval(obj.timer);
if(fn){fn()};
}
},30);
}

三、照片墙——多图展开、收缩

var oUl = document.getElementById('ul1');
var aLi = oUl.getElementsByTagName('li');
var oImg = document.getElementsByClassName('img1');
//js布局
var minIndex=2;//设置层级初始值
var i=0;
for(i=0;i<aLi.length;i++){
//aLi[i].innerHTML='left:'+aLi[i].offsetLeft+',top:'+aLi[i].offsetTop;
aLi[i].style.left=aLi[i].offsetLeft+'px';
aLi[i].style.top=aLi[i].offsetTop+'px';
}
for(i=0;i<aLi.length;i++){
aLi[i].style.position='absolute';
}
//加事件
for(i=0;i<aLi.length;i++){

aLi[i].onmouseover=function(){
this.style.zIndex=minIndex++;//console.log(this.style.zIndex);确保当前图片的层级为最高
startMove(this,{width:200,height:200,margin:-50});

}
aLi[i].onmouseout=function(){
startMove(this,{width:100,height:100,margin:10});
}
}
for(i=0;i<oImg.length;i++){
oImg[i].onmouseover=function(){
startMove(this,{width:300,height:200,margin:0});
}
oImg[i].onmouseout=function(){
startMove(this,{width:100,height:100,margin:0});
}
}

四、新浪微博之大家正在说

var oTxt = document.getElementById('txt1');
var oBtn = document.getElementById('btn1');
var oUl = document.getElementById('ul1');

oBtn.onclick=function(){
  var oLi = document.createElement('div');//这里要用div替换li,否则IE7的移动效果会卡
var aLi = oUl.getElementsByTagName('div');//这里要用div替换li,否则IE7的移动效果会卡

oLi.innerHTML=oTxt.value;
oTxt.value='';

if(aLi.length){
oUl.insertBefore(oLi,aLi[0]);
}else{
oUl.appendChild(oLi);
}

var iHeight=oLi.offsetHeight;
oLi.style.height=0;

startMove(oLi,{height:iHeight},function(){
startMove(oLi,{opacity:100});//alert();
});
}

布局的时候也要用div套div代替ul套li

五、无缝滚动

var oDiv = document.getElementById('div1');
var oUl = oDiv.getElementsByTagName('ul')[0];
var oLi = oUl.getElementsByTagName('li');
var aA = document.getElementsByTagName('a');
var timer=null;
var iSpeed=-3;

oUl.innerHTML+=oUl.innerHTML;
oUl.style.width=oLi.length*oLi[0].offsetWidth+'px';
function fnMove(){
  if(oUl.offsetLeft<-oUl.offsetWidth/2){
    oUl.style.left=0;
  }else if(oUl.offsetLeft>0){
    oUl.style.left=-oUl.offsetWidth/2+'px';
  }
oUl.style.left=oUl.offsetLeft+iSpeed+'px';
}
timer=setInterval(fnMove,30);

aA[0].onclick=function(){
  iSpeed=-3;
};
aA[1].onclick=function(){
  iSpeed=3;
};
oDiv.onmouseover=function(){
  clearInterval(timer);
}
oDiv.onmouseout=function(){
  timer=setInterval(fnMove,30);
}

js基础之动画(三)的更多相关文章

  1. GSAP JS基础教程--动画的控制及事件

    好多天没有写无博文啦,今天无聊就再写一下! 今天要讲的是TweenLite的一些事件以及,TweenLite动画的控制,TweenMax类似,请自行参考官方文档:http://api.greensoc ...

  2. JS基础速成(三)- DOM(文件对象模型)

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.DOM树的基本结构 DOM节点分为三大类:元素节点(标签节点),属性节 ...

  3. Vue.js基础语法(三)

    vue学习的一系列,全部来自于表哥---表严肃,是我遇到过的讲课最通透,英文发音最好听的老师,想一起听课就去这里吧 https://biaoyansu.com/i/hzhj1206 1过滤器filte ...

  4. js基础之动画(二)

    一.多物体同时运动 栗子一:多个Div,鼠标移入变高,动态下拉菜单 function startMove(obj,iTarget){  clearInterval(obj.timer); obj.ti ...

  5. js基础之动画(一)

    一.让div动起来 var oBtn = document.getElementById('btn1');  var timer='';//设置定时器 oBtn.onclick=function st ...

  6. 前端新人学习笔记-------html/css/js基础知识点(三)

    这断时间家里有点事,上班也有点任务,所以几天没看视频没来更新了.今天来更新一下了. 一:默认样式重置 但凡是浏览默认的样式,都不要使用. body,p,h1,h2,h3,h4,h5,h6,dl,dd{ ...

  7. JS基础语法---创建对象---三种方式创建对象:调用系统的构造函数;自定义构造函数;字面量的方式

    创建对象三种方式: 调用系统的构造函数创建对象 自定义构造函数创建对象(结合第一种和需求通过工厂模式创建对象) 字面量的方式创建对象 第一种:调用系统的构造函数创建对象 //小苏举例子: //实例化对 ...

  8. JS基础_if练习三

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. JS——基础知识(三)

    1.select (1)它的选择事件是onchange (2)他的选项索引可以通过value获取,比tab选项卡要方便一点. 2.数组常用方法 (1)添加元素 push():可以向一个数组末尾添加一个 ...

随机推荐

  1. activity去标题栏操作&保留高版本主题

    方式一:每个类都需要去添加此代码 在setContentView(R.layout.activity_splash); 前设置以下代码 requestWindowFeature(Window.FEAT ...

  2. Java注解Annotation学习

    学习注解Annotation的原理,这篇讲的不错:http://blog.csdn.net/lylwo317/article/details/52163304 先自定义一个运行时注解 @Target( ...

  3. Linux下利用rsync实现多服务器文件同步

    windows做为文件服务器,使用rsync的windows服务版本,然后配置好就可以了.需要的朋友可以参考下. windows做为文件服务器,使用rsync的windows服务版本:cwRsyncS ...

  4. hiho_1067_最近公共祖先2

    题目大意 给出一棵家谱树,树中的节点都有一个名字,保证每个名字都是唯一的,然后进行若干次查询,找出两个名字的最近公共祖先. 题目链接最近公共祖先 分析 数据量大,根据题目提示,采用Tarjan + 并 ...

  5. html5常用API之Full Screen

    所谓Full Screen API,就是全屏API,在html5中,该API允许开发者以编程方式将Web应用程序全屏运行,使Web应用程序更像本地应用程序.这款API十分简单有用,是html5初学者必 ...

  6. RequireJS加载ArcGIS API for JavaScript

    1.在main.js中配置ArcGIS API for JavaScript require.config({ paths : { //arcgisJS "esri": " ...

  7. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

  8. MM--发票校验 及基于采购订单的MIRO发票校验过程(

    一.介绍发票校验是物料管理(MM)系统的一部分.它提供物料管理部分和财务会计, 成本控制和资产管理部分的连接.物料管理模块的发票校验为以下目的服务:它完成物料采购的全过程 - 物料采购从采购申请开始, ...

  9. mydbtest文档

    mydbtest是楼方鑫编写的一个数据库测试工具,有需要的话,请自取 http://pan.baidu.com/s/1mgJpukg#path=%252FOneSQL%252FDocument 找到& ...

  10. linux笔记:搜索命令find,locate,which,whereis,grep

    命令名称:find功能:文件搜索命令所在路径:/bin/find用法:find 搜索范围 匹配条件其他:举例:find /root -name initfind /root -size +1024fi ...