0

I had a a question on stackoverflow and I got multiple answers to animate a font from font awesome (considered like normal text), the first was this:

$(document).ready(function(){
    $("#plane-icon").click(function(){
        $(this).animate({
            left:'180px',
            top:'-20px',
            fontSize:'20px'
        },2000);
        $(this).animate({
            left:'0px',
            top:'180px',
            fontSize:'100px'
        },0);
        $(this).animate({
            left:'0px',
            top:'80px',
        },1000);
    });
});

.icon-wrap {
    width:180px;
    height:180px;
    font-size:100px;
    cursor:pointer;
    position:relative;
    overflow:hidden;
}

#plane-icon {
    position:absolute;
    top:80px;
    left:0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="icon-wrap">
    <span id="plane-icon">🚀</span>
</div>

the second answer, with the same result, was this:

$(document).ready(function(){
    $('body').on('webkitAnimationEnd oanimationend oAnimationEnd msAnimationEnd animationend',
        function(){
            $('#plane-icon').removeClass('launched');
        }
    );
    $('#plane-icon').click(function(){
        $(this).addClass('launched');
    });
});
.icon-wrap {
    width:180px;
    height:180px;
    font-size:100px;
    overflow:hidden;
}

#plane-icon {
    display:block;
    cursor:pointer;
    transform:translate(0px,80px) scale(1);
}

#plane-icon.launched {
    animation-name:rocket;
    animation-duration:3s;
    cursor:default;
}


@keyframes rocket {
    0% {
        transform:translate(0px,80px) scale(1);
    }
    66% {
        transform:translate(120px,-80px) scale(0.1);
    }
    67% {
        transform:translate(120px,180px) scale(0.1);
    }
    68% {
        transform:translate(0px,180px) scale(1);
     }
     100% {
         transform:translate(0px,80px) scale(1);
     }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="icon-wrap">
    <span id="plane-icon">🚀</span>
</div>

Which of these 2 will require less processing power?

5
  • 1
    You must use css when you can instead of javascript to make animations or other things that CSS can make natively. Although there are more factors Commented Feb 11, 2016 at 15:30
  • 3
    Easily the CSS solution. The animations can be hardware accelerated, which means it's mostly in the GPU's hands and you don't have to perform all of that extra object boxing done in the first example. You could speed up the first one by caching $(this) (var $this = $(this)) but the CSS one will probably still end up being faster. When in doubt: profile. Commented Feb 11, 2016 at 15:30
  • 1
    thx guys! love this site! Commented Feb 11, 2016 at 15:32
  • Check this too...stackoverflow.com/questions/27094587/… Commented Feb 11, 2016 at 15:46
  • Caching $(this) in this case would have practically zero effect performance-wise. Commented Feb 11, 2016 at 16:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.