Creando múltiples carruseles con jQuery

This post was published 9 years ago. It may be exremely outdated.

jQuery - Carrusel

En vista de que algunos tenían problemas para mostrar varios carruseles con aquel tutorial que escribí sobre cómo crear un carrusel con jQuery, hoy voy a explicar cómo crear varios carruseles, a modo de continuación del anterior tutorial, así que os recomiendo leerlo antes de continuar con éste.

Bien, partiremos del código que usé en la demo (que podéis descargar aquí), y tendremos que cambiar tanto el CSS como el Javascript que hay incrustado en el HTML, dejando intacto los dos primeros códigos Javascript que llamamos (jQuery y Stepcarousel), en definitiva, que partiremos del siguiente código:

<br />
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br />
&lt;title&gt;Carousel con jQuery&lt;/title&gt;<br />
&lt;script src=&quot;jquery.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />
&lt;script src=&quot;carousel.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />
&lt;script type=&quot;text/javascript&quot;&gt;<br />
stepcarousel.setup({<br />
galleryid: 'carousel', //id of carousel DIV<br />
beltclass: 'belt', //class of inner &quot;belt&quot; DIV containing all the panel DIVs<br />
panelclass: 'panel', //class of panel DIVs each holding content<br />
autostep: {enable:true, moveby:1, pause:3000},<br />
panelbehavior: {speed:500, wraparound:true, persist:true},<br />
statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels<br />
contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']<br />
})<br />
&lt;/script&gt;<br />
&lt;style type=&quot;text/css&quot;&gt;<br />
/* Carousel */</p>
<p>#carousel {<br />
position: relative; /* Necesario */<br />
overflow: hidden; /* Necesario */<br />
height: 250px;<br />
margin-left:35px;<br />
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;<br />
}</p>
<p>#carousel .belt {<br />
position: absolute; /* Necesario */<br />
left: 0;<br />
top: 0;<br />
margin:0 10px 10px 0;<br />
}</p>
<p>#carousel .panel {<br />
width:265px;<br />
float: left; /* Necesario */<br />
overflow: hidden;<br />
margin: 15px;<br />
padding:7px;<br />
border:1px solid #5B5B5B;<br />
background:#383838 url(carousel-panel-bg.png) bottom left repeat-x;<br />
}</p>
<p>#carousel .panel .panel-text {<br />
padding-top:5px;<br />
font-size:13px;<br />
font-family:Verdana, Geneva, sans-serif;<br />
color:#FFF;<br />
}</p>
<p>#carousel .panel .panel-text a {<br />
color:#CCC;<br />
text-decoration:none;<br />
}</p>
<p>#carousel .panel .panel-text a:hover {<br />
color:#FFF;<br />
text-decoration:underline;<br />
}</p>
<p>/* Botones del carousel */</p>
<p>.button-prev {<br />
height:250px;<br />
width:35px;<br />
float:left;<br />
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;<br />
-moz-border-radius:10px 0 0 10px;<br />
}</p>
<p>.button-prev a {<br />
display:block;<br />
padding:5px;<br />
margin-top:105px;<br />
}</p>
<p>.button-next {<br />
height:250px;<br />
width:35px;<br />
float:right;<br />
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;<br />
-moz-border-radius:0 10px 10px 0;<br />
}</p>
<p>.button-next a {<br />
display:block;<br />
padding:5px;<br />
margin-top:105px;<br />
}</p>
<p>a img {<br />
border:none;<br />
}<br />
&lt;/style&gt;<br />
&lt;/head&gt;</p>
<p>&lt;body&gt;<br />
&lt;div class=&quot;button-next&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', 1)&quot;&gt;&lt;img src=&quot;arrow_right.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-prev&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', -1)&quot;&gt;&lt;img src=&quot;arrow_left.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div id=&quot;carousel&quot; class=&quot;stepcarousel&quot;&gt;</p>
<p>&lt;div class=&quot;belt&quot;&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img1.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Este carousel es una demostraci&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img2.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;de un tutorial de &lt;a href=&quot;http://sumolari.com&quot;&gt;Sumolari.com&lt;/a&gt;.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img3.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Puedes ver el tutorial en cuesti&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img4.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;en &lt;a href=&quot;http://sumolari.com/?p=1708&quot;&gt;este art&amp;iacute;culo&lt;/a&gt; del blog.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img5.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img6.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Amet sit dolor ipsum lorem.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img7.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Bla bla bla bla bla bla bla&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img8.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Ble ble ble ble ble ble ble&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />

Vale, primero nos centraremos en el CSS, que es la parte más fácil de adaptar, lo que tenemos que hacer es cambiar todas las líneas que definan un estilo para el elemento con ID carousel, ya que al haber varios carruseles necesitaremos un estilo común a ellos, y no es lo mejor usar el mismo ID para dos elementos (además de que no sería compatible con Stepcarousel), así que básicamente lo que tenemos que hacer es cambiar #carousel por .carousel, dejando el código CSS así:

