PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > JavaScript教程 > 正文

    9种原生js动画效果

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:9种原生js动画效果
    9种原生js动画效果

    1. 1,匀速动画效果 
    2. window.onload = function(){ 
    3.  var odiv = document.getElementById('odiv'); 
    4.  odiv.onmouseover = function(){ 
    5.   startMover(0); 
    6.  } 
    7.  odiv.onmouseout = function(){ 
    8.   startMover(-200); 
    9.  } 
    10. var timer = null
    11. function startMover(itarget){//目标值 
    12.  clearInterval(timer);//执行当前动画同时清除之前的动画 
    13.  var odiv = document.getElementById('odiv'); 
    14.  timer = setInterval(function(){ 
    15.  var speed = 0; 
    16.  if(odiv.offsetLeft > itarget){ 
    17.   speed = -1; 
    18.  } 
    19.  else
    20.   speed = 1; 
    21.  } 
    22.  if(odiv.offsetLeft == itarget){ 
    23.   clearInterval(timer); 
    24.  } 
    25.  else
    26.   odiv.style.left = odiv.offsetLeft+speed+'px'
    27.   } 
    28.  },30); 
    29.  
    30.  
    31. 2,缓冲动画效果 
    32. window.onload = function(){ 
    33.  var odiv = document.getElementById('odiv'); 
    34.  odiv.onmouseover = function(){ 
    35.   startMover(0); 
    36.  } 
    37.  odiv.onmouseout = function(){ 
    38.   startMover(-200); 
    39.  } 
    40. var timer = null
    41. function startMover(itarget){//速度和目标值 
    42.  clearInterval(timer);//执行当前动画同时清除之前的动画 
    43.  var odiv = document.getElementById('odiv'); 
    44.  timer = setInterval(function(){ 
    45.  var speed = (itarget-odiv.offsetLeft)/10;//缓冲动画的速度参数变化值 
    46.  //如果速度是大于0,说明是向右走,那么就向上取整 
    47.  speed = speed>0?Math.ceil(speed):Math.floor(speed); 
    48.  //Math.floor();向下取整 
    49.  if(odiv.offsetLeft == itarget){ 
    50.   clearInterval(timer); 
    51.  } 
    52.  else
    53.   //clientLeft 返回对象的offsetLeft属性值和到当前窗口左边的真实值之间的距离  
    54.   odiv.style.left = odiv.offsetLeft+speed+'px'
    55.   } 
    56.  },30); 
    57.  
    58. 3,透明度动画 
    59. window.onload = function(){ 
    60.  var odiv = document.getElementsByTagName('div'); 
    61.  for(var i=0;i<odiv.length;i++) 
    62.  { 
    63.    odiv[i].onmouseover = function(){ 
    64.    startOP(this,100); 
    65.   } 
    66.   odiv[i].onmouseout = function(){ 
    67.    startOP(this,30); 
    68.   } 
    69.   odiv[i].timer = null;//事先定义 
    70.   odiv[i].alpha = null;//事先定义 
    71.   //这里发现一个问题,对象的动画属性可以不定义,但是透明度属性必须定义,否则报错 
    72.  } 
    73. function startOP(obj,utarget){ 
    74.   clearInterval(obj.timer);//先关闭定时器 
    75.   obj.timer = setInterval(function(){ 
    76.   var speed = 0; 
    77.   if(obj.alpha>utarget){ 
    78.   speed = -10; 
    79.   } 
    80.   else
    81.   speed = 10; 
    82.   } 
    83.   obj.alpha = obj.alpha+speed; 
    84.   if(obj.alpha == utarget){ 
    85.   clearInterval(obj.timer); 
    86.   } 
    87.   obj.style.filter = 'alpha(opacity:'+obj.alpha+')';//基于IE的 
    88.   obj.style.opacity = parseInt(obj.alpha)/100; 
    89.   },30);  
    90.  
    91. 4,多物体动画 
    92.  
    93. window.onload = function(){ 
    94.  var olist = document.getElementsByTagName('li'); 
    95.  for(var i=0; i<olist.length;i++) 
    96.  { 
    97.   olist[i].onmouseover = function(){ 
    98.   startmov(this,400); 
    99.   }; 
    100.   olist[i].onmouseleave = function(){ 
    101.   startmov(this,200); 
    102.   }; 
    103.   olist[i].timer = null
    104.  } 
    105.  function startmov(obj,itarget){ 
    106.   clearInterval(obj.timer);//执行动画之前清除动画 
    107.   obj.timer = setInterval(function(){ 
    108.    var speed =0; 
    109.    speed = (itarget - obj.offsetWidth)/8; 
    110.    speed = speed>0?Math.ceil(speed):Math.floor(speed); 
    111.    if(obj.offsetWidth == itarget){ 
    112.    clearInterval(obj.timer); 
    113.    } 
    114.    else
    115.    obj.style.width = obj.offsetWidth+speed+'px'
    116.    } 
    117.   },30); 
    118.  } 
    119.  
    120. 5,获取样式动画 
    121.  
    122. window.onload = function(){ 
    123.         var odiv = document.getElementById('odiv'); 
    124.         odiv.onmouseover = function(){ 
    125.             startMov(this); 
    126.         }; 
    127.         function startMov(obj){ 
    128.             setInterval(function(){ 
    129.                 obj.style.width = parseInt(getStyle(obj,'width'))+1+'px'
    130.                 obj.style.fontSize = parseInt(getStyle(obj,'fontSize'))+1+'px'
    131.             },30); 
    132.         } 
    133.         function getStyle(obj,attr) 
    134.         { 
    135.             if(obj.currentStyle){ 
    136.                 return obj.currentStyle[attr]; 
    137.             } 
    138.             else
    139.                 return getComputedStyle(obj,false)[attr]; 
    140.             } 
    141.         } 
    142.     } 
    143.  
    144. 6,多物体复杂动画 
    145.  
    146. window.onload = function(){ 
    147.  var li1 = document.getElementById('li1'); 
    148.  var li2 = document.getElementById('li2'); 
    149.  li1.onmouseover = function(){ 
    150.   startMov(this,400,'width'); 
    151.  }; 
    152.  li1.onmouseout = function(){ 
    153.   startMov(this,200,'width'); 
    154.  }; 
    155.  li2.onmouseover = function(){ 
    156.   startMov(this,200,'height'); 
    157.  }; 
    158.  li2.onmouseout = function(){ 
    159.   startMov(this,100,'height'); 
    160.  }; 
    161.  function startMov(obj,itarget,attr){ 
    162.   clearInterval(obj.timer);//执行动画之前清除动画 
    163.   obj.timer = setInterval(function(){ 
    164.    var icur = parseInt(getStyle(obj,attr)); 
    165.    var speed =0; 
    166.    speed = (itarget - icur)/8; 
    167.    speed = speed>0?Math.ceil(speed):Math.floor(speed); 
    168.    if(icur == itarget){ 
    169.    clearInterval(obj.timer); 
    170.    } 
    171.    else
    172.    obj.style[attr] = icur+speed+'px'
    173.    } 
    174.   },30); 
    175.  } 
    176.  function getStyle(obj,attr) 
    177.  { 
    178.   if(obj.currentStyle){ 
    179.   return obj.currentStyle[attr]; 
    180.   } 
    181.   else
    182.   return getComputedStyle(obj,false)[attr]; 
    183.   } 
    184.  } 
    185. 7,多物体复杂动画(带透明度的) 
    186.  
    187. window.onload = function(){ 
    188.  var li1 = document.getElementById('li1'); 
    189.  var li2 = document.getElementById('li2'); 
    190.  li1.onmouseover = function(){ 
    191.   startMov(this,100,'opacity'); 
    192.  }; 
    193.  li1.onmouseout = function(){ 
    194.   startMov(this,30,'opacity'); 
    195.  }; 
    196.  li2.onmouseover = function(){ 
    197.   startMov(this,200,'height'); 
    198.  }; 
    199.  li2.onmouseout = function(){ 
    200.   startMov(this,100,'height'); 
    201.  } 
    202.  li1.timer = null
    203.  li2.timer = null
    204.  function startMov(obj,itarget,attr){ 
    205.   clearInterval(obj.timer);//执行动画之前清除动画 
    206.   obj.timer = setInterval(function(){ 
    207.    var icur = 0; 
    208.    if(attr == 'opacity'){ 
    209.    icur = Math.round(parseFloat(getStyle(obj,attr))*100);//转换成整数,并且四舍五入下 
    210.    //计算机在计算小数的时候往往是不准确的! 
    211.    } 
    212.    else
    213.    icur = parseInt(getStyle(obj,attr)); 
    214.    } 
    215.    var speed =0; 
    216.    speed = (itarget - icur)/8; 
    217.    speed = speed>0?Math.ceil(speed):Math.floor(speed); 
    218.    if(icur == itarget){ 
    219.    clearInterval(obj.timer); 
    220.    } 
    221.    else
    222.     if(attr == 'opacity'){ 
    223.     obj.style.filter = 'alpha(opacity:'+(icur+speed)+')'
    224.     obj.style.opacity = (icur+speed)/100; 
    225.     } 
    226.     else
    227.     obj.style[attr] = icur+speed+'px'
    228.     }  
    229.    } 
    230.   },30); 
    231.  } 
    232.  function getStyle(obj,attr) 
    233.  { 
    234.   if(obj.currentStyle){ 
    235.   return obj.currentStyle[attr]; 
    236.   } 
    237.   else
    238.   return getComputedStyle(obj,false)[attr]; 
    239.   } 
    240.  } 
    241.  
    242. 8,链式动画 
    243. window.onload = function(){ 
    244.  var li1 = document.getElementById('li1'); 
    245.  li1.onmouseover = function(){ 
    246.   startMov(li1,400,'width',function(){ 
    247.    startMov(li1,200,'height',function(){ 
    248.    startMov(li1,100,'opacity'); 
    249.    }); 
    250.   }); 
    251.  }; 
    252.  li1.onmouseout = function(){ 
    253.   startMov(li1,30,'opacity',function(){ 
    254.    startMov(li1,100,'height',function(){ 
    255.    startMov(li1,100,'width'); 
    256.    }); 
    257.   }); 
    258.  }; 
    259.  li1.timer = null
    260.  function startMov(obj,itarget,attr,fn){//fn回调函数 
    261.   clearInterval(obj.timer);//执行动画之前清除动画 
    262.   obj.timer = setInterval(function(){ 
    263.    var icur = 0; 
    264.    if(attr == 'opacity'){ 
    265.    icur = Math.round(parseFloat(getStyle(obj,attr))*100);//转换成整数,并且四舍五入下 
    266.    //计算机在计算小数的时候往往是不准确的! 
    267.    } 
    268.    else
    269.    icur = parseInt(getStyle(obj,attr)); 
    270.    } 
    271.    var speed =0; 
    272.    speed = (itarget - icur)/8; 
    273.    speed = speed>0?Math.ceil(speed):Math.floor(speed); 
    274.    if(icur == itarget){ 
    275.    clearInterval(obj.timer); 
    276.    if(fn){ 
    277.     fn(); 
    278.    } 
    279.    } 
    280.    else
    281.     if(attr == 'opacity'){ 
    282.     obj.style.filter = 'alpha(opacity:'+(icur+speed)+')'
    283.     obj.style.opacity = (icur+speed)/100; 
    284.     } 
    285.     else
    286.     obj.style[attr] = icur+speed+'px'
    287.     }  
    288.    } 
    289.   },30); 
    290.  } 
    291.  function getStyle(obj,attr) 
    292.  { 
    293.   if(obj.currentStyle){ 
    294.   return obj.currentStyle[attr]; 
    295.   } 
    296.   else
    297.   return getComputedStyle(obj,false)[attr]; 
    298.   } 
    299.  } 
    300.  
    301. 9,多物体同时运动动画 
    302. window.onload = function(){ 
    303.  var li1 = document.getElementById('li1'); 
    304.  li1.onmouseover = function(){ 
    305.   startMov(li1,{width:201,height:200,opacity:100}); 
    306.  }; 
    307.  li1.onmouseout = function(){ 
    308.   startMov(li1,{width:200,height:100,opacity:30}); 
    309.  }; 
    310.  li1.timer = null
    311.  function startMov(obj,json,fn){//fn回调函数 
    312.   clearInterval(obj.timer);//执行动画之前清除动画 
    313.   var flag = true;//是否动画都完成了 
    314.   obj.timer = setInterval(function(){ 
    315.    for(var attr in json){ 
    316.    var icur = 0; 
    317.    if(attr == 'opacity'){ 
    318.    icur = Math.round(parseFloat(getStyle(obj,attr))*100);//转换成整数,并且四舍五入下 
    319.    //计算机在计算小数的时候往往是不准确的! 
    320.    } 
    321.    else
    322.    icur = parseInt(getStyle(obj,attr)); 
    323.    } 
    324.    var speed =0; 
    325.    speed = (json[attr] - icur)/8; 
    326.    speed = speed>0?Math.ceil(speed):Math.floor(speed); 
    327.    if(icur != json[attr]){ 
    328.    flag = false
    329.    } 
    330.    if(attr == 'opacity'){ 
    331.    obj.style.filter = 'alpha(opacity:'+(icur+speed)+')'
    332.    obj.style.opacity = (icur+speed)/100; 
    333.    } 
    334.    else
    335.    obj.style[attr] = icur+speed+'px'
    336.    } 
    337.    if(flag){ 
    338.    clearInterval(obj.timer); 
    339.    if(fn){ 
    340.     fn(); 
    341.    } 
    342.    } 
    343.   } 
    344.   },30); 
    345.  } 
    346.  function getStyle(obj,attr) 
    347.  { 
    348.   if(obj.currentStyle){ 
    349.   return obj.currentStyle[attr]; 
    350.   } 
    351.   else
    352.   return getComputedStyle(obj,false)[attr]; 
    353.   } 
    354.  } 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-69-4900-1.html
    相关热词搜索: js动画 js效果