YTPlayer是一个jQuery插件,允许您使用html5将youtube视频作为网页的背景数据-*属性和youtube API。
您也可以将此插件用作网页的普通视频播放器(支持播放列表)
# NPM $ npm install jquery.mb.ytplayer # Bower $ bower install jquery.mb.ytplayer
1.在页面上包含jQuery框架和jQuery?YTPlayer
<script src="/path/to/cdn/jquery.min.js"></script> <script src="/path/to/dist/jquery.mb.YTPlayer.min.js"></script>
2.包含jQuery YTPlayer CSS来为您的播放器设置样式
<link href="/path/to/dist/css/jquery.mb.YTPlayer.min.css" rel="stylesheet" />
3.调用插件,我们就可以开始了。
jQuery(function(){
jQuery(".player").YTPlayer();
});4.初始化YTPlayer作为背景。您可以使用数据属性属性:
<a class="player"
data-property="{videoURL:'YW2Aa4CJEgY',containment:'body',autoPlay:true, mute:true, startAt:0,opacity:1}">
My video
</a>5.将插件初始化为一个简单的Youtube播放器。
<a class="player"
data-property="{videoURL:'YW2Aa4CJEgY',containment:'self',autoPlay:true, mute:true, startAt:0,opacity:1}">
My video
</a>6.您也可以使用YT播放列表方法。
// videos: an array of video objects
// shuffle: set to true you can have a random sequence of videos
// callback: callback function
var videos = [
{videoURL:"VIDEO ID or VIDEO URL here",containment:'body',autoPlay:true, mute:false, startAt:0,opacity:1, loop:false, ratio:"4/3", addRaster:true},
{videoURL: "VIDEO ID or VIDEO URL here",containment:'body',autoPlay:true, mute:true, startAt:0,opacity:1, loop:false, ratio:"4/3", addRaster:false},
{videoURL: "VIDEO ID or VIDEO URL here",containment:'body',autoPlay:true, mute:false, startAt:0,opacity:1, loop:false, ratio:"4/3", addRaster:true}
];
jQuery(".player").YTPlaylist(videos, false, function(video){
jQuery("#videoID").html(video.videoData.id);
jQuery("#videoTitle").html(video.videoData.title);
jQuery("#videoDesc").html(video.videoData.description);
});7.自定义播放器的所有可能选项。这些选项也可以通过数据-*属性。
jQuery(".player").YTPlayer({
/**
videoURL (string)
the complete Youtube video URL or the short url or the videoID
*/
videoURL: null,
/**
containment (string)
default containment for the player
*/
containment: 'body',
/**
ratio (string or number)
"auto", "16/9", "4/3" or number: 4/3, 16/9
*/
ratio: 'auto',
/**
fadeOnStartTime (int)
fade in timing at video start
*/
fadeOnStartTime: 1000,
/**
startAt (int)
start second
*/
startAt: 0,
/**
stopAt (int)
stop second
*/
stopAt: 0,
/**
autoPlay (bool)
on page load video should start or pause
*/
autoPlay: true,
/**
coverImage (string)
The path to the image to be used as cover if the autoPlay option is set to false
*/
coverImage: false,
/**
loop (bool or int)
video should loop or not; if number it will loop for the specified times
*/
loop: true,
/**
addRaster (bool)
shows a raster image over the video (added via CSS)
You can change the raster image via CSS:
.YTPOverlay.raster { background: url(images/raster.png)}
*/
addRaster: false,
/**
mask (bool or object) the key is the second and the value is the path to the image
Ex: mask:{ 0:'assets/mask-1.png', 5:'assets/mask-2.png', 30: false, 50:'assets/mask-3.png'}
*/
mask: false,
/**
opacity (int)
0 to 1
*/
opacity: 1,
/**
quality (string)
@deprecated
setPlaybackQuality has been deprecated on the YT API and doesn't work anymore
âsmallâ, âmediumâ, âlargeâ, âhd720â, âhd1080â, âhighresâ, "default"
*/
quality: 'default',
/**
vol (int)
0 to 100
*/
vol: 50,
/**
mute (bool)
mute the video at start
*/
mute: false,
/**
showControls (bool)
shows the control bar at the bottom of the containment
*/
showControls: true,
/**
anchor (string)
center,top,bottom,left,right combined in pair
*/
anchor: 'center,center',
/**
showAnnotations (bool)
display the annotations on video
*/
showAnnotations: false,
/**
cc_load_policy (bool)
display the subtitles
*/
cc_load_policy: false,
/**
showYTLogo (bool)
display the Youtube logotype inside the button bar
*/
showYTLogo: true,
/**
useOnMobile (bool)
activate the player also on mobile
*/
useOnMobile: true,
/**
playOnlyIfVisible (bool)
play the video only if the containment is on screen
*/
playOnlyIfVisible: false,
/**
onScreenPercentage (bool)
percentage of the player height the video should stop or start when visible
*/
onScreenPercentage: 30,
/**
* goFullScreenOnPlay (bool)
* if the player containment is set to "self" this allow the video to go fullscreen when played
*/
goFullScreenOnPlay: false,
/**
stopMovieOnBlur (bool)
stop the video if the window loose the focus
*/
stopMovieOnBlur: true,
/**
realfullscreen (bool)
the video when in full screen covers all the display
*/
realFullscreen: true,
/**
optimizeDisplay (bool)
The video always fit the containment without displaying the black strips
*/
optimizeDisplay: true,
/**
abundance (bool)
the abudance of the video size
*/
abundance: 0.3,
/**
gaTrack (bool)
track the video plays on GA
*/
gaTrack: true,
/**
remember_last_time (bool)
when the page is reloaded the video will start from the last position
*/
remember_last_time: false,
/**
addFilters (bool or string)
add one or more CSS filters as object to the video
Ex: {sepia: 50, hue_rotate : 220}
*/
addFilters: false,
/**
useNoCookie (bool)
use https://www.youtube-nocookie.com host to serve the video
*/
useNoCookie: true,
/**
onReady (function)
a callback function fired once the player is ready
*/
onReady: function (player) {
},
/**
onReady (function)
a callback function fired if there's an error
*/
onError: function (player, err) {
},
/**
onEnd (function)
a callback function fired when the video ends
*/
onEnd: function () {
}
});8.更多API方法。
// change the video of the specified player
jQuery(".player").YTPChangeMovie(opt);
// play the video
jQuery(".player").YTPPlay();
// pause the video
jQuery(".player").YTPPause();
// stop the video
jQuery(".player").YTPStop();
// seek to 20s
jQuery('#[playerID]').YTPSeekTo(20);
// Manage video speed
jQuery(".player").YTPSetPlaybackRate()
jQuery(".player").YTPGetPlaybackRate()
// switch video from background to front
jQuery(".player").YTPFullscreen();
// change the video volume
jQuery(".player").YTPSetVolume(val);
// mute the audio
jQuery(".player").YTPMute();
// unmute the audio
jQuery(".player").YTPUnmute();
// switch from mute to unmute
jQuery(".player").YTPToggleVolume();
// retrive the original player returned by the YouTube API
jQuery(".player").YTPGetPlayer();
// return the info data of the current video as JSON.
jQuery(".player").YTPGetVideoData();
// available only if you are playing a video list;
// goes to the next video in the play list.
jQuery(".player").YTPPlayNext();
// available only if you are playing a video list;
// goes to the previous video in the play list.
jQuery(".player").YTPPlayPrev();
// available only if you are playing a video list; goes to a specific video in the play list based on the index.
jQuery(".player").YTPPlayIndex(idx);
// return the actual video time.
jQuery(".player").YTPGetTime();
// return the total video time.
jQuery(".player").YTPGetTotalTime();
// return the actual anchor point for the video.
jQuery(".player").YTPGetAnchor();
// define how the video will behave once the window is resized
// possible value are: âtop,bottom,left,right,centerâ
// it accepts a pair of value comma separated (ex: âtop,rightâ or âbottom,leftâ)
jQuery(".player").YTPSetAnchor(posX,posY);
// apply a CSS filter to the player:
// grayscale : 1-100,
// hue_rotate: 1-360,
// invert : 1-100,
// opacity : 1-100,
// saturate : 1-400,
// sepia : 1-100,
// brightness: 1-400,
// contrast : 1-400,
// blur : 1-100
jQuery(".player").YTPApplyFilter('sepia', 50);
// apply multiple CSS filters to the player
jQuery(".player").YTPApplyFilters(filters);
// toggle the filter from the passed value to 0 and vice-versa
jQuery(".player").YTPtoggleFilter(filter, value);
// toggle all the filters
jQuery(".player").YTPToggleFilters(callback);
// remove a filter
jQuery(".player").YTPRemoveFilter(filter, callback);
// disable all filters
jQuery(".player").YTPDisableFilters();
// enable filters
jQuery(".player").YTPEnableFilters();
// add a mask to the target video player
jQuery(".player").YTPAddMask("path/to/mask.jpg");
// remove the mask
jQuery(".player").YTPRemoveMask();
// toggle the mask
jQuery(".player").YTPToggleMask();
// Change the cover image
jQuery(".player").changeCoverImage(img);9.活动主持人。
// properties:
videoData.id
videoData.channelTitle
videoData.title
videoData.description
videoData.thumb_max
videoData.thumb_high
videoData.thumb_medium
jQuery(".player").on("YTPUnstarted",function(e){
// do something here
});
jQuery(".player").on("YTPBuffering",function(e){
// do something here
});
jQuery(".player").on("YTPReady",function(e){
// do something here
});
jQuery(".player").on("YTPStart",function(e){
// do something here
});
jQuery(".player").on("YTPPlay",function(e){
// do something here
});
jQuery(".player").on("YTPPause",function(e){
// do something here
});
jQuery(".player").on("YTPEnd",function(e){
// do something here
});
jQuery(".player").on("YTPFullScreenStart",function(e){
// do something here
});
jQuery(".player").on("YTPFullScreenEnd",function(e){
// do something here
});
jQuery(".player").on("YTPMuted",function(e){
// do something here
});
jQuery(".player").on("YTPUnmuted",function(e){
// do something here
});
jQuery(".player").on("YTPTime",function(e){
// do something here
});
jQuery(".player").on("YTPData",function(e){
// do something here
});
v3.3.9稳定(2022-05-29)
v3.3.8稳定(2021-01-10)
第3.3.7版(2020-12-04)
第3.3.6版(2020-11-27年)
第3.3.5版(2020-11-11)
第3.3.4版(2020-11-01)
第3.3.3版(2020-06-07)
第3.3.2版(2020-05-21)
第3.3.1版(2020-04-17)
第3.3.1版(2020-04-10)
第3.3.1版(2020-03-18)
第3.3.1版(2020-01-13)
版本3.2.11(2019-12-16)
版本3.2.10(2019-06-21)
第3.2.9版(2019-02-25)
第3.2.8版(2018-12-03)
第3.2.7版(2018年10月10日)
第3.2.6版(2018-10-05)
第3.2.5版(2018年9月24日)
第3.2.5版(2018-09-07)
第3.2.4版(2018-07-13)
第3.2.3版(2018-06-21)
版本3.2.2(2018-06-20)
v3.2.1(2018年5月30日)
v3.1.13(2018年4月07日)
v3.1.12(2018年1月31日)
v3.1.11(2018年1月28日)
v3.1.9(2018年1月24日)
v3.1.8(2018年1月19日)
v3.1.6(2018年1月17日)
v3.1.5(2018年1月16日)
第3.1.2版(2017-11-17)
v3.1.1(2017年10月08日)
v3.1.0(2017年9月27日)
3.0.20版(2017年8月19日)
版本3.0.19(2017-05-12)
3.0.18版(2017-05-11)
3.0.17版(2017-05-06)
3.0.16版(2017年4月26日)
3.0.15版(2017年4月24日)
版本3.0.14(2017年4月19日)
3.0.12版(2017-04-05)
版本3.0.12(2017-03-02)
3.0.11版(2017年2月22日)
版本3.0.10(2017-01-08)
3.0.8版(2016年7月26日)
3.0.6版(2016-07-07)
v3.0.3(2016年6月10日)
3.0.1版(2016年5月20日)
v3.0.0(2016年3月27日)
v2.9.5版本(2016-03-11)
v2.9.13(2016年2月6日)
v2.9.11(2016年1月21日)
v2.9.10版本(2016-01-09)
v2.9.9版本(2016-01-07)
v2.9.8版本(2015-12-08)
v2.9.5版本(2015年10月25日)
v2.9.4版本(2015-10-08)
v2.9.3版本(2015-06-28)
v2.9.0版本(2015-05-09)
2015-04-26
2015-04-23
2015-04-21
2015-04-20
v2.8.0版本(2015-01-31)
v2.7.9版本(2015-01-12)
v2.7.8版本(2014-12-14)
v2.7.5版本(2014-11-16)
v2.7.2版本(2014-08-23)
v2.7.0版本(2014-08-19)
v2.6.9版本(2014-06-23)
v2.6.8版本(2014-06-23)
v2.6.7版本(2014-05-13)
v2.6.6版本(2014-03-31)
v2.6.3版本(2014-03-19)
v2.6.2版本(2014-01-28)
v2.6.0版本(2013-11-24)
v2.5.9版本(2013-11-21)
v2.5.8版本(2013-11-13)
v2.5.7版本(2013-08-31)
v2.5.4版本(2013-05-31)
v2.5.2版本(2013-05-31)