To scroll to a specific arbitrary position in jCarousel...
- Get the jcarousel instance object. It's in the jQuery .data() of the element that .jcarousel() was called on (side note: in Drupal views jcarousel module, that's the
ul.jcarousel
)
- Call
.scroll()
on it.
In code:
// Create it
$('.posts').jcarousel( someSettings );
// Get it
var jcarousel = $('.posts').data( 'jcarousel' );
// Scroll it
var scrollTo = 1;
var animateScrolling = true;
jcarousel.scroll( scrollTo - 1, animateScrolling );
If ever want to look up a specific element using jQuery selectors, then, scroll to that element (scrolling a jCarousel by element not by position). It's easy: each jCarousel element has an attribute, jcarouselindex. Look it up with var position = $('#some-element').attr('jcarouselindex');
.
Sample:
// Get jcarousel
var jcarousel = $('#menu').data('jcarousel');
var scrollTo = menuOption.parent().attr("jcarouselindex");
var animateScrolling = true;
// Scroll it
jcarousel.scroll(scrollTo - 1, animateScrolling);
where menuOption
is an anchor <a>
like this one:
<li class="jcarousel-item jcarousel-item-horizontal jcarousel-item-8 jcarousel-item-8-horizontal" style="float: left; list-style: none outside none;" jcarouselindex="8">
<a title="Educação de Pacientes e Familiares" data-chapterid="16" data-acronym="PFE" href="">
</li>
Note: it's important to use scrollTo - 1
because index is 0 based.