Next, we want to set up the function that will create the next button, previous button, and pagination buttons and makes them work.
initCallback
that will allow us to pass in the name of a function that should be called when the carousel is created. Let's get started by creating an empty function and calling it:var slider = $('#featured-carousel'), function carouselInit(carousel) { // Our function goes here } $(document).ready(function(){ slider.jcarousel({ scroll: 1, buttonNextHTML: null, buttonPrevHTML: null, initCallback: carouselInit }); });
Whatever actions we write inside of our carouselInit()
function, it will be
executed when the carousel is initialized or set up. Since any page numbers and
previous and next buttons would only be functional if JavaScript is enabled, we want
to create those buttons dynamically with JavaScript rather than coding them in our
HTML. Let's take a look at how we can create a list of page links to each slide in the
slider.
function carouselInit(carousel) {
var slides = slider.find('li'),
}
<div>
tags just before the slider:function carouselInit(carousel) {
var slides = slider.find('li'),
slider.before('<span id="page-controls"><span id="pages"></span></span>'),
}
function carouselInit(carousel) {
var slides = slider.find('li'),
slider.before('<span id="page-controls"><span id="pages"></span></span>'),
var controls = $('#page-controls'),
var pages = $('#pages'),
}
function carouselInit(carousel) {
var slides = slider.find('li'),
slider.before('<span id="page-controls"><span id="pages"></span></span>'),
var controls = $('#page-controls'),
var pages = $('#pages'),
for (i=1; i<=slides.length; i++) {
pages.append('<a href="#">' + i + '</a>'),
}
}
We're starting with i = 1
, because the first page number will be 1. Then we're
checking to see if i
is less than or equal to the number of slides (slides.length
is
the number of slides). If i
is less than or equal to the number of slides, we're going
to increment i by one number—basically we're going to add 1 to i
and i++
is a
JavaScript shortcut way of saying i = i+1
.
Each time through the loop, we're going to append a link to the pages container we created. It's a link wrapped around a page number, and i represents our page number.
If you refresh the page in a browser at this point, you'll see numbers 1 to 12 linked above the slideshow. They aren't styled, and clicking on them won't do anything, because we haven't set that up yet—that's what we'll do next.
styles.css
file and add these few lines to the CSS:#page-controls { line-height:25px;height:25px; } #page-controls a { margin:0 4px 0 0;padding:0 5px;border:1px solid #859900; } #page-controls a:hover { border-color: #D33682; } #page-controls a.current { color:#333;border-color:#333; }
This sets the height for our slider controls row to the 25 pixels that we allowed for
it previously. Then we put a green border around each link, which will turn to a pink
border when the link is hovered over. We adjusted margins and padding to get a
nicely spaced row of boxes. Finally, we added a .current
class for our links to allow
us to mark the currently selected link in dark gray.
function carouselInit(carousel) {
var slides = slider.find('li'),
slider.before('<span id="page-controls"><span id="pages"></span></span>'),
var controls = $('#page-controls'),
var pages = $('#pages'),
for (i=1; i<=slides.length; i++) {
pages.append('<a href="#">' + i + '</a>'),
}
pages.find('a').bind('click', function(){
//click code will go here
});
}
function carouselInit(carousel) {
var slides = slider.find('li'),
slider.before('<span id="page-controls"><span id="pages"></span></span>'),
var controls = $('#page-controls'),
var pages = $('#pages'),
for (i=1; i<=slides.length; i++) {
pages.append('<a href="#">' + i + '</a>'),
}
pages.find('a').bind('click', function(){
return false;
});
}
carousel.scroll($.jcarousel.intval(number));
The number
s near the end is where we would pass in which slide we want to scroll
to. For example, if we wanted to scroll to the sixth slide, we'd say:
carousel.scroll($.jcarousel.intval(6));
In our case, the number slide we want to scroll to is the page number in our link. For example, if I click on the following link:
<a href="#">3</a>
That means I want to scroll to the third slide in the slider. I can get that number by
using jQuery's text()
method as follows:
pages.find('a').bind('click', function(){
carousel.scroll($.jcarousel.intval($(this).text()));
return false;
});
If I click on the fourth link, $(this).text()
will be equal to 4; on the seventh link,
it will be equal to 7, and so on.
Refresh the page in the browser, and you'll see that clicking on a numbered link will scroll the slider to that slide.
current
class—now we just have to be sure we're adding that class to the current link. Here's how we'll do that.pages.find('a').bind('click', function(){ carousel.scroll($.jcarousel.intval($(this).text())); $(this).addClass('current'), return false; });
Now if you refresh the page in the browser, you'll see that clicking a page number
applies the current
class CSS to the link, highlighting it. However, clicking a second
page number highlights that link in addition to the previous link. We have to make
sure that we're removing the class from the old link too. Add the following line to
take care of that:
pages.find('a').bind('click', function(){
carousel.scroll($.jcarousel.intval($(this).text()));
$(this).siblings('.current').removeClass('current'),
$(this).addClass('current'),
return false;
});
This line checks all of the links' siblings for any that might have the class of current. If it finds any, it removes the class.
pages.find('a').bind('click', function(){
carousel.scroll($.jcarousel.intval($(this).text()));
$(this).siblings('.current').removeClass('current'),
$(this).addClass('current'),
return false;
}).filter(':first').click();
Remember that jQuery allows us to chain methods—even though we've got a whole
function written inside the bind()
method, we can still chain the next method to
the end of it. We call the filter()
method to narrow down the list of links to just
the first one, then call the click()
method to fire off the click function we just
bound to the link.
Now if you refresh the page in the browser, you'll see that the first link is highlighted with our current class CSS.
18.224.39.74