play()
and pause()
methods to start and stop the video based on the position of the player.Player example
Scroll the player in and out of view. When the player is fully scrolled into view, the video will start playing. When you scroll the player out of view, the video will stop playing.
See the Pen 18202-scrolling-player-view by Brightcove Learning Services (@rcrooks1969) on CodePen.
Source code
View the complete solution on GitHub.
Using the CodePen
Here are some tips to effectively use the above CodePen:
- Toggle the actual display of the player by clicking the Result button.
- Click the HTML/CSS/JS buttons to display ONE of the code types.
- Later in this document the logic, flow and styling used in the application will be discussed in the Player/HTML configuration, Application flow and Application styling sections. The best way to follow along with the information in those sections is to:
- Click the EDIT ON CODEPEN button in the CodePen and have the code available in one browser/browser tab.
- In CodePen, adjust what code you want displayed. You can change the width of different code sections within CodePen.
- View the Player/HTML configuration, Application flow and/or Application styling sections in another browser/browser tab. You will now be able to follow the code explanations and at the same time view the code.
API/Plugin resources used
API Methods |
---|
play() |
pause() |
A key JavaScript event used in this code is onscroll
. This event is dispatched, and in this code handled, every time the window
element is scrolled.
Player/HTML configuration
This section details any special configuration needed during player creation. In addition, other HTML elements that must be added to the page, beyond the in-page embed player implementation code, are described.
Player configuration
The muted
attribute has been added to the player so as to avoid autoplay issues. See the Autoplay Considerations document for details.
Other HTML
An HTML <script>
tag is used to import the jQuery library.
Application flow
The basic logic behind this application is:
- Listen for scroll events.
- When a scroll event is handled, check if the player is in the viewport or not.
- If the player in the viewport, play the video, if it is not, pause the video.
Listen for any scroll events on the window element
Find the code which is labeled:
// ### Execute every time page is scrolled ###
This one line of code calls the checkIfVideoInView()
method on every window.onscroll
event dispatch.
Handle the scroll events
Find the code which is labeled:
// ### Called on scroll, check if in view and the play/pause ###
The checkIfVideoInView()
event handler function checks if the player is in the viewport, then either plays or pauses the video. The isScrolledIntoView()
method is used in an if statement to check if the player is in the viewport. Naturally, the isScrolledIntoView()
method returns a Boolean value.
Check if player is entirely in the viewport
Find the code which is labeled:
// ### Checks if player is in view ###
This function, which returns a Boolean value, uses jQuery to determine if the player is entirely in the viewport.
Application styling
The only CSS sets the player size.
Plugin code
Normally when converting the JavaScript into a Brightcove Player plugin nominal changes are needed. One required change is to replace the standard use of the ready()
method with the code that defines a plugin.
Here is the very commonly used start to JavaScript code that will work with the player:
videojs.getPlayer('myPlayerID').ready(function() {
var myPlayer = this;
...
});
You will change the first line to use the standard syntax to start a Brightcove Player plugin:
videojs.registerPlugin('pluginName', function(options) {
var myPlayer = this;
...
});
As mentioned earlier, you can see the plugin's JavaScript code in this document's corresponding GitHub repo: scroll-into-view.js.
Using the plugin with a player
Once you have the plugin's CSS and JavaScript files stored in an Internet accessible location, you can use the plugin with a player. In Studio's PLAYERS module you can choose a player, then in the PLUGINS section add the URLs to the CSS and JavaScript files, and also add the Name and Options, if options are needed.