js动画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ani</title>
    <style type="text/css">
    *{
        margin:0px;
        padding:0px;
    }
    ul{
        list-style: none;

    }
    ul li{
    height:200px;
    width:200px;
    border:4px solid #233;
    margin-bottom:20px;
    background: yellow;
    }
    #list2{
        filter: alpha(opacity:30);
        opacity: 0.3;
    }
    </style>
    <script>
    window.onload=function(){

    //var list=document.getElementsByTagName("li");
      var list0=document.getElementById("list0");
      var list1=document.getElementById("list1");
      var list2=document.getElementById("list2");
      var list3=document.getElementById("list3");
      var list4=document.getElementById("list4");
    /*for(var i=0;i<list.length;i++){
         list[i].timer=null;
        list[i].=function(){
        startmove(this,400);

        }
        list[i].onmouseout=function(){
            startmove(this,200);
        }
    }    */
    list0.onmouseover=function(){
        startmove(this,"height",400);}
    list0.onmouseout=function(){
        startmove(this,"height",200);}
    list1.onmouseover=function(){
        startmove(this,"width",400);}
    list1.onmouseout=function(){
        startmove(this,"width",200);}
    list2.onmouseover=function(){
        startmove(this,"opacity",100);}
    list2.onmouseout=function(){
        startmove(this,"opacity",30);}
    list3.onmouseover=function(){
        startmove(list3,"height",400,function(){
            startmove(list3,"width",400,function(){
                startmove(list3,"opacity",30);
            });
        });}
        list3.onmouseout=function(){
        startmove(list3,"opacity",100,function(){
            startmove(list3,"width",200,function(){
                startmove(list3,"height",200);
            });
        });}
    list4.onmouseover=function(){
        startmove1(this,{height :400,width :400,opacity:30});}
    list4.onmouseout=function(){
        startmove1(this,{height :200,width :200,opacity:100});}
function startmove1(obj,json,fn){
    var flag=true;
     clearInterval(obj.timer);
     obj.timer=setInterval(function(){
     for(var i in json){
         if(i=="opacity"){
             var curr=Math.round(parseFloat(getStyle(obj,i))*100);
             alert(curr);
         }
         else{
         var curr=parseInt(getStyle(obj,i));}
         var speed=(json[i]-curr)/8;
         speed=speed>0?Math.ceil(speed):Math.floor(speed);
         if(curr!=json[i]){
             flag=false;}
         if(i=="opacity"){
             obj.style.filter="alpha(opacity:"+(curr+speed)+")";
             obj.style.opacity=(curr+speed)/100;
         }
         else{
            obj.style[i]=curr+speed+"px";}

        if(flag){
          clearInterval(obj.timer);
        }
        if(fn){
             fn();

}
}
     },30);
    }
    function startmove(obj,arr,iTarge,fn){
     clearInterval(obj.timer);
     obj.timer=setInterval(function(){
         if(arr=="opacity"){
             var curr=Math.round(parseFloat(getStyle(obj,arr))*100);
             alert(curr);
         }
         else{
         var curr=parseInt(getStyle(obj,arr));}
         var speed=(iTarge-curr)/8;
         speed=speed>0?Math.ceil(speed):Math.floor(speed);
         if(curr==iTarge){
         clearInterval(obj.timer);
         if(fn){
             fn();
         }
         }
     else{
         if(arr=="opacity"){
             obj.style.filter="alpha(opacity:"+(curr+speed)+")";
             obj.style.opacity=(curr+speed)/100;
         }
         else{
            obj.style[arr]=curr+speed+"px";}
 }
     },30);
    }
    function getStyle(obj,arr){
        if(obj.currentStyle){
            //alert(obj.currentStyle[arr]);
            return obj.currentStyle[arr];
        }
        else{
            return getComputedStyle(obj,false)[arr];
        }
    }
}
    </script>
</head>
<body>
<div><ul>
    <li id="list0"></li>
    <li id="list1"></li>
    <li id="list2"></li>
    <li id="list3"></li>
    <li id="list4"></li>
</ul></div>
</body>
</html>

五个li前三个都是只改变高、宽、透明度中的其中一个。第四个是链式动画,当鼠标移入的时候先改变高度再改变宽度,最后再改变透明度;当鼠标移出的时候反序恢复,先恢复透明度与,再恢复宽度最后恢复高度。最后一个li是同时运动,使得元素的高宽和透明度同时发生改变。为了实现同时改变引用了json。

