Browse Source

Added error message

Added exception because return nothing is hard to debug
pull/164/head
Nathalie Larsson 12 years ago
parent
commit
c9957564d3
  1. 782
      responsiveslides.js

782
responsiveslides.js

@ -1,391 +1,391 @@
/*! ResponsiveSlides.js v1.53 /*! ResponsiveSlides.js v1.53
* http://responsiveslides.com * http://responsiveslides.com
* http://viljamis.com * http://viljamis.com
* *
* Copyright (c) 2011-2012 @viljamis * Copyright (c) 2011-2012 @viljamis
* Available under the MIT license * Available under the MIT license
*/ */
/*jslint browser: true, sloppy: true, vars: true, plusplus: true, indent: 2 */ /*jslint browser: true, sloppy: true, vars: true, plusplus: true, indent: 2 */
(function ($, window, i) { (function ($, window, i) {
$.fn.responsiveSlides = function (options) { $.fn.responsiveSlides = function (options) {
// Default settings // Default settings
var settings = $.extend({ var settings = $.extend({
"auto": true, // Boolean: Animate automatically, true or false "auto": true, // Boolean: Animate automatically, true or false
"speed": 500, // Integer: Speed of the transition, in milliseconds "speed": 500, // Integer: Speed of the transition, in milliseconds
"timeout": 4000, // Integer: Time between slide transitions, in milliseconds "timeout": 4000, // Integer: Time between slide transitions, in milliseconds
"pager": false, // Boolean: Show pager, true or false "pager": false, // Boolean: Show pager, true or false
"nav": false, // Boolean: Show navigation, true or false "nav": false, // Boolean: Show navigation, true or false
"random": false, // Boolean: Randomize the order of the slides, true or false "random": false, // Boolean: Randomize the order of the slides, true or false
"pause": false, // Boolean: Pause on hover, true or false "pause": false, // Boolean: Pause on hover, true or false
"pauseControls": true, // Boolean: Pause when hovering controls, true or false "pauseControls": true, // Boolean: Pause when hovering controls, true or false
"prevText": "Previous", // String: Text for the "previous" button "prevText": "Previous", // String: Text for the "previous" button
"nextText": "Next", // String: Text for the "next" button "nextText": "Next", // String: Text for the "next" button
"maxwidth": "", // Integer: Max-width of the slideshow, in pixels "maxwidth": "", // Integer: Max-width of the slideshow, in pixels
"navContainer": "", // Selector: Where auto generated controls should be appended to, default is after the <ul> "navContainer": "", // Selector: Where auto generated controls should be appended to, default is after the <ul>
"manualControls": "", // Selector: Declare custom pager navigation "manualControls": "", // Selector: Declare custom pager navigation
"namespace": "rslides", // String: change the default namespace used "namespace": "rslides", // String: change the default namespace used
before: function () {}, // Function: Before callback before: function () {}, // Function: Before callback
after: function () {} // Function: After callback after: function () {} // Function: After callback
}, options); }, options);
return this.each(function () { return this.each(function () {
// Index for namespacing // Index for namespacing
i++; i++;
var $this = $(this), var $this = $(this),
// Local variables // Local variables
vendor, vendor,
selectTab, selectTab,
startCycle, startCycle,
restartCycle, restartCycle,
rotate, rotate,
$tabs, $tabs,
// Helpers // Helpers
index = 0, index = 0,
$slide = $this.children(), $slide = $this.children(),
length = $slide.size(), length = $slide.size(),
fadeTime = parseFloat(settings.speed), fadeTime = parseFloat(settings.speed),
waitTime = parseFloat(settings.timeout), waitTime = parseFloat(settings.timeout),
maxw = parseFloat(settings.maxwidth), maxw = parseFloat(settings.maxwidth),
// Namespacing // Namespacing
namespace = settings.namespace, namespace = settings.namespace,
namespaceIdx = namespace + i, namespaceIdx = namespace + i,
// Classes // Classes
navClass = namespace + "_nav " + namespaceIdx + "_nav", navClass = namespace + "_nav " + namespaceIdx + "_nav",
activeClass = namespace + "_here", activeClass = namespace + "_here",
visibleClass = namespaceIdx + "_on", visibleClass = namespaceIdx + "_on",
slideClassPrefix = namespaceIdx + "_s", slideClassPrefix = namespaceIdx + "_s",
// Pager // Pager
$pager = $("<ul class='" + namespace + "_tabs " + namespaceIdx + "_tabs' />"), $pager = $("<ul class='" + namespace + "_tabs " + namespaceIdx + "_tabs' />"),
// Styles for visible and hidden slides // Styles for visible and hidden slides
visible = {"float": "left", "position": "relative", "opacity": 1, "zIndex": 2}, visible = {"float": "left", "position": "relative", "opacity": 1, "zIndex": 2},
hidden = {"float": "none", "position": "absolute", "opacity": 0, "zIndex": 1}, hidden = {"float": "none", "position": "absolute", "opacity": 0, "zIndex": 1},
// Detect transition support // Detect transition support
supportsTransitions = (function () { supportsTransitions = (function () {
var docBody = document.body || document.documentElement; var docBody = document.body || document.documentElement;
var styles = docBody.style; var styles = docBody.style;
var prop = "transition"; var prop = "transition";
if (typeof styles[prop] === "string") { if (typeof styles[prop] === "string") {
return true; return true;
} }
// Tests for vendor specific prop // Tests for vendor specific prop
vendor = ["Moz", "Webkit", "Khtml", "O", "ms"]; vendor = ["Moz", "Webkit", "Khtml", "O", "ms"];
prop = prop.charAt(0).toUpperCase() + prop.substr(1); prop = prop.charAt(0).toUpperCase() + prop.substr(1);
var i; var i;
for (i = 0; i < vendor.length; i++) { for (i = 0; i < vendor.length; i++) {
if (typeof styles[vendor[i] + prop] === "string") { if (typeof styles[vendor[i] + prop] === "string") {
return true; return true;
} }
} }
return false; return false;
})(), })(),
// Fading animation // Fading animation
slideTo = function (idx) { slideTo = function (idx) {
settings.before(); settings.before();
// If CSS3 transitions are supported // If CSS3 transitions are supported
if (supportsTransitions) { if (supportsTransitions) {
$slide $slide
.removeClass(visibleClass) .removeClass(visibleClass)
.css(hidden) .css(hidden)
.eq(idx) .eq(idx)
.addClass(visibleClass) .addClass(visibleClass)
.css(visible); .css(visible);
index = idx; index = idx;
setTimeout(function () { setTimeout(function () {
settings.after(); settings.after();
}, fadeTime); }, fadeTime);
// If not, use jQuery fallback // If not, use jQuery fallback
} else { } else {
$slide $slide
.stop() .stop()
.fadeOut(fadeTime, function () { .fadeOut(fadeTime, function () {
$(this) $(this)
.removeClass(visibleClass) .removeClass(visibleClass)
.css(hidden) .css(hidden)
.css("opacity", 1); .css("opacity", 1);
}) })
.eq(idx) .eq(idx)
.fadeIn(fadeTime, function () { .fadeIn(fadeTime, function () {
$(this) $(this)
.addClass(visibleClass) .addClass(visibleClass)
.css(visible); .css(visible);
settings.after(); settings.after();
index = idx; index = idx;
}); });
} }
}; };
// Random order // Random order
if (settings.random) { if (settings.random) {
$slide.sort(function () { $slide.sort(function () {
return (Math.round(Math.random()) - 0.5); return (Math.round(Math.random()) - 0.5);
}); });
$this $this
.empty() .empty()
.append($slide); .append($slide);
} }
// Add ID's to each slide // Add ID's to each slide
$slide.each(function (i) { $slide.each(function (i) {
this.id = slideClassPrefix + i; this.id = slideClassPrefix + i;
}); });
// Add max-width and classes // Add max-width and classes
$this.addClass(namespace + " " + namespaceIdx); $this.addClass(namespace + " " + namespaceIdx);
if (options && options.maxwidth) { if (options && options.maxwidth) {
$this.css("max-width", maxw); $this.css("max-width", maxw);
} }
// Hide all slides, then show first one // Hide all slides, then show first one
$slide $slide
.hide() .hide()
.css(hidden) .css(hidden)
.eq(0) .eq(0)
.addClass(visibleClass) .addClass(visibleClass)
.css(visible) .css(visible)
.show(); .show();
// CSS transitions // CSS transitions
if (supportsTransitions) { if (supportsTransitions) {
$slide $slide
.show() .show()
.css({ .css({
// -ms prefix isn't needed as IE10 uses prefix free version // -ms prefix isn't needed as IE10 uses prefix free version
"-webkit-transition": "opacity " + fadeTime + "ms ease-in-out", "-webkit-transition": "opacity " + fadeTime + "ms ease-in-out",
"-moz-transition": "opacity " + fadeTime + "ms ease-in-out", "-moz-transition": "opacity " + fadeTime + "ms ease-in-out",
"-o-transition": "opacity " + fadeTime + "ms ease-in-out", "-o-transition": "opacity " + fadeTime + "ms ease-in-out",
"transition": "opacity " + fadeTime + "ms ease-in-out" "transition": "opacity " + fadeTime + "ms ease-in-out"
}); });
} }
// Only run if there's more than one slide // Only run if there's more than one slide
if ($slide.size() > 1) { if ($slide.size() > 1) {
// Make sure the timeout is at least 100ms longer than the fade // Make sure the timeout is at least 100ms longer than the fade
if (waitTime < fadeTime + 100) { if (waitTime < fadeTime + 100) {
return; throw new Error("Make sure the timeout is at least 100ms longer than the fade");
} }
// Pager // Pager
if (settings.pager && !settings.manualControls) { if (settings.pager && !settings.manualControls) {
var tabMarkup = []; var tabMarkup = [];
$slide.each(function (i) { $slide.each(function (i) {
var n = i + 1; var n = i + 1;
tabMarkup += tabMarkup +=
"<li>" + "<li>" +
"<a href='#' class='" + slideClassPrefix + n + "'>" + n + "</a>" + "<a href='#' class='" + slideClassPrefix + n + "'>" + n + "</a>" +
"</li>"; "</li>";
}); });
$pager.append(tabMarkup); $pager.append(tabMarkup);
// Inject pager // Inject pager
if (options.navContainer) { if (options.navContainer) {
$(settings.navContainer).append($pager); $(settings.navContainer).append($pager);
} else { } else {
$this.after($pager); $this.after($pager);
} }
} }
// Manual pager controls // Manual pager controls
if (settings.manualControls) { if (settings.manualControls) {
$pager = $(settings.manualControls); $pager = $(settings.manualControls);
$pager.addClass(namespace + "_tabs " + namespaceIdx + "_tabs"); $pager.addClass(namespace + "_tabs " + namespaceIdx + "_tabs");
} }
// Add pager slide class prefixes // Add pager slide class prefixes
if (settings.pager || settings.manualControls) { if (settings.pager || settings.manualControls) {
$pager.find('li').each(function (i) { $pager.find('li').each(function (i) {
$(this).addClass(slideClassPrefix + (i + 1)); $(this).addClass(slideClassPrefix + (i + 1));
}); });
} }
// If we have a pager, we need to set up the selectTab function // If we have a pager, we need to set up the selectTab function
if (settings.pager || settings.manualControls) { if (settings.pager || settings.manualControls) {
$tabs = $pager.find('a'); $tabs = $pager.find('a');
// Select pager item // Select pager item
selectTab = function (idx) { selectTab = function (idx) {
$tabs $tabs
.closest("li") .closest("li")
.removeClass(activeClass) .removeClass(activeClass)
.eq(idx) .eq(idx)
.addClass(activeClass); .addClass(activeClass);
}; };
} }
// Auto cycle // Auto cycle
if (settings.auto) { if (settings.auto) {
startCycle = function () { startCycle = function () {
rotate = setInterval(function () { rotate = setInterval(function () {
// Clear the event queue // Clear the event queue
$slide.stop(true, true); $slide.stop(true, true);
var idx = index + 1 < length ? index + 1 : 0; var idx = index + 1 < length ? index + 1 : 0;
// Remove active state and set new if pager is set // Remove active state and set new if pager is set
if (settings.pager || settings.manualControls) { if (settings.pager || settings.manualControls) {
selectTab(idx); selectTab(idx);
} }
slideTo(idx); slideTo(idx);
}, waitTime); }, waitTime);
}; };
// Init cycle // Init cycle
startCycle(); startCycle();
} }
// Restarting cycle // Restarting cycle
restartCycle = function () { restartCycle = function () {
if (settings.auto) { if (settings.auto) {
// Stop // Stop
clearInterval(rotate); clearInterval(rotate);
// Restart // Restart
startCycle(); startCycle();
} }
}; };
// Pause on hover // Pause on hover
if (settings.pause) { if (settings.pause) {
$this.hover(function () { $this.hover(function () {
clearInterval(rotate); clearInterval(rotate);
}, function () { }, function () {
restartCycle(); restartCycle();
}); });
} }
// Pager click event handler // Pager click event handler
if (settings.pager || settings.manualControls) { if (settings.pager || settings.manualControls) {
$tabs.bind("click", function (e) { $tabs.bind("click", function (e) {
e.preventDefault(); e.preventDefault();
if (!settings.pauseControls) { if (!settings.pauseControls) {
restartCycle(); restartCycle();
} }
// Get index of clicked tab // Get index of clicked tab
var idx = $tabs.index(this); var idx = $tabs.index(this);
// Break if element is already active or currently animated // Break if element is already active or currently animated
if (index === idx || $("." + visibleClass).queue('fx').length) { if (index === idx || $("." + visibleClass).queue('fx').length) {
return; return;
} }
// Remove active state from old tab and set new one // Remove active state from old tab and set new one
selectTab(idx); selectTab(idx);
// Do the animation // Do the animation
slideTo(idx); slideTo(idx);
}) })
.eq(0) .eq(0)
.closest("li") .closest("li")
.addClass(activeClass); .addClass(activeClass);
// Pause when hovering pager // Pause when hovering pager
if (settings.pauseControls) { if (settings.pauseControls) {
$tabs.hover(function () { $tabs.hover(function () {
clearInterval(rotate); clearInterval(rotate);
}, function () { }, function () {
restartCycle(); restartCycle();
}); });
} }
} }
// Navigation // Navigation
if (settings.nav) { if (settings.nav) {
var navMarkup = var navMarkup =
"<a href='#' class='" + navClass + " prev'>" + settings.prevText + "</a>" + "<a href='#' class='" + navClass + " prev'>" + settings.prevText + "</a>" +
"<a href='#' class='" + navClass + " next'>" + settings.nextText + "</a>"; "<a href='#' class='" + navClass + " next'>" + settings.nextText + "</a>";
// Inject navigation // Inject navigation
if (options.navContainer) { if (options.navContainer) {
$(settings.navContainer).append(navMarkup); $(settings.navContainer).append(navMarkup);
} else { } else {
$this.after(navMarkup); $this.after(navMarkup);
} }
var $trigger = $("." + namespaceIdx + "_nav"), var $trigger = $("." + namespaceIdx + "_nav"),
$prev = $trigger.filter(".prev"); $prev = $trigger.filter(".prev");
// Click event handler // Click event handler
$trigger.bind("click", function (e) { $trigger.bind("click", function (e) {
e.preventDefault(); e.preventDefault();
var $visibleClass = $("." + visibleClass); var $visibleClass = $("." + visibleClass);
// Prevent clicking if currently animated // Prevent clicking if currently animated
if ($visibleClass.queue('fx').length) { if ($visibleClass.queue('fx').length) {
return; return;
} }
// Adds active class during slide animation // Adds active class during slide animation
// $(this) // $(this)
// .addClass(namespace + "_active") // .addClass(namespace + "_active")
// .delay(fadeTime) // .delay(fadeTime)
// .queue(function (next) { // .queue(function (next) {
// $(this).removeClass(namespace + "_active"); // $(this).removeClass(namespace + "_active");
// next(); // next();
// }); // });
// Determine where to slide // Determine where to slide
var idx = $slide.index($visibleClass), var idx = $slide.index($visibleClass),
prevIdx = idx - 1, prevIdx = idx - 1,
nextIdx = idx + 1 < length ? index + 1 : 0; nextIdx = idx + 1 < length ? index + 1 : 0;
// Go to slide // Go to slide
slideTo($(this)[0] === $prev[0] ? prevIdx : nextIdx); slideTo($(this)[0] === $prev[0] ? prevIdx : nextIdx);
if (settings.pager || settings.manualControls) { if (settings.pager || settings.manualControls) {
selectTab($(this)[0] === $prev[0] ? prevIdx : nextIdx); selectTab($(this)[0] === $prev[0] ? prevIdx : nextIdx);
} }
if (!settings.pauseControls) { if (!settings.pauseControls) {
restartCycle(); restartCycle();
} }
}); });
// Pause when hovering navigation // Pause when hovering navigation
if (settings.pauseControls) { if (settings.pauseControls) {
$trigger.hover(function () { $trigger.hover(function () {
clearInterval(rotate); clearInterval(rotate);
}, function () { }, function () {
restartCycle(); restartCycle();
}); });
} }
} }
} }
// Max-width fallback // Max-width fallback
if (typeof document.body.style.maxWidth === "undefined" && options.maxwidth) { if (typeof document.body.style.maxWidth === "undefined" && options.maxwidth) {
var widthSupport = function () { var widthSupport = function () {
$this.css("width", "100%"); $this.css("width", "100%");
if ($this.width() > maxw) { if ($this.width() > maxw) {
$this.css("width", maxw); $this.css("width", maxw);
} }
}; };
// Init fallback // Init fallback
widthSupport(); widthSupport();
$(window).bind("resize", function () { $(window).bind("resize", function () {
widthSupport(); widthSupport();
}); });
} }
}); });
}; };
})(jQuery, this, 0); })(jQuery, this, 0);

Loading…
Cancel
Save