javascript - How to animate endless loop using jquery? -
i have been trying using jquery animate running text. can't seems run in endless loop. runs 1 time only..
/* js: */ $(document).ready(function(){ function scroll() { $('.scroll').animate({ right: $(document).width() }, 8000, scroll); } scroll(); }); /* css: */ .scroll { position: absolute; right: -200px; width: 200px; } <!-- html: --> <div class="scroll">this text scrollin'!</div> this demo: https://jsfiddle.net/y9hvr9fa/1/
do guys know how fix it?
so did:
precalculate
$(document).width()if horizontal scroll appears, width change in next iterationremove
widthhave setscrollwidth long content - , have givewhite-space:nowrapkeep text in line.in
animateuse width ofscrolltext using$('.scroll').outerwidth()
see demo below , update fiddle here
$(document).ready(function() { // initialize var $width = $(document).width(); var $scrollwidth = $('.scroll').outerwidth(); $('.scroll').css({'right': -$scrollwidth + 'px'}); // animate function scroll() { $('.scroll').animate({ right: $width }, 8000, 'linear', function() { $('.scroll').css({'right': -$scrollwidth + 'px'}); scroll(); }); } scroll(); }); body { overflow: hidden; } .scroll { position: absolute; white-space: nowrap; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="scroll">this text scrollin'!</div> let me know feedback on this, thanks!
Comments
Post a Comment