The
onChange
event
handler/ Embedding YouTube videos
The onChange event handler, defined as an option within the
initialization code featuredcontentglider.init()
, lets you execute
code whenever the Glider changes slides, including when the very first slide
glides in automatically on page load. The event handler has the syntax:
onChange: function(previndex,
curindex, $allcontents){
//custom code here
}
When the Glider first loads on the page, the first two parameters point to
the same slide. The index count starts at 0, meaning 0 indicates the 1st slide,
1 the second etc. The third parameter, $allcontents
, is an
array-like jQuery collection referencing all of the slides within the Glider.
Putting it all together, the below simple example changes the background color
of the current slide to lightyellow (and the previous slide's background color
to white):
onChange: function(previndex,
curindex, $allcontents){
$allcontents.eq(curindex).css({backgroundColor: 'lightyellow'}) //change
current slide to lightyellow
if (previndex != curindex) // if the current slide isn't the same as the
previous slide
$allcontents.eq(previndex).css({backgroundColor: 'white'}) // change
previous slide to white
}
The code:
$allcontents.eq( index )
lets you access a particular slide within the glider based on its index (where 0=1st slide, 1=2nd slide etc). In the above case, we see how to access both the previous slide and current slide.
Embedding YouTube videos with the help of the onChange
event
handler
Embedding Youtube videos inside sliders is all fine and dandy until the user decides to play a video- when the user changes slides, what happens typically is that the video in the previous slide will continue to play, creating an audio and visual mess of increasing proportions. Well, using the onChange event handler of Featured Content Glider, along with YouTube's API that lets us use JavaScript to dynamically manipulate a YouTube video, we can take care of this pesky issue once and for all.
The below complete example demonstrates a Glider with 4 YouTube videos inside it, one for each slide. If the user plays a video and moves to another slide, the Glider will pause the previous slide's video as well, ensuring at no time are two videos playing at the same time:
Full HTML Source: