Skip to content

Commit 0460b69

Browse files
committed
- Auto play/pause youtube videos & audio
1 parent a2742f5 commit 0460b69

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Would you like to have a website using fullpage.js functionality but you don't k
3434
- [Creating smaller sections](https://github.com/alvarotrigo/fullPage.js#creating-smaller-sections)
3535
- [State classes added by fullpage.js](https://github.com/alvarotrigo/fullPage.js#state-classes-added-by-fullpagejs)
3636
- [Lazy loading](https://github.com/alvarotrigo/fullPage.js#lazy-loading)
37+
- [Autoplay embedded media](https://github.com/alvarotrigo/fullPage.js#)
3738
- [Options](https://github.com/alvarotrigo/fullPage.js#options)
3839
- [Methods](https://github.com/alvarotrigo/fullPage.js#methods)
3940
- [Callbacks](https://github.com/alvarotrigo/fullPage.js#callbacks)
@@ -271,6 +272,27 @@ To enable lazy loading all you need to do is change your `src` attribute to `dat
271272
</video>
272273
```
273274

275+
###Auto play/pause embedded media
276+
277+
#### Play on section/slide load:
278+
Using the attribute `autoplay` for videos or audio, or the param `autoplay=1` for youtube iframes will result in the media element playing on page load.
279+
In order to play it on section/slide load use instead the attribute `data-autoplay`. For example:
280+
281+
```html
282+
<audio data-autoplay>
283+
<source src="http://metakoncept.hr/horse.ogg" type="audio/ogg">
284+
</audio>
285+
```
286+
287+
#### Pause on leave
288+
Embedded HTML5 `<video>` / `<audio>` and Youtube iframes are automatically paused when you navigate away from a section or slide. This can be disabled by using the attribute `data-keepplaying`. For example:
289+
```html
290+
<audio data-keepplaying>
291+
<source src="http://metakoncept.hr/horse.ogg" type="audio/ogg">
292+
</audio>
293+
```
294+
295+
274296
## Options
275297

276298
- `controlArrows`: (default `true`) Determines whether to use control arrows for the slides to move right or left.

jquery.fullPage.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@
654654
addVerticalNavigation();
655655
}
656656

657+
enableYoutubeAPI();
658+
enableVidemoAPI();
659+
657660
if(options.scrollOverflow){
658661
if(document.readyState === 'complete'){
659662
createSlimScrollingHandler();
@@ -838,6 +841,35 @@
838841
afterRenderActions();
839842
}
840843

844+
/*
845+
* Enables the Youtube videos API so we can control their flow if necessary.
846+
*/
847+
function enableYoutubeAPI(){
848+
container.find('iframe[src*="youtube.com/embed/"]').each(function(){
849+
var sign = getUrlParamSign($(this).attr('src'));
850+
$(this).attr('src', $(this).attr('src') + sign + 'enablejsapi=1');
851+
});
852+
}
853+
854+
/*
855+
* Enables the Vimeo videos API so we can control their flow if necessary.
856+
*/
857+
function enableVidemoAPI(){
858+
container.find('iframe[src*="player.vimeo.com/"]').each(function(){
859+
var sign = getUrlParamSign($(this).attr('src'));
860+
$(this).attr('src', $(this).attr('src') + sign + 'api=1');
861+
});
862+
}
863+
864+
/*
865+
* Returns the prefix sign to use for a new parameter in an existen URL.
866+
*
867+
* @return {String} ? | &
868+
*/
869+
function getUrlParamSign(url){
870+
return ( !/\?/.test( url ) ) ? '?' : '&';
871+
}
872+
841873
/**
842874
* Actions and callbacks to fire afterRender
843875
*/
@@ -1534,10 +1566,19 @@
15341566
destiny.find('video, audio').each(function(){
15351567
var element = $(this).get(0);
15361568

1537-
if( element.hasAttribute('autoplay') && typeof element.play === 'function' ) {
1569+
if( element.hasAttribute('data-autoplay') && typeof element.play === 'function' ) {
15381570
element.play();
15391571
}
15401572
});
1573+
1574+
//youtube videos
1575+
destiny.find('iframe[src*="youtube.com/embed/"]').each(function(){
1576+
var element = $(this).get(0);
1577+
1578+
if( /youtube\.com\/embed\//.test($(this).attr('src')) && element.hasAttribute('data-autoplay')){
1579+
element.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
1580+
}
1581+
});
15411582
}
15421583

15431584
/**
@@ -1550,10 +1591,19 @@
15501591
destiny.find('video, audio').each(function(){
15511592
var element = $(this).get(0);
15521593

1553-
if( !element.hasAttribute('data-ignore') && typeof element.pause === 'function' ) {
1594+
if( !element.hasAttribute('data-keepplaying') && typeof element.pause === 'function' ) {
15541595
element.pause();
15551596
}
15561597
});
1598+
1599+
//youtube videos
1600+
destiny.find('iframe[src*="youtube.com/embed/"]').each(function(){
1601+
var element = $(this).get(0);
1602+
1603+
if( /youtube\.com\/embed\//.test($(this).attr('src')) && !element.hasAttribute('data-keepplaying')){
1604+
$(this).get(0).contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}','*');
1605+
}
1606+
});
15571607
}
15581608

15591609
/**

0 commit comments

Comments
 (0)