/* Carousel */
.carousel {
position: relative; /* Necesario */
overflow: hidden; /* Necesario */
height: 250px;
margin-left:35px;
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;
}
.carousel .belt {
position: absolute; /* Necesario */
left: 0;
top: 0;
margin:0 10px 10px 0;
}
.carousel .panel {
width:265px;
float: left; /* Necesario */
overflow: hidden;
margin: 15px;
padding:7px;
border:1px solid #5B5B5B;
background:#383838 url(carousel-panel-bg.png) bottom left repeat-x;
}
.carousel .panel .panel-text {
padding-top:5px;
font-size:13px;
font-family:Verdana, Geneva, sans-serif;
color:#FFF;
}
.carousel .panel .panel-text a {
color:#CCC;
text-decoration:none;
}
.carousel .panel .panel-text a:hover {
color:#FFF;
text-decoration:underline;
}
/* Botones del carousel */
.button-prev {
height:250px;
width:35px;
float:left;
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;
-moz-border-radius:10px 0 0 10px;
}
.button-prev a {
display:block;
padding:5px;
margin-top:105px;
}
.button-next {
height:250px;
width:35px;
float:right;
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;
-moz-border-radius:0 10px 10px 0;
}
.button-next a {
display:block;
padding:5px;
margin-top:105px;
}
a img {
border:none;
}

Como os podréis imaginar, ahora tendremos que actualizar el código HTML del carrusel para que se adapte al nuevo CSS. Lo único que hay que hacer es añadir la clase carousel al div con ID carousel. Fácil, ¿verdad? El código quedará así:

<br />
&lt;div class=&quot;button-next&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', 1)&quot;&gt;&lt;img src=&quot;arrow_right.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-prev&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', -1)&quot;&gt;&lt;img src=&quot;arrow_left.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div id=&quot;carousel&quot; class=&quot;stepcarousel carousel&quot;&gt;</p>
<p>&lt;div class=&quot;belt&quot;&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img1.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Este carousel es una demostraci&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img2.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;de un tutorial de &lt;a href=&quot;http://sumolari.com&quot;&gt;Sumolari.com&lt;/a&gt;.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img3.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Puedes ver el tutorial en cuesti&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img4.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;en &lt;a href=&quot;http://sumolari.com/?p=1708&quot;&gt;este art&amp;iacute;culo&lt;/a&gt; del blog.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img5.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img6.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Amet sit dolor ipsum lorem.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img7.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Bla bla bla bla bla bla bla&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img8.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Ble ble ble ble ble ble ble&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;/div&gt;<br />

Lo siguiente será crear el código HTML del segundo carrusel. En mi caso serán idénticos (tendrán los mismos elementos), así que lo único que tendré que hacer será duplicar el código HTML y cambiar la ID del segundo carrusel de “carousel” a “carousel2“, de forma que el código quedará así:

<br />
&lt;div class=&quot;button-next&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', 1)&quot;&gt;&lt;img src=&quot;arrow_right.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-prev&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', -1)&quot;&gt;&lt;img src=&quot;arrow_left.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div id=&quot;carousel&quot; class=&quot;stepcarousel carousel&quot;&gt;</p>
<p>&lt;div class=&quot;belt&quot;&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img1.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Este carousel es una demostraci&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img2.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;de un tutorial de &lt;a href=&quot;http://sumolari.com&quot;&gt;Sumolari.com&lt;/a&gt;.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img3.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Puedes ver el tutorial en cuesti&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img4.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;en &lt;a href=&quot;http://sumolari.com/?p=1708&quot;&gt;este art&amp;iacute;culo&lt;/a&gt; del blog.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img5.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img6.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Amet sit dolor ipsum lorem.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img7.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Bla bla bla bla bla bla bla&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img8.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Ble ble ble ble ble ble ble&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-next&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel2', 1)&quot;&gt;&lt;img src=&quot;arrow_right.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-prev&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel2', -1)&quot;&gt;&lt;img src=&quot;arrow_left.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div id=&quot;carousel2&quot; class=&quot;stepcarousel carousel&quot;&gt;</p>
<p>&lt;div class=&quot;belt&quot;&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img1.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Este carousel es una demostraci&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img2.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;de un tutorial de &lt;a href=&quot;http://sumolari.com&quot;&gt;Sumolari.com&lt;/a&gt;.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img3.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Puedes ver el tutorial en cuesti&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img4.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;en &lt;a href=&quot;http://sumolari.com/?p=1708&quot;&gt;este art&amp;iacute;culo&lt;/a&gt; del blog.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img5.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img6.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Amet sit dolor ipsum lorem.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img7.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Bla bla bla bla bla bla bla&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img8.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Ble ble ble ble ble ble ble&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;/div&gt;<br />

