当前位置:Gxlcms > JavaScript > JS div匀速移动动画与变速移动动画代码实例

JS div匀速移动动画与变速移动动画代码实例

时间:2021-07-01 10:21:17 帮助过:2人阅读

1.匀速移动代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>title</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. div {
  12. margin-top: 20px;
  13. width: 200px;
  14. height: 100px;
  15. background-color: green;
  16. position: absolute;
  17. left: 0;
  18. top: 0;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <input type="button" value="移动到400px" id="btn1"/>
  24. <input type="button" value="移动到800px" id="btn2"/>
  25. <div id="dv">
  26. <script src="common.js"></script>
  27. <script>
  28. //点击按钮移动div
  29. my$("btn1").onclick = function () {
  30. animate(my$("dv"), 400);
  31. };
  32. my$("btn2").onclick = function () {
  33. animate(my$("dv"), 800);
  34. };
  35. //匀速动画
  36. function animate(element, target) {
  37. //清理定时器
  38. clearInterval(element.timeId);
  39. element.timeId = setInterval(function () {
  40. //获取元素的当前位置
  41. var current = element.offsetLeft;
  42. //移动的步数
  43. var step = 10;
  44. step = target > current ? step : -step;
  45. current += step;
  46. if (Math.abs(current - target) > Math.abs(step)) {
  47. element.style.left = current + "px";
  48. } else {
  49. clearInterval(element.timeId);
  50. element.style.left = target + "px";
  51. }
  52. }, 20);
  53. }
  54. </script>
  55. </div>
  56. </body>
  57. </html>

2.变速移动代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>title</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. div {
  12. margin-top: 20px;
  13. width: 200px;
  14. height: 100px;
  15. background-color: green;
  16. position: absolute;
  17. left: 0;
  18. top: 0;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <input type="button" value="移动到400px" id="btn1"/>
  24. <input type="button" value="移动到800px" id="btn2"/>
  25. <div id="dv">
  26. <script src="common.js"></script>
  27. <script>
  28. //点击按钮移动div
  29. my$("btn1").onclick = function () {
  30. animate(my$("dv"), 400);
  31. };
  32. my$("btn2").onclick = function () {
  33. animate(my$("dv"), 800);
  34. };
  35. //变速动画
  36. function animate(element, target) {
  37. //清理定时器
  38. clearInterval(element.timeId);
  39. element.timeId = setInterval(function () {
  40. //获取元素的当前位置
  41. var current = element.offsetLeft;
  42. //移动的步数
  43. var step = (target-current)/10;
  44. step = step>0?Math.ceil(step):Math.floor(step);
  45. current += step;
  46. element.style.left = current + "px";
  47. if(current==target) {
  48. //清理定时器
  49. clearInterval(element.timeId);
  50. }
  51. }, 20);
  52. }
  53. </script>
  54. </div>
  55. </body>
  56. </html>

common.js

  1. /**
  2. * 获取指定标签对象
  3. * @param id 标签的id属性值
  4. * @returns {Element}根据id属性值返回指定标签对象
  5. */
  6. function my$(id) {
  7. return document.getElementById(id);
  8. }

以上所述是小编给大家介绍的JS div匀速移动动画与变速移动动画详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行