jquery - Is there any easier way to write a scrollTop animation? -
i'm wondering if there easier or better way of writing jquery animation triggers after point.
the code have @ moment looks like;
html
<div class="div"> <div id="box"></div> </div>
script
$(document).scroll(function() { var scrolled = $(document).scrolltop(); if(scrolled > 300) { $("#box").css("margin-left", "300px"); } else { $("#box").css("margin-left", "0px"); } });
also there away add different amount of time each animation take complete instead of add .css("transition", "all 2s");
tl; dr : yes, there multiple ways fire off transitions jquery, javascript, , css.
there few ways , have showcased them in following jsfiddle, can comment/uncomment code necessary see 2 different ways of using transitions.
1st option
jquery
$('#box').addclass("margin-left-300");
css:
transition: 2s ease;
which, when combined ability of removing class, allow css operate little interference possible jquery.
2nd option
jquery:
$('#box').animate({marginleft: "300px"}, 1000)
this allow jquery act upon transition when necessary , specify should happen in transition, , how long transition should last.
what's better?
honestly, i'm working on pretty big spa (single page application) requires lot of transitions , movements. made silly mistake of attempting use jquery animations, proved unreliable , choppy @ times.
the following article explains difference between css , javascript:
css animations vs jquery animations
in opinion, think best limit amount of jquery interference css transitions soon/much possible unless plan on using javascript library outperform native css.
Comments
Post a Comment