json的格式是{a:b,c:d......}a和c代表元素,相当于object,b 和d代表数值,相当于iTarget。通过对json的for-in循环实现元素的同时运动。

json 的for-in循环的格式是for(var i in json){.....}其中i指的是 元素名,json[i]代表元素名对应的数值。

对于已经可以实现高宽运动的startmove函数,为了让元素通过startmove同时实现对透明度的改变需要对元素名进行判断,如果是opacity就特殊处理,否则正常处理。

对于元素的链式动画,需要对startmove的形参中多添加一个代表函数的形参。然后在清除定时器之前判断,如果有此实参传入就先清除定时器然后执行此函数。

对于元素的同时运动,将要同时执行的要求放入json中,然后通过for循环执行运动。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>ani</title><style type="text/css">*{margin:0px;padding:0px;}ul{list-style: none;        }ul li{height:200px;width:200px;    border:4px solid #233;margin-bottom:20px;background: yellow;}#list2{filter: alpha(opacity:30);opacity: 0.3;}</style><script>window.onload=function(){//var list=document.getElementsByTagName("li");      var list0=document.getElementById("list0");      var list1=document.getElementById("list1");      var list2=document.getElementById("list2");      var list3=document.getElementById("list3");      var list4=document.getElementById("list4");/*for(var i=0;i<list.length;i++){ list[i].timer=null;list[i].=function(){startmove(this,400);}list[i].onmouseout=function(){startmove(this,200);}}*/    list0.onmouseover=function(){    startmove(this,"height",400);}    list0.onmouseout=function(){    startmove(this,"height",200);}    list1.onmouseover=function(){    startmove(this,"width",400);}    list1.onmouseout=function(){    startmove(this,"width",200);}    list2.onmouseover=function(){    startmove(this,"opacity",100);}    list2.onmouseout=function(){    startmove(this,"opacity",30);}    list3.onmouseover=function(){    startmove(list3,"height",400,function(){    startmove(list3,"width",400,function(){    startmove(list3,"opacity",30);    });    });}    list3.onmouseout=function(){    startmove(list3,"opacity",100,function(){    startmove(list3,"width",200,function(){    startmove(list3,"height",200);    });    });}    list4.onmouseover=function(){    startmove1(this,{height :400,width :400,opacity:30});}    list4.onmouseout=function(){    startmove1(this,{height :200,width :200,opacity:100});}function startmove1(obj,json,fn){var flag=true;     clearInterval(obj.timer);          obj.timer=setInterval(function(){     for(var i in json){     if(i=="opacity"){     var curr=Math.round(parseFloat(getStyle(obj,i))*100);     alert(curr);     }     else{     var curr=parseInt(getStyle(obj,i));}     var speed=(json[i]-curr)/8;     speed=speed>0?Math.ceil(speed):Math.floor(speed);     if(curr!=json[i]){     flag=false;}     if(i=="opacity"){     obj.style.filter="alpha(opacity:"+(curr+speed)+")";     obj.style.opacity=(curr+speed)/100;     }     else{            obj.style[i]=curr+speed+"px";}         if(flag){          clearInterval(obj.timer);        }        if(fn){         fn();         }}      },30);    }    function startmove(obj,arr,iTarge,fn){     clearInterval(obj.timer);          obj.timer=setInterval(function(){     if(arr=="opacity"){     var curr=Math.round(parseFloat(getStyle(obj,arr))*100);     alert(curr);     }     else{     var curr=parseInt(getStyle(obj,arr));}     var speed=(iTarge-curr)/8;     speed=speed>0?Math.ceil(speed):Math.floor(speed);     if(curr==iTarge){         clearInterval(obj.timer);         if(fn){         fn();         }     }     else{     if(arr=="opacity"){     obj.style.filter="alpha(opacity:"+(curr+speed)+")";     obj.style.opacity=(curr+speed)/100;     }     else{            obj.style[arr]=curr+speed+"px";} }     },30);    }    function getStyle(obj,arr){    if(obj.currentStyle){    //alert(obj.currentStyle[arr]);    return obj.currentStyle[arr];    }    else{    return getComputedStyle(obj,false)[arr];    }    }}</script></head><body><div><ul><li id="list0"></li><li id="list1"></li><li id="list2"></li><li id="list3"></li><li id="list4"></li></ul></div></body></html>

时间: 2024-08-08 03:36:25

js动画的相关文章

关于JS动画和CSS3动画的性能差异

本文章为综合其它资料所得. 根据Google Developer,Chromium项目里,渲染线程分为main thread和compositor thread.如果CSS动画只是改变transforms和opacity,这时整个CSS动画得以在compositor thread完成(而JS动画则会在main thread执行,然后触发compositor进行下一步操作)在JS执行一些昂贵的任务时,main thread繁忙,CSS动画由于使用了compositor thread可以保持流畅,可

【06-23】js动画学习笔记01

1 <html> 2 <head> 3 <style> 4 * { 5 margin:0; 6 padding:0; 7 } 8 #div1{ 9 width:200px; 10 height:200px; 11 background:red; 12 position:relative; 13 left:-200px; 14 top:0; 15 } 16 #div1 span{ 17 width:20px; 18 height:50px; 19 background:s

js 动画方法之requestAnimationFrame

js动画实现的方法到现在有三种 1 css3: 通过animattion+keyframes;或 transition 2. setTimeout/setInterval: setTimeout/setInterval:通过递归调用同一方法来不断更新动画元素以达到动起来的效果,动画过度绘制,浪费 CPU 周期以及消耗额外的电能等问题. 3.requestAnimationFrame: requestAnimationFrame:是由浏览器专门为动画提供的API,在运行时浏览器会自动优化方法的调用

CSS VS JS动画,哪个更快[译]

英文原文:https://davidwalsh.name/css-js-animation 原作者Julian Shapiro是Velocity.js的作者,Velocity.js是一个高效易用的js动画库.在<Javascript网页动画设计>一书中对这个库有很多更具体的剖析,对Velocity及JS动画感兴趣的可以一看. 基于Javascript的动画怎么可能总是和 CSS transition 一样快,甚至更快呢?到底是什么秘密呢?Adobe 和 Google 是怎么做到让他们的富媒体移

js动画---一个小bug的处理

对于前面的课程,大家似乎看不出来存在什么问题,一切都很顺利,但是其实是存在一个很大的bug的,这个bug是什么呢?? 我们来看看下面这个程序就知道了 <!DOCTYPE html> <html> <head> <title>js动画事件</title> <link href="move.css" rel="stylesheet" type="text/css"/> <s

性能更好的js动画实现方式&mdash;&mdash;requestAnimationFrame

用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css3动画出来后,我们又可以使用css3来实现动画了,而且性能和流畅度也得到了很大的提升.但是css3动画还是有不少局限性,比如不是所有属性都能参与动画.动画缓动效果太少.无法完全控制动画过程等等.所以有的时候我们还是不得不使用setTimeout或setInterval的方式来实现动画,可是setTimeout和setInterval有着严重的性能问题,虽然某些现代浏览器对这两函个数进行了一些优化,但还是

CSS vs JS动画:谁更快?

CSS vs JS动画:谁更快? 2016-05-16 前端大全 (点击上方公众号,可快速关注) 英文:Julian Shapiro 译者:MZhou's blog 链接:http://zencode.in/19.CSS-vs-JS动画:谁更快?.html 这篇文章翻译自 Julian Shapiro 的 CSS vs. JS Animation: Which is Faster?.Julian Shapiro 也是 Velocity.js 的创造者.这是一个非常高效.简单易用的JS动画库.他在

zepto.js动画源码

;(function($, undefined){ var prefix = '', eventPrefix, endEventName, endAnimationName, vendors = { Webkit: 'webkit', Moz: '', O: 'o' }, document = window.document, testEl = document.createElement('div'), supportedTransforms = /^((translate|rotate|sc

js动画最佳实现——requestAnimationFrame

我们经常用setInterval来实现动画,其实这种做法不是太好,因为不同浏览器的刷新频率也不一样(一般认为设置16为最佳,按每秒60帧算,1000/60≈16.67) var dis = 0,timer = 0; clearInterval(timer); timer = setInterval(function(){ div.style.left = ++dis; if(dis>=50) clearInterval(timer) },16) 实现js动画最好的是requestAnimatio

简单模拟jQuery创建对象的方法,以及封装一个js动画框架

今天无事点开了N年未点开的慕课网,看了一个js动画框架的视频,心血来潮用jQuery的方法封装了一下,虽然不如jQuery,但是还是有点点所获. 什么都不说,直接上代码: /** * 这是框架的唯一对象,使用jQuery框架的创建方法 * @class MyAnimation * @constructor */ function MyAnimation(Selector){ //返回MyAnimation原型链中init()方法创建的对象 return new MyAnimation.proto