匀速/缓冲
    匀速运动

    1. <head>
    2. <meta charset="UTF-8">
    3. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    4. <title>Document</title>
    5. <style>
    6. .move{
    7. width: 100px;
    8. height: 100px;
    9. position: absolute;
    10. background: red;
    11. }
    12. </style>
    13. </head>
    14. <body>
    15. <div class="move"></div>
    16. <script>
    17. var div = document.getElementsByClassName('move')[0];
    18. // 匀速运动
    19. function move (dom,target){
    20. var time = null;
    21. clearInterval(time);
    22. var iSpeed = target - dom.offsetLeft > 0 ? 7 : -7;
    23. time = setInterval(() => {
    24. if(Math.abs(target - dom.offsetLeft) < Math.abs(iSpeed)){
    25. clearInterval(time)
    26. dom.style.left = target + 'px';
    27. }else{
    28. dom.style.left = dom.offsetLeft + iSpeed + 'px'
    29. }
    30. }, 30);
    31. }
    32. move(div,500)
    33. </script>
    34. </body>

    缓冲运动

    1. // 物体的速度,距离目标点越近,就越小,当到达目标点时,速度减小为0
    2. // 缓冲运动的原理:速度由距离决定,速度=(目标值-当前值)/缩放比例
    3. function startMove (dom ,target){
    4. var timer = null,iSpeed = null;
    5. clearInterval(timer);
    6. timer = setInterval(() => {
    7. iSpeed = (target - dom.offsetLeft) / 7;
    8. iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //ceil 向上一位取整
    9. if(dom.offsetLeft == target){
    10. clearInterval(timer);
    11. }else{
    12. dom.style.left = dom.offsetLeft + iSpeed + 'px';
    13. }
    14. }, (30));
    15. }

    透明度

    1. var div = document.getElementsByClassName('move')[0];
    2. var timer = null;
    3. function startMove (dom, target) {
    4. clearInterval(timer);
    5. var iSpeed = null, iCur = null;
    6. timer = setInterval(function () {
    7. iCur = parseFloat( getStyle(dom, 'opacity') ) * 100;
    8. iSpeed = (target - iCur) / 7;
    9. iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    10. if (iCur == target) {
    11. console.log(1)
    12. clearInterval(timer);
    13. }else {
    14. // 0 - 1 iCur * 100 target * 100 iSpeed * 100 iCur + iSpeed 0 - 100 / 100
    15. dom.style.opacity = (iCur + iSpeed) / 100;
    16. }
    17. }, 30)
    18. }
    19. div.onclick = function(){
    20. startMove(this,50)
    21. }

    多物体不同值运动

    1. function startMove(dom , attr, target){
    2. var timer = null,iSpeed = null,iCur = null;
    3. clearInterval(timer);
    4. dom.timer = setInterval(function(){
    5. if(attr == 'opacity'){
    6. iCur = parseFloat(getStyle(dom,attr))*100
    7. }else{
    8. iCur = parseInt(getStyle(dom,attr))
    9. }
    10. iSpeed = (target - iCur) /7 ;
    11. iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    12. if(iCur == target){
    13. clearInterval(dom.timer);
    14. }
    15. if(attr == 'opacity'){
    16. dom.style.opacity = (iSpeed+iCur)/100;
    17. }else{
    18. dom.style[attr]=iCur+iSpeed +'px'
    19. }
    20. })
    21. }
    22. function getStyle(dom,attr){
    23. if(window.getComputedStyle){
    24. return window.getComputedStyle(dom,null)[attr];
    25. }else{
    26. return dom.currentStyle[attr];
    27. }
    28. }
    29. div[0].onclick = function(){
    30. startMove(div[0],'width',500)
    31. };
    32. div[1].onclick = function(){
    33. startMove(div[1],'height',500)
    34. };
    35. div[2].onclick = function(){
    36. startMove(div[2],'opacity',50)
    37. };

    多物体多值运动+回调

    1. <div id='topDiv'></div>
    2. <div id="bottomDiv"></div>
    3. <script>
    4. // js div
    5. // width -> 100 400 height 100 400 left 0 200 top 200 300 opacity 1 0.5
    6. // {width: 400, height: 400, left: 200, top: 300, opacity: 50}
    7. var oTopDiv = document.getElementById('topDiv');
    8. var oBottomDiv = document.getElementById('bottomDiv');
    9. oTopDiv.onclick = function () {
    10. startMove(this, {width: 400, height: 400, left: 200, top: 300, opacity: 50}, function () {
    11. startMove(oBottomDiv, {width: 400, height: 400, left: 200, top: 300, opacity: 50}, function () {
    12. alert('over');
    13. })
    14. })
    15. }
    16. function getStyle (dom, attr) {
    17. if (window.getComputedStyle) {
    18. return window.getComputedStyle(dom, null)[attr];
    19. }else {
    20. return dom.currentStyle[attr];
    21. }
    22. }
    23. function startMove (dom, attrObj, callback) {
    24. clearInterval(dom.timer);
    25. var iSpeed = null, iCur = null;
    26. dom.timer = setInterval(function () {
    27. var bStop = true;
    28. for (var attr in attrObj) {
    29. // 'width' 'height'
    30. if (attr == 'opacity') {
    31. iCur = parseFloat( getStyle(dom, attr) ) * 100;
    32. }else {
    33. iCur = parseInt( getStyle(dom, attr) );
    34. }
    35. iSpeed = (attrObj[attr] - iCur) / 7;
    36. iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    37. if (attr == 'opacity') {
    38. dom.style.opacity = (iCur + iSpeed) / 100;
    39. }else {
    40. dom.style[attr] = iCur + iSpeed + 'px';
    41. }
    42. if (iCur != attrObj[attr]) {
    43. bStop = false;
    44. }
    45. }
    46. if (bStop) {
    47. clearInterval(dom.timer);
    48. typeof callback == 'function' && callback(); // 此处&& 当前面为true就会执行回调函数
    49. }
    50. }, 30);
    51. }
    52. </script>