Ya sólo queda adaptar el código Javascript. Lo primero que tenemos que hacer es añadir una línea más al código original que nos evitará problemas: defaultbuttons: {enable: true, moveby: 1, leftnav: [‘arrowl.gif’, -10, 100], rightnav: [‘arrowr.gif’, -10, 100]}, . A continuación duplicaremos el código y añadiremos un punto y coma (;) al final del código original. Por último cambiaremos la ID que hay en el segundo código de “carousel” a “carousel2“. El código Javascript quedaría así:

<br />
stepcarousel.setup({<br />
galleryid: 'carousel', //id of carousel DIV<br />
beltclass: 'belt', //class of inner &quot;belt&quot; DIV containing all the panel DIVs<br />
panelclass: 'panel', //class of panel DIVs each holding content<br />
autostep: {enable:true, moveby:1, pause:3000},<br />
panelbehavior: {speed:500, wraparound:true, persist:true},<br />
defaultbuttons: {enable: true, moveby: 1, leftnav: ['arrowl.gif', -10, 100], rightnav: ['arrowr.gif', -10, 100]},<br />
statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels<br />
contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']<br />
});<br />
stepcarousel.setup({<br />
galleryid: 'carousel2', //id of carousel DIV<br />
beltclass: 'belt', //class of inner &quot;belt&quot; DIV containing all the panel DIVs<br />
panelclass: 'panel', //class of panel DIVs each holding content<br />
autostep: {enable:true, moveby:1, pause:3000},<br />
panelbehavior: {speed:500, wraparound:true, persist:true},<br />
defaultbuttons: {enable: true, moveby: 1, leftnav: ['arrowl.gif', -10, 100], rightnav: ['arrowr.gif', -10, 100]},<br />
statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels<br />
contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']<br />
})<br />

Y con esto ya lo tenemos acabado. El código final que tendríamos sería este:

