javascript动画基础代码
<html> <head> <title></title> <style type="text/css"> #line { height: 40px; width: 100%; background: #ccc; position: relative; top: 45%; } #box { width: 100px; height: 100px; background: blue; position: absolute; top: -30px; left: 0; } </style> </head> <body> <div id="line"> <div id='box'></div> </div> <script type="text/javascript"> function animate(opts) { opts.delay = opts.delay || 15; opts.duration = opts.duration || 1000; opts.effect = opts.effect || linear; var start = new Date; if ('requestAnimationFrame' in window) { function draw() { var now = new Date; var timePassed = now - start; var progress = (now - start) / opts.duration; if (progress < 1) { var delta = opts.effect(progress); opts.step(delta); setTimeout(function() { requestAnimationFrame(draw); }, opts.delay); } } draw(); } else { var id = setInterval(function() { var timePassed = new Date - start; var progress = timePassed / opts.duration; if (progress > 1) progress = 1; var delta = opts.effect(progress); opts.step(delta); if (progress === 1) { clearInterval(id); } }, opts.delay); } } function linear(progress) { return progress; } function quad(progress) { return Math.pow(progress, 2); } function circ(progress) { return 1 - Math.sin(Math.acos(progress)) } function back(progress, x) { x = x || 1.5; return Math.pow(progress, 2) * ((x + 1) * progress - x) } function bounce(progress) { for (var a = 0, b = 1, result; 1; a += b, b /= 2) { if (progress >= (7 - 4 * a) / 11) { return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2) + Math.pow(b, 2) } } } function elastic(progress, x) { x = x || 1.5; return Math.pow(2, 10 * (progress - 1)) * Math.cos(20 * Math.PI * x / 3 * progress) } function makeEaseOut(delta) { return function(progress) { return 1 - delta(1 - progress) } } function makeEaseInOut(delta) { return function(progress) { if (progress < .5) return delta(2 * progress) / 2 else return (2 - delta(2 * (1 - progress))) / 2 } } var el = document.getElementById('box'); animate({ duration: 3000, effect: makeEaseInOut(bounce), step: function(delta) { el.style.left = 1000 * delta + 'px'; } }); </script> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述