Carousel using Dojo
2009 January 13
For a project i had to make a carousel and i was using Dojo throughout my project. After about two weeks, i hav made one.
UPDATE:
U can c this live at http://boomroom.com/library/…the look has changed, the code remains da same
Final Output :

Here is da code :
html code :
<div id="carouselwrap"> <div id="carouselprev" class="traversecontrol"><img src="/img/blank.gif"/></div> <!--image of the previous arrow--> <div id="carousel"><!--start of the carousel--> <div id="carouselscroll" ><!--start of the carouselscroll--> <em> <div class="carouselscrollbox"><!--start of the carouselscrollbox block--> <div class="carouselscrollerimg"> <img src="Your image here" /><!--this div contains the image path --></div> <div class="soundsettitle bmrm_text"> Lorem Ipsum</div> <div class="artisttitle bmrm_text"> Lorem Ipsum</div> <div class="soundsetdesc bmrm_text"> - Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</div> </div> </em><em><em><!--end of the carouselscrollbox block--></em></em></div> <div id="carouselnext" class="traversecontrol"><img src="/img/blank.gif"/></div> <!--image of the next arrow--></div> <!--repeat the div block wid class 'carouselscrollbox'(in a for loop) as many times to get as many records in da carousel.-->
Javascript code:First step : dojo.require("dojo.fx");
var animationMethod = "chain"; var animG, offsetW, offsetH = null; var newLeft = 0; var boxMixin = {duration: 1000}; var carouselscroller ; //attach events on load dojo.addOnLoad(function() { var prev = dojo.byId('carouselprev'); var next = dojo.byId('carouselnext'); carouselscroller = dojo.byId('carouselscroll'); dojo.connect(prev,"onclick",null,"slideLeft"); dojo.connect(next,"onclick",null,"slideRight"); }); //sliding to the right function slideRight() { var difference = dojo.byId('carousel').offsetWidth-dojo.byId('carouselscroll').offsetWidth; if(newLeft>difference){ newLeft = newLeft - 200; var style = carouselscroller.style; var anim1 = dojo.animateProperty({ node: carouselscroller, duration: boxMixin.duration/2, properties: { left: { end: newLeft, unit:"px" } } }); animG = dojo.fx[animationMethod]([anim1]).play(); } } //sliding to the left function slideLeft() { if(newLeft<0) { newLeft = newLeft + 200; var style = carouselscroller.style; var anim1 = dojo.animateProperty({ node: carouselscroller, duration: boxMixin.duration/2, properties: { //width: { /* start: boxMixin.startWidth, */ end: boxMixin.endWidth, unit:"px" }, left: { end: newLeft, unit:"px" } } }); animG = dojo.fx[animationMethod]([anim1]).play(); } } <pre>CSS Code:
/*soundsets traversal [carousel]*/ #carouselwrap{width:700px;margin:35px 0px} #carousel{ width: 600px; height: 230px; overflow: hidden; float:left; position:relative; } #carouselscroll{ float:left; width:2000px !important; position:absolute; left:0px; } #carouselprev{background:url('/*image left arrow*/') no-repeat;} #carouselnext{background:url('/*image right arrow*/') no-repeat;} .traversecontrol{float:left;width:40px;height:45px;margin:70px 0px} #carouselprev:hover,#carouselnext:hover{cursor:pointer} .carouselscrollbox{ float:left; border:1px solid #eee; margin:9px; background:#f6f5f5; width:180px; text-align:left; padding-bottom:10px; } .carouselscrollerimg img{width:140px;height:80px;margin:3px 1px } <pre> <pre>U can change the css accordingly.This is just my way of doing it.








hey anoop…
good if you can post a working example ….
thanks,
arun
The project is now in alpha.So i cant giv the URL. But I can assure you it’s working
Hello !
i’m a student in france in lyon.
Can you post a complete source code of your work.
thanks
mamadi
@mamadi
thnx for stopping by
As far as the carousel is concerned…the code posted works fine.And that is what i hav used too.
U just hav to put the div with class =”CAROUSELSCROLLBOX” in a for loop.
Tell me where u r stuck.
This code (after adding is var carouselscroller = dojo.byId(‘carouselscroll’); to both slide function ) will work like a charm.
Thanks, so much Anoop.
@Jason d
Thnx for the feedback.
One can also declare var carouselscroller above with all other initializations which i missed out i guess.Thnx for pointing out.