<br />
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br />
&lt;title&gt;Carousel con jQuery&lt;/title&gt;<br />
&lt;script src=&quot;jquery.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />
&lt;script src=&quot;carousel.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />
&lt;script type=&quot;text/javascript&quot;&gt;<br />
stepcarousel.setup({<br />
galleryid: 'carousel', //id of carousel DIV<br />
beltclass: 'belt', //class of inner &quot;belt&quot; DIV containing all the panel DIVs<br />
panelclass: 'panel', //class of panel DIVs each holding content<br />
autostep: {enable:true, moveby:1, pause:3000},<br />
panelbehavior: {speed:500, wraparound:true, persist:true},<br />
defaultbuttons: {enable: true, moveby: 1, leftnav: ['arrowl.gif', -10, 100], rightnav: ['arrowr.gif', -10, 100]},<br />
statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels<br />
contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']<br />
});<br />
stepcarousel.setup({<br />
galleryid: 'carousel2', //id of carousel DIV<br />
beltclass: 'belt', //class of inner &quot;belt&quot; DIV containing all the panel DIVs<br />
panelclass: 'panel', //class of panel DIVs each holding content<br />
autostep: {enable:true, moveby:1, pause:3000},<br />
panelbehavior: {speed:500, wraparound:true, persist:true},<br />
defaultbuttons: {enable: true, moveby: 1, leftnav: ['arrowl.gif', -10, 100], rightnav: ['arrowr.gif', -10, 100]},<br />
statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels<br />
contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']<br />
})<br />
&lt;/script&gt;<br />
&lt;style type=&quot;text/css&quot;&gt;<br />
/* Carousel */</p>
<p>.carousel {<br />
position: relative; /* Necesario */<br />
overflow: hidden; /* Necesario */<br />
height: 250px;<br />
margin-left:35px;<br />
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;<br />
}</p>
<p>.carousel .belt {<br />
position: absolute; /* Necesario */<br />
left: 0;<br />
top: 0;<br />
margin:0 10px 10px 0;<br />
}</p>
<p>.carousel .panel {<br />
width:265px;<br />
float: left; /* Necesario */<br />
overflow: hidden;<br />
margin: 15px;<br />
padding:7px;<br />
border:1px solid #5B5B5B;<br />
background:#383838 url(carousel-panel-bg.png) bottom left repeat-x;<br />
}</p>
<p>.carousel .panel .panel-text {<br />
padding-top:5px;<br />
font-size:13px;<br />
font-family:Verdana, Geneva, sans-serif;<br />
color:#FFF;<br />
}</p>
<p>.carousel .panel .panel-text a {<br />
color:#CCC;<br />
text-decoration:none;<br />
}</p>
<p>.carousel .panel .panel-text a:hover {<br />
color:#FFF;<br />
text-decoration:underline;<br />
}</p>
<p>/* Botones del carousel */</p>
<p>.button-prev {<br />
height:250px;<br />
width:35px;<br />
float:left;<br />
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;<br />
-moz-border-radius:10px 0 0 10px;<br />
}</p>
<p>.button-prev a {<br />
display:block;<br />
padding:5px;<br />
margin-top:105px;<br />
}</p>
<p>.button-next {<br />
height:250px;<br />
width:35px;<br />
float:right;<br />
background:#5B5B5B url(carousel-bg.png) bottom left repeat-x;<br />
-moz-border-radius:0 10px 10px 0;<br />
}</p>
<p>.button-next a {<br />
display:block;<br />
padding:5px;<br />
margin-top:105px;<br />
}</p>
<p>a img {<br />
border:none;<br />
}<br />
&lt;/style&gt;<br />
&lt;/head&gt;</p>
<p>&lt;body&gt;<br />
&lt;div class=&quot;button-next&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', 1)&quot;&gt;&lt;img src=&quot;arrow_right.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-prev&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel', -1)&quot;&gt;&lt;img src=&quot;arrow_left.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div id=&quot;carousel&quot; class=&quot;stepcarousel carousel&quot;&gt;</p>
<p>&lt;div class=&quot;belt&quot;&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img1.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Este carousel es una demostraci&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img2.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;de un tutorial de &lt;a href=&quot;http://sumolari.com&quot;&gt;Sumolari.com&lt;/a&gt;.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img3.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Puedes ver el tutorial en cuesti&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img4.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;en &lt;a href=&quot;http://sumolari.com/?p=1708&quot;&gt;este art&amp;iacute;culo&lt;/a&gt; del blog.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img5.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img6.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Amet sit dolor ipsum lorem.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img7.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Bla bla bla bla bla bla bla&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img8.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Ble ble ble ble ble ble ble&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-next&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel2', 1)&quot;&gt;&lt;img src=&quot;arrow_right.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;button-prev&quot;&gt;<br />
&lt;a href=&quot;javascript:stepcarousel.stepBy('carousel2', -1)&quot;&gt;&lt;img src=&quot;arrow_left.png&quot; /&gt;&lt;/a&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div id=&quot;carousel2&quot; class=&quot;stepcarousel carousel&quot;&gt;</p>
<p>&lt;div class=&quot;belt&quot;&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img1.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Este carousel es una demostraci&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img2.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;de un tutorial de &lt;a href=&quot;http://sumolari.com&quot;&gt;Sumolari.com&lt;/a&gt;.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img3.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Puedes ver el tutorial en cuesti&amp;oacute;n&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img4.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;en &lt;a href=&quot;http://sumolari.com/?p=1708&quot;&gt;este art&amp;iacute;culo&lt;/a&gt; del blog.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img5.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img6.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Amet sit dolor ipsum lorem.&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img7.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Bla bla bla bla bla bla bla&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;div class=&quot;panel&quot;&gt;<br />
&lt;img src=&quot;img8.png&quot; /&gt;<br />
&lt;div class=&quot;panel-text&quot;&gt;<br />
&lt;p&gt;Ble ble ble ble ble ble ble&lt;/p&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/div&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />

Como en el tutorial anterior, he publicado una pequeña demo para que veáis el código en acción, que podéis descargar desde aquí.

Nota: Acabo de ver que han actualizado el script de Stepcarousel y le han añadido algunas funciones nuevas. En cuento tenga algo de tiempo libre (últimamente ando algo ocupado) actualizaré WP Carousel.

7 Responses to Creando múltiples carruseles con jQuery

Simplemente una recomendación, jaja: podías esconder el código en cajitas porque es una barbaridad la cantidad que es. O si no que el tutorial en sí aparezca después de un "Leer más".

xDD

Jaja sólo son un par de cientos de líneas, ahora mismo WP Main Menu tiene ya casi 1000 líneas 🙂

Es una burrada… jaja

Perfecto men gracias por responder!! me faltaba una toteria!!! gracias!!!!

Tengo la siguiente duda, has probado en hacer el carrusel con un bucle php, para que genere x carruseles según los que tengas almacenados en la base de datos?

stepcarousel.setup({
galleryid: 'carouselid?>', //id of carousel DIV
beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
panelclass: 'panel', //class of panel DIVs each holding content
autostep: {enable:true, moveby:1, pause:3000},
panelbehavior: {speed:500, wraparound:true, persist:true},
statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
contenttype: ['external'] //content setting ['inline'] or ['external', 'path_to_external_file']
})

Yo probé a escribir esto y al igual la misma linea en la identificación de los carruseles. Gracias de antemano.

Sí, y precisamente de ahí surgió WP Carousel 🙂 .

Ok le voy a hechar un vistazo haber que tal esta!!Muchas gracias!!

Leave a Reply