I have a slideshow building elements into an array, I modify the DOM and then use items from the array to fade in and out on a timer. There are a few more things I'd like to do but I am not sure where to go from here.
The two things I would like to do:
1) on hover & click of the slideshow pause the timer, then restart timer on un-hover or click of other items on the page. (This is necessary to allow the youtube video to play if user clicks the play button so the slideshow doesn't move to the next slide)
I know I could do something like:
on hover()
and click()
// pause timer
but I'm not sure how to stop and restart a setInterval
timer and where to put it in my code
2) And the second thing the Youtube videos do not load gracefully into the page and instead of smoothly fading up they load with a lag, can they be preloaded to achieve the smooth fade with the approach I am using?
Here is a fiddle I am working with: http://jsfiddle.net/designaroni/a34yF/
Here is my code:
HTML:
<div class="slideshow">
<img src="http://placehold.it/401x401" />
<img src="http://placehold.it/402x402" />
<div class="video">
<iframe width="100%" height="100%" src="//www.youtube.com/embed/cbP2N1BQdYc?rel=0" frameborder="0"></iframe>
</div>
<img src="http://placehold.it/404x404" />
<img src="http://placehold.it/405x405" />
<div class="video">
<iframe width="100%" height="100%" src="//www.youtube.com/embed/cbP2N1BQdYc?rel=0" frameborder="0"></iframe>
</div>
CSS:
.media .video,
.media img { background-color:#fad400; padding:10px; margin:0px; height:100%;}
.slideshow { width:100%; height:400px; }
.media { height:100%; }
JS
/*
* 1) input objects into arrays
* 2) write html to remove excess html, add items inside .media div... and other stuff
* 3) do slideshow stuff to the content
*
*
*
*/
(function ($) {
/*make it global*/
//array w/ multiple media types - .video contains youtube iframes
media = $( ".slideshow img, .slideshow .video" );
//remove items from dom
$( ".slideshow img, .slideshow .video" ).remove();
var width = $('.slideshow').width()
// end make globes
//make .media div
$(".slideshow").append("<div class='media'></div>");
//use first and .html() into .media
$(".media").html(media[0]);
$.fn.simpleSlider = function(options) {
//give the user options
var defaults = {
pause : 5000,
speed : 400,
descriptionSpeed: 400,
transition : 'fade'
},
//merge options with defaults
options = $.extend(defaults, options);
//if statement
if(options.transition === 'fade') fadesetint();
/////////////////////////////////////////////////////////////////
function fadesetint() {
//this will start the animation
fadeBeginNow = setInterval(fade, options.pause);
}
/////////////////////////////////////////////////////////////////
//KEY
/////////////////////////////////////////////////////////////////
function currentImageKey() {
currentMedia = $(".media").children()[0]
i = $.inArray(currentMedia, media)
return i;
}
/////////////////////////////////////////////////////////////////
//FORWARD & BACK
/////////////////////////////////////////////////////////////////
//will fade & move the slideshow forward one step
function fade() {
currentImageKey();
if (i < media.length -1) {
fadeImage(i + 1);
} else {
fadeImage(0);
}
}
//will fade & move slideshow backward one step
function fadePrev() {
currentImageKey();
if (i == 0) {
fadeImage (media.length - 1);
} else {
fadeImage(i - 1);
}
}
/////////////////////////////////////////////////////////////////
//ANIMATION
/////////////////////////////////////////////////////////////////
//fade animate & change media to variable passed to i
function fadeImage(i) {
$('.media').children().stop().animate({
opacity: 0,
}, options.speed, function() {
$(".media").html(media[i]).children().css({opacity: 0})
$('.media').children().stop().animate({
opacity: 1
}, options.speed)
})
}
}; // end simpleSlider
})(jQuery);
//this code below runs in your html while plugin above runs in external script
$('.slideshow').simpleSlider({
pause : 6000, //can set transition pause in mill
speed : 500, //can set transition speed in mill, this is in ### AND out ###
descriptionSpeed : 400, //can set transition speed in mill, this is in ### AND out ###
transition : 'fade', //can set to slide or fade
});