Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cycle a single div in an element with jQuery

I am trying to see if the following is possible:

single rotating div

I want to be able to cycle a single div within an element continuously [so the start of the div is by the end of the same div as it cycles.]

This doesn't have to be an existing plugin. I would prefer to not clone the div if possible. The div's width will be set via javascript prior to cycle but might be adjusted in small amounts.

I would appreciate any ideas!

like image 684
BorisKourt Avatar asked Aug 30 '12 15:08

BorisKourt


1 Answers

jsBin demo

jQuery:

$('.scroller').each(function(){
  $(this).find('img').clone().appendTo($(this));
});

(function move(){
  $('.scroller').scrollLeft(0).stop().animate({scrollLeft:310},800,'linear',move);
})();

HTML:

  <div class="scroller">
    <img src="" alt="" />
  </div>

CSS:

.scroller{
  width:310px;
  height:80px;
  white-space:nowrap;
  word-spacing:-1em;
  overflow:hidden;
  margin:30px;
}
.scroller img{
  display:inline; 
}

It will make clones only once. Than my jQuery script will create a loop that will just play with the scrollLeft() element property.

N.B: this is just a plain example, you could make 310px be dynamically calculated, but that's another story, let's keep it simple.

like image 199
Roko C. Buljan Avatar answered Sep 21 '22 01:09

Roko C. Buljan