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:

  1. precalculate $(document).width() if horizontal scroll appears, width change in next iteration

  2. remove width have set scroll width long content - , have give white-space:nowrap keep text in line.

  3. in animate use width of scroll text 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

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -