Building a nice slideshow with Drupal views and jQuery
For one of my projects at One-Agency, i had to build a slideshow with images of one of my content types.
I started to make my promotions content type with CCK and make it possible to upload an image and add extra information on the promo.
After that, i could start with the real work.
Prepare the view
I made a block-view with the settings to show only promotions that are published, and show the nodes in this view as a node. Once you have this set up, you can start to prepare you’re template file.
To easely theme my nodes, i made a node-promotion.tpl.php file.
Iadded a promo-splitter class between my teaser nodes, so i can easely hook on this class in my javascript. All the other classes / id’s are used to style my promotion-slideshow.
Once this is finished, i have a nice clean view that gives me a list of the promotions. Seperated by a div with class promo-splitter.
Make the javascript
Prepare the vars
I started with adding vars that will be needed in all my functions. Following vars are needed:
- slides: jquery object that contains all the slides
- position: the current position in the slideshow
- slideshow_counter: empty var that will contain the counter (see later)
- slideshow: my interval
Init the slideshow
After this, i started to make a new function on Drupal.behaviors that will be the init-er of my slideshow.
In this function, i will create my slides jquery object, create all the counters / start-stop buttons and start the slideshow.
show next slide
I created a function to loop through all the slides, and start with the first again after the last slide has been reached.
Start / stop the slideshow
To finish the javascript stuff. I created a start-stop function, so users can pause the slideshow, and start it again when they want. I wrapped my text in a Drupal.t function, so this text can easely be changed through Drupal translations.
Javascript code
-
var slides;
-
var position = 0;
-
var slideshow_counter;
-
var slideshow;
-
-
Drupal.behaviors.promosSlideshow = function(context) {
-
-
slides = $("#block-views-promosfrontendimage-block_1 .view-promosfrontendimage-splitter");
-
if(slides.length > 0) {
-
var slideshow_stats = $(‘#slideshow-stats’);
-
-
slideshow_counter = document.createElement("span");
-
slideshow_counter.id = "slideshow-counter";
-
slideshow_counter.innerHTML = "1";
-
slideshow_stats.append(slideshow_counter);
-
-
var slideshow_total = document.createElement("span");
-
slideshow_total.id = "slideshow-total";
-
slideshow_total.innerHTML = ‘ / ‘ + slides.length;
-
slideshow_stats.append(slideshow_total);
-
-
var slideshow_pause = document.createElement("a");
-
slideshow_pause.onclick = function() { startStopSlideshow(); return false;}
-
slideshow_pause.href = "#";
-
slideshow_pause.id = "slideshow-pause";
-
slideshow_pause.innerHTML = Drupal.t("Pause");
-
slideshow_stats.append(slideshow_pause);
-
-
$(slides[position]).fadeIn("slow");
-
slideshow = setInterval("showNextSlide()", 7000);
-
}
-
-
}
-
-
function startStopSlideshow() {
-
-
if(slideshow == "") {
-
$(‘#slideshow-pause’).text(Drupal.t("Pause"));
-
slideshow = setInterval("showNextSlide()", 7000);
-
} else {
-
$(‘#slideshow-pause’).text(Drupal.t("Start"));
-
clearInterval(slideshow);
-
slideshow = "";
-
}
-
-
}
-
-
function showNextSlide() {
-
-
var old_position = position;
-
position++;
-
if(position == slides.length) {
-
position = 0;
-
}
-
$(slides[old_position]).fadeOut("slow", function() {
-
$(slides[position]).fadeIn("slow");
-
});
-
-
if(slideshow_counter) {
-
slideshow_counter.innerHTML = position + 1;
-
}
-
-
}
Finish it with css
Finally, i wrote some css to style my slideshow, and set all the slides default to display: none;.
Final result
A result can be viewed here
August 13th, 2009 at 11:55 pm
Why don’t just use Slideshow Views module?
I used it at this mantenimiento informatico spanish site, and it worked like a charm without going into javascript coding..