js动画

来源:互联网 发布:unity3d lerp函数 编辑:程序博客网 时间:2024/05/17 02:32
  1. 简单动画(原理都是设置一个固定speed,都是匀速运动)
    • 速度动画
    • 透明度动画
    • 注意:注意清除动画
  2. 缓冲动画(speed是一个变的值,但是注意取整)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>速度动画</title>
  6. <style type="text/css">
  7. *{margin:0; padding:0;}
  8. a{text-decoration:none;color:#000; }
  9. .wrap{text-align:right;width:100px; height:100px; position: relative; left:-100px;}
  10. .show{width:100px; height:100px; background-color:#31C37C; }
  11. #t{width:50px;height:50px;text-align:center;line-height:50px; background-color:#ff0;position:absolute;margin-top:-75px;}
  12. </style>
  13. <script type="text/javascript">
  14. var timer=null;
  15. // 抽象动画
  16. function translate (target) {
  17. clearInterval(timer);
  18. timer=setInterval(function () {
  19. var speed=(target-con.offsetLeft)/20;(缓冲动画)
  20. speed=speed>0?Math.ceil(speed):Math.floor(speed);
  21. if(con.offsetLeft==(target )){
  22. clearInterval(timer);
  23. }else{
  24. con.style.left=(con.offsetLeft+speed)+'px';
  25. }
  26. }, 50);
  27. }
  28. window.onload=function () {
  29. a=document.getElementById('t');
  30. con=document.getElementById('con');
  31. a.onclick=function () {
  32. if(t.innerHTML.indexOf('分享')>-1){
  33. t.innerHTML='关闭';
  34. translate(0);
  35. }else{
  36. t.innerHTML='分享';
  37. translate(-100);
  38. }
  39. }
  40. }
  41. </script>
  42. </head>
  43. <body>
  44. <div class="wrap" id="con">
  45. <div class="show" >
  46. hello world;
  47. </div>&nbsp;
  48. <a href="javascript:;" id="t">分享</a>
  49. </div>
  50. </body>
  51. </html>
3,多物体动画(要出入目标,多物体动画不能够公用)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style type="text/css">
  7. *{margin:0; padding:0;}
  8. li{list-style:none;}
  9. li{width:200px; height:200px; background-color:#ff0; margin:10px;}
  10. </style>
  11. <script type="text/javascript">
  12. function startMove(obj,target){
  13. clearInterval(obj.timer);
  14. obj.timer=setInterval(function () {
  15. var speed=(target-obj.offsetWidth)/8;
  16. speed=speed>0?Math.ceil(speed):Math.floor(speed);
  17. if(target==obj.offsetWidth){
  18. clearInterval(obj.timer);
  19. }else{
  20. obj.style.width=obj.offsetWidth+speed+"px";
  21. }
  22. }, 30)
  23. }
  24. window.onload=function () {
  25. var lis=document.getElementsByTagName('li');
  26. for (var i = 0; i < lis.length; i++) {
  27. lis[i].timer=null; //必须给每个都加timer
  28. lis[i].onmouseover=function () {
  29. startMove(this,400); //得传入对象
  30. }
  31. lis[i].onmouseleave=function () {
  32. startMove(this,200);
  33. }
  34. };
  35. }
  36. </script>
  37. </head>
  38. <body>
  39. <ul>
  40. <li></li>
  41. <li></li>
  42. <li></li>
  43. <li></li>
  44. </ul>
  45. </body>
  46. </html>
4,注意事项和框架完善:
offsetWidth=clientWidth+border+margin;
clientWidth=物体的实际值+padding
所以对于offset**前缀的一般会使用:
  1. // 获取样式
  2. function getStyle (obj,attr) {
  3. if(obj.currentStyle){ //ie
  4. return obj.currentStyle[attr];
  5. } else{
  6. return getComputedStyle(obj,false)[attr];
  7. }
  8. }
5,链式动画:就是多传个参数fn
  1. function startMove(obj,attr,target,fn){
  2. clearInterval(obj.timer);
  3. obj.timer=setInterval(function () {
  4. var cur;
  5. if(attr=='opacity'){
  6. cur=Math.round(parseFloat(getStyle(obj,attr))*100);
  7. }else{
  8. cur=parseInt(getStyle(obj,attr));
  9. }
  10. var speed=(target-cur)/8;
  11. speed=speed>0?Math.ceil(speed):Math.floor(speed);
  12. if(target==cur){
  13. if(fn){
  14. fn();
  15. }else{
  16. clearInterval(obj.timer);
  17. }
  18. }else{
  19. if(attr=='opacity'){
  20. obj.style.filter='alpha(opacity='+cur+speed+')';
  21. obj.style.opacity=(cur+speed)/100;
  22. }
  23. obj.style[attr]=cur+speed+"px";
  24. }
  25. }, 30)
  26. }
6,同时运动:(使用了json,json是存贮key-value 值的,遍历用for-in的形式


0 0