JavaScript animation

前端 未结 7 1987
忘了有多久
忘了有多久 2020-11-29 09:47

I am trying to animate a div moving 200px horizontally in JavaScript.

The code below makes it jump the pixels, but is there a way to make i

相关标签:
7条回答
  • 2020-11-29 09:58

    You can easily do this through CSS3-Transition :

    #challengeOneImageJavascript {
        -webkit-transition: left .2s;
           -moz-transition: left .2s;
             -o-transition: left .2s;
                transition: left .2s;
    }
    

    Though, it is not supported by IE9 and earlier browser versions.

    0 讨论(0)
  • 2020-11-29 09:59

    CustomAnimation is a small libary for animating html elements which is written in pure js.You can use this libary.

    0 讨论(0)
  • 2020-11-29 10:10

    With JavaScript, you will have to use setInterval function or this is how it can be done in jQuery:

    $('#challengeOneImageJavascript').animate({left: '=-5'});
    

    Adust value (5) as per your needs as well as direction via =- or =+

    With Vanilla JavaScript:

    var interval;
    var animate = function(id, direction, value, end, speed){
        var div = document.getElementById(id);
        interval = setInterval(function() {
           if (+(div.style) === end) {
              clearInterval(interval);
              return false;
           }
           div.style[direction] += value; // or -= as per your needs
        }, speed);
    }
    

    And you can use it like:

    animate('challengeOneImageJavascript', 'left', 5, 500, 200);
    

    To stop animation any time, you would do:

    clearInterval(interval);
    

    Note: This just a very quick way to do it to give you an idea.

    0 讨论(0)
  • 2020-11-29 10:10

    Simplest way via css.

    https://jsfiddle.net/pablodarde/5hc6x3r4/

    translate3d uses hardware acceleration running on GPU. http://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-css

    HTML

    <div class="movingBox"></div>
    

    CSS

    .movingBox {
      width: 100px;
      height: 40px;
      background: #999;
      transform: translate3d(0,0,0);
      transition: all 0.5s;
    }
    
    .moving {
      transform: translate3d(200px,0,0);
      background: #f00;
    }
    

    JavaScript

    const box = document.getElementsByClassName('movingBox')[0];
    
    setTimeout(() => {
        box.className += ' moving';
    }, 1000);
    
    0 讨论(0)
  • 2020-11-29 10:15

    Here is a basic animation setup:

    function animate(elem,style,unit,from,to,time) {
        if( !elem) return;
        var start = new Date().getTime(),
            timer = setInterval(function() {
                var step = Math.min(1,(new Date().getTime()-start)/time);
                elem.style[style] = (from+step*(to-from))+unit;
                if( step == 1) clearInterval(timer);
            },25);
        elem.style[style] = from+unit;
    }
    

    To use:

    animate(
        document.getElementById('challengeOneImageJavascript'),
        "left","px",0,200,1000
    );
    

    This example will animate the given element to slide linearly from 0px to 200px over a time of 1 second (1000 ms).

    0 讨论(0)
  • 2020-11-29 10:15

    I did a ton of research, and I finally learned how to do it really well.

    I like to place my program in a window.onload function, that way it dosn't run the code until the page has finished loading.

    To do the animation, make a function(I'll call it the draw function) and call it what ever you want except reserved words, then at the very end of the draw function call the requestAnimationFrame function and give it the name of the function to be called next frame.

    Before the requestAnimationFrame function can be used it must be declared.

    See the code below:

    window.onload = function() {
      function draw() { //  declare animation function
        context.fillStyle = "white";
        context.fillRect(0, 0, 400, 400);
        requestAnimationFrame(draw); // make another frame
      }
      var requestAnimationFrame = // declare the 
        window.requestAnimationFrame || // requestAnimationFrame
        window.mozRequestAnimationFrame || // function
        window.webkitRequestAnimationFrame ||
        window.msRequestAnimationFrame;
      draw(); // call draw function
    }

    Note: Nothing after the line that calls the draw function will run, so you need to put everything you want to run before the line that calls the draw function.

    0 讨论(0)
提交回复
热议问题