在新聞網站中,我們針對國外用戶,需要引入第三方視頻,
首先引入youtube視頻
我們拷貝youtube視頻網址
https://www.youtube.com/watch?v=oK6k9O65QAs或則
https://youtu.be/oK6k9O65QAs
我們可以看到他的規則 每個youtube視頻都由特殊的id oK6k9O65QAs 就是這個,我們取到他
參考youtube的視頻規則
也可以參考 https://developers.google.com/youtube/iframe_api_reference?hl=zh-cn youtube視頻api(需翻牆)
<iframe type="text/html" src="http://www.youtube.com/embed/oK6k9O65QAs frameborder="0"></iframe>
可以給iframe定義自己的類名控制樣式只需要被iframe嵌入到網站任何地方都可以出現視頻,可以自定義視頻樣式
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '360',
width: '640',
videoId: 'oK6k9O65QAs',//唯一的id
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
這是通過js控制視頻播放,可以控制播放以及暫停,也是通過唯一 oK6k9O65QAs的來判斷
下邊講解facebook視頻我們找一個facebook視頻地址如
https://www.facebook.com/ExtremeLoveShow/videos/598167423879792/
<div class="fb-video" data-href="https://www.facebook.com/ExtremeLoveShow/videos/598167423879792/" data-width="auto" data-show-text="false"></div>
最後調用這段js代碼,注意要分清順序,必須向有了 class爲fb-video,的元素纔可以出視頻,上邊的視頻是根據父元素寬度自適應的,你也可以自己指定視頻
<script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>也可以直接拷貝facebook視頻的嵌入就像下邊
嵌入的視頻,