4

I've created a scrolling script but i need it to loop when a set div gets to so many pixels to the left, here's my script to do it all but there's no such luck in getting it to work.

var scrollerwidth = 0;

$(window).load(function () {
    $('.scroller ul li').each(function() {
        scrollerwidth += ($(this).width() + 40);
    });
    $(".scrollerTwo").css({left: scrollerwidth});
    ulScrolls();
});

function ulScrolls(){
    $(".scroller, .scrollerTwo").animate({'left': '-=10px'}, 100);
    ulScrolls();
}

$(document).ready(function() {
    var scrollertwoleft = $(".scrollerTwo").offset().left; 
    scrollertwoleft = parseInt(scrollertwoleft, 10);
    scrollerwidth = parseInt(scrollerwidth, 10);
    if(scrollertwoleft <= 100){
        $(".scroller").css({left: scrollerwidth});
        alert("derp");
    }else{
        alert(scrollertwoleft);
    }
});

Also here's the html that it's putting out(note that the jQuery generates the left positions

<div id="homeSponsorsContent">
     <div class="scroller" style="left: -990px;">
         <ul>
            <li><img src="images/abplogo.png"></li>
            <li><img src="images/ansellogo.png"></li>
            <li><img src="images/balfourlogo.png"></li>
            <li><img src="images/beallogo.png"></li>
            <li><img src="images/nhslogo.png"></li>
            <li><img src="images/consortlogo.png"></li>
            <li><img src="images/brocklesbylogo.png"></li>
            <li><img src="images/bupalogo.png"></li>
            <li><img src="images/greenergylogo.png"></li>
            <li><img src="images/hullcitylogo.png"></li>
            <li><img src="images/wittlogo.png"></li>
            <li><img src="images/outredlogo.png"></li>
            <li><img src="images/omglogo.png"></li>
            <li><img src="images/manchesterlogo.png"></li>
            <li><img src="images/northumbrialogo.png"></li>
            <li><img src="images/mimaslogo.png"></li>
        </ul>
     </div>

     <div class="scrollerTwo" style="left: 2164px;">
         <ul>
            <li><img src="images/abplogo.png"></li>
            <li><img src="images/ansellogo.png"></li>
            <li><img src="images/balfourlogo.png"></li>
            <li><img src="images/beallogo.png"></li>
            <li><img src="images/nhslogo.png"></li>
            <li><img src="images/consortlogo.png"></li>
            <li><img src="images/brocklesbylogo.png"></li>
            <li><img src="images/bupalogo.png"></li>
            <li><img src="images/greenergylogo.png"></li>
            <li><img src="images/hullcitylogo.png"></li>
            <li><img src="images/wittlogo.png"></li>
            <li><img src="images/outredlogo.png"></li>
            <li><img src="images/omglogo.png"></li>
            <li><img src="images/manchesterlogo.png"></li>
            <li><img src="images/northumbrialogo.png"></li>
            <li><img src="images/mimaslogo.png"></li>
        </ul>
     </div>
</div>

live Demo

I'd appreciate any help on the matter, thank you

Matthew Artiman
  • 151
  • 1
  • 1
  • 13

1 Answers1

1
  • repeated call of ulScrolls should something like

    setTimeout(function() { ulScrolls() }, 120);
    

    to prevent call stack exceeding and to give time for animation to work.

  • checking for .scroller and .scrollerTwo CSS left value should be in ulScrolls.

  • li width set to "45px" for testing.

Fiddle

So in total code should look like:

var scrollerwidth = 0;

$(document).ready(function()
{
    $('.scroller ul li').each(function()
    {
        scrollerwidth += $(this).width() + 40;
    });
    $(".scrollerTwo").css({left: scrollerwidth});

    ulScrolls();
});

function ulScrolls()
{
    var scrollertwoleft = parseInt($(".scrollerTwo").css('left')); 
    if (scrollertwoleft <= -scrollerwidth)
    {
        $(".scrollerTwo").css({left: scrollerwidth});
    }
    else
    {
        var scrolleroneleft = parseInt($(".scroller").css('left'));
        if (scrolleroneleft <= -scrollerwidth)
        {
            $(".scroller").css({left: scrollerwidth});
        }
    }    
    $(".scroller, .scrollerTwo").animate({'left': '-=10px'}, 100);

    setTimeout(function() { ulScrolls(); }, 120);
}
Regent
  • 5,142
  • 3
  • 21
  • 35
  • I've just looked at this but it moves them altogether and only loops the full thing so it'll jump and not connect scroller one onto the end of scroller two when it hits that point It should start like this:- [ 1 ][ 2 ] Then when 2 hits left:0; it should be like this:- [ 2 ][ 1 ] Then finally like this again when 1 hits 0 again [ 1 ][ 2] So that it all snakes together, hope this makes sense – Matthew Artiman Aug 21 '14 at 09:37
  • 1
    @MatthewArtiman I rewrote answer to fit your fiddle and, I hope, your idea too. – Regent Aug 21 '14 at 10:13
  • That's great, that definitely loops now so thank you for your help so far but i am now encountering an error with the second div overlaying like this for some reason - [click me](http://oi58.tinypic.com/10zo6bs.jpg). The html and css is exactly the same as before, so i'm curious as to what's going on there – Matthew Artiman Aug 21 '14 at 10:26
  • 1
    @MatthewArtiman I set `li` width to "45px" in my fiddle - maybe you do not change it back? – Regent Aug 21 '14 at 10:28
  • Actually it was my fault, i accidentally set img to inline block inside and inline block so it caused a conflict. Thank you every much – Matthew Artiman Aug 21 '14 at 10:29
  • 1
    @MatthewArtiman you're welcome :) Just to mention: if `div.scroller` and `div.scrollerTwo` will have different width, use something like `scrollerWidthOne` and `scrollerWidthTwo` to keep it working properly. – Regent Aug 21 '14 at 10:33