一、链式运动

首先,要改进运动框架

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. mysql 占用的内存大小

    1.mysql执行查询计划,key_len表示索引使用的字节数,这个字节数和三个条件有关.mysql> create table t1(v1 char(10));Query OK, 0 rows ...

  2. FATAL ha.BootstrapStandby: Unable to fetch namespace information from active NN at ***

    This problem (Unable to fetch namespace information from active NN) occurs, because the active namen ...

  3. openid4java 使用记录[转载]

    [文章来源:http://r-j-r-a5438-163-com.iteye.com/blog/611351] 在项目中使用了openid4java进行开发,在开发过程中碰到过一些问题,在网上也找了很 ...

  4. 自定义view获取宽高

    View在构造函数初始化并未布局处理,此时宽高均为0,待所有控件初始化完毕后,由上级容器对内部各控件进行布局,此时控件才会具有位置与大小属性,可以通过以下方法获取:1.在ondraw()函数中获取,2 ...

  5. 提高php开发效率的9大代码片段

    在网站开发中,我们都期望能高效快速的进行程序开发,如果有能直接使用的代码片段,提高开发效率,那将是极好的.php开发福利来了,今天小编就将为大家分享9大超实用的.可节省大量开发时间的php代码片段. ...

  6. commonJS — 数组操作(for Array)

    for Array github: https://github.com/laixiangran/commonJS/blob/master/src/forArray.js 代码 /** * Creat ...

  7. MFC Socket

    目录 第1章同步TCP通讯    1 1.1 同步通讯与异步通讯    1 1.2 同步通讯类    1 1.3 同步TCP通讯客户端    4 1.3.1 界面    4 1.3.2 界面类声明   ...

  8. java 多线程7(线程的停止)

    notify(): 是很温和的唤醒线程的方法,它不可以指定清除哪一个异常 interrupt(): 粗暴的方式,强制清除线程的等待状态,被清除的线程会接收到一个InterruptedException ...

  9. as的Enter_Frame与Timer

    As3中的Timer和Event.EnterFrame是有明显的区别的. Evnet.EnterFrame是定时间隔多少时间出发.如果执行时间比间隔时间长,则会间隔执行时间这么久. 举个例子: Fla ...

  10. Scrum 项目5.0

    1.团队成员完成自己认领的任务. 2.燃尽图:理解.设计并画出本次Sprint的燃尽图的理想线.参考图6. 3.每日立会更新任务板上任务完成情况.燃尽图的实际线,分析项目进度是否在正轨.    每天的 ...