Last active
December 14, 2015 19:09
-
-
Save dflynn15/5134995 to your computer and use it in GitHub Desktop.
jQuery plugin for a lazy loading "presenter." Based on the comp presenter here: http://blog.stevendesigns.com/comp-presenter-jquery-plugin/ Allows for both lists and div's to organize images or content. Current support is only for images. Content sniffing will be added later on in a GitHub repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Created by: Daniel Flynn | |
* Date: Mar 04, 2013 | |
* jQuery plugin for a lazy loading slideshow | |
* Assumes that the slides or content is in order and has a data-src attribute. | |
* | |
* Currently is only working with images as content. Other file types will be added | |
* in as development continues | |
* | |
* Overall structure should follow as such: | |
* <wrapper> | |
* <inner content wrap> | |
* <content data-src=""></content | |
* </inner content wrap> | |
* </wrapper> | |
**/ | |
(function($){ | |
$.fn.extend({ | |
/** | |
* Default options: | |
* next: the selector for the next "button" | |
* previous: the selector for the previous "button" | |
* align: the aligment for the content piece | |
* preload: the number of content pieces to lazy load | |
* start: the content piece to start with, accepts order number | |
* position: the position the presenter is on | |
* current: the current content piece the presenter is on | |
* list: list of the content pieces | |
**/ | |
presenter: function(options){ | |
// Sets the default options to be overridden | |
var defaults = { | |
next: $('.next'), | |
previous: $('.previous'), | |
align: "center", | |
preload: 3, | |
start: 0, | |
position: 0, | |
current: $(this).children().first().children().first(), | |
list: $(this).children().children() | |
}; | |
// Merges given options with defaults | |
options = $.extend(defaults, options); | |
// Sets the position slide to the start content as an override | |
options.position = options.start; | |
// Return the object to allow for method chaining and allows for multiple slideshows | |
return this.each(function(){ | |
// For referencing | |
$presenter = $(this); | |
// Initialize the presenter | |
init(); | |
function init(){ | |
// Add event listeners | |
addEventListeners(); | |
// Load the first n amount of content pieces | |
for (var i = 0; i < options.preload; i++){ | |
$content = $(options.list[i]); | |
$content.attr('src', $content.data('src')).hide(); | |
} | |
$(options.current).fadeIn(); | |
} | |
/** | |
* Adds the event listeners to the page | |
**/ | |
function addEventListeners(){ | |
presenter = this; | |
// On the next "button" click call nextContent | |
options.next.on('click', function(){ | |
nextContent(); | |
}); | |
// On the previous "button" click call prevContent | |
options.previous.on('click', function(){ | |
prevContent(); | |
}); | |
// This will re-align the content pieces when the window is resized | |
$(window).resize(function(){ | |
alignContent(); | |
}); | |
// Keyboard arrow navigation | |
$(document).keydown(function(e){ | |
// User pressed the right arrow key | |
if(e.keyCode == 39){ | |
nextContent(); | |
return false; | |
} | |
// User pressed the left arrow key | |
if(e.keyCode == 37){ | |
prevContent(); | |
return false; | |
} | |
}); | |
} | |
/** | |
* Function called when the next slide event is triggered | |
**/ | |
function nextContent(){ | |
// Increase the position by one place or set back to 0 if it is at the end of the list | |
if((options.position + 2) > options.list.length){ | |
options.position = 0; | |
} else{ | |
options.position++; | |
} | |
// Fade out the "old" current content and set current to the new content piece | |
$(options.current).fadeOut(); | |
options.current = options.list[options.position]; | |
showContent(); | |
} | |
/** | |
* Function called when the previous slide event is triggered | |
**/ | |
function prevContent(){ | |
if(options.position === 0){ | |
return false; | |
} else { | |
options.position--; | |
$(options.current).fadeOut(); | |
options.current = options.list[options.position]; | |
} | |
showContent(); | |
} | |
/** | |
* Function that handles fading in the content after aligning it | |
**/ | |
function showContent(){ | |
alignContent(); | |
checkLoadStatus(); | |
$content = $(options.current); | |
// If the file is a swf in an object tag handle height differently | |
if($content.prop("tagName") == "OBJECT"){ | |
$presenter.height($content.attr("height")); | |
} else { | |
$presenter.height($content.height()); | |
} | |
$(options.current).fadeIn(); | |
} | |
/** | |
* Aligns the content piece based on how the options were given | |
**/ | |
function alignContent(){ | |
var windowWidth = window.innerWidth; | |
var slideWidth = $(options.current).width(); | |
var rightWidth = windowWidth - slideWidth; | |
var centerWidth = Math.round(rightWidth/2); | |
if(options.align == "center"){ | |
$presenter.css("left", centerWidth); | |
} else if(options.align == "right"){ | |
$presenter.css("left", rightWidth); | |
} | |
} | |
/** | |
* Checks the status of the buffer and loads it if necessary | |
**/ | |
function checkLoadStatus(){ | |
if((options.position + (options.preload - 1)) < (options.list.length)){ | |
$content = $(options.list[options.position + options.preload]); | |
// If the content is not loaded, then load | |
if($content.attr('src')){ | |
return false; | |
} | |
// Load the content | |
$content.attr('src', $content.data('src')).hide(); | |
} | |
} | |
}); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment