Skip to content Skip to sidebar Skip to footer

Scrolling Through Div With Javascript

I am trying to show one div at a time and scroll trough them over and over. I have modified a Fiddle I found and I got it working on fiddle, but for some reason I cant implement a

Solution 1:

I am guessing that you have a working fiddle but on your local test page its not working

your fiddle works because you got selected the default handlers from left handside dropdowns and its not working on your test page because your jquery handler is missing.

the reason is you are missing the document ready handler here:

$(function(){
    setInterval(go, 1000);
});

try replacing with this one and see if helps.

Solution 2:

I would probably simplyfy and would go with something like this: http://jsbin.com/osepim/1/

$(function() {

  // hide all and show first
  $(".boxes").hide().first().show();

  setInterval(function(){
    moveNext();
  }, 1000);

});

functionmoveNext() {
  var box = $(".boxes:visible"),
      nextBox = box.next();

  if(nextBox.length === 0)
    nextBox = $(".boxes:first");

  //hide all
  $(".boxes").hide();

  // show next
  nextBox.fadeIn();
}

Solution 3:

you need to wrap your javascripts code with the ready event $(document).ready() or use the short version $() and that will only excecute your codes when the page is finished loading so your codes should looks something like that

$(function(){
   functiongo() {
        var visibleBox = $('#container .boxes:visible');
        visibleBox.hide();
        var nextToShow = $(visibleBox).next('.boxes:hidden');
        if (nextToShow.length > 0) {
            nextToShow.show();
        } else {
            $('#container .boxes:hidden:first').show();
        }
        returnfalse;
    }​
        setInterval(go, 1000);​

});

Solution 4:

<!DOCTYPE html><head><metacharset="UTF-8"><linkrel="stylesheet"type="text/css"href="styles/styles.css" /><styletype="text/css">.boxes{display:none}</style></head><body><divid="container"><divclass="boxes">first box</div><divclass="boxes">second box</div><divclass="boxes">third box</div><divclass="boxes">forth box</div></div><scripttype="text/javascript"charset="utf-8"src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script><scripttype="text/javascript">functiongo() {
        var visibleBox = $('#container .boxes:visible'); // GET THE DIV
        visibleBox.hide();
        var nextToShow = $(visibleBox).next('.boxes:hidden');
        if (nextToShow.length > 0) { // SHOW NEXT ITEM
            nextToShow.show();
        } else {
            $('#container .boxes:hidden:first').show();
        }
        returnfalse;
    }​
    setInterval(go, 1000);​ // MS SECOND OF LOOP</script></body></html>

Post a Comment for "Scrolling Through Div With Javascript"