html - Jquery - Never ending slider, speed issue -
i've made never ending loop headlines sliding 1 side another. , when headline reach end put last in queue.
the problem headlines of different size in width. ends slider have different animation speeds. maybe i'm wrong setup code:
html:
<header> <div id="spotlight"> <span class="active">foo</span> <span>baaaar</span> <span>baaz</span> <span>baz</span> <span>qux</span> <span>quux</span> <span>corge</span> <span>grault</span> <span>garply</span> <span>waldo</span> </div> </header>
css:
body { background: #000; } header { border-bottom: 8px solid white; height: 300px; margin-bottom: 4%; overflow: hidden; position: relative; } header #spotlight { left: 0; position: absolute; width: 1100%; } header #spotlight span { color: white; display: block; float: left; font-size: 6em; font-weight: 900; margin-top: 1%; text-transform: uppercase; } header #spotlight span { margin-left: 30px; } header #spotlight span:not(:last-child):after { content: "•"; margin-left: 30px; }
js:
$(window).load(function(){ animate_spotlight(); }); function animate_spotlight(){ var $current_spot = $('#spotlight span.active') var pos = $current_spot.next().position().left; $('#spotlight').stop().animate({ left : '-' + pos + 'px' }, {easing : 'linear', duration : 3000, complete : function(){ $current_spot.next().addclass('active'); // fetch new object $current_spot.appendto('#spotlight'); // put old object last in #spotlight elem. $('#spotlight').css({left : 0 + 'px'}) // reset position $current_spot.removeclass('active'); // removes active class of old elem. animate_spotlight(); // loop }}); }
js fiddle http://jsfiddle.net/7bmaf/2/
i fixed adding variable , used source calculating speed.
var divide_to_get_time = (pos / 10000) * 50000;
and put variable in duration.
final code:
function animate_spotlight(){ var $current_spot = $('#spotlight span.active') var pos = $current_spot.next().position().left; var divide_to_get_time = (pos / 10000) * 50000; $('#spotlight').stop().animate({ left : '-' + pos + 'px' }, {easing : 'linear', duration : divide_to_get_time, complete : function(){ $current_spot.next().addclass('active'); $current_spot.appendto('#spotlight'); $('#spotlight').css({left : 0 + 'px'}) $current_spot.removeclass('active'); animate_spotlight(); }}); }
Comments
Post a Comment