2015-02-11 120 views
1

您好我想使用的fancybox在我提出包含視頻的網頁,像影片畫廊,我的意圖是要在鏈接的YouTube視頻。的JQuery的fancybox有關iframe問題(PHP)

的問題,我得到它只是通過鏈接到年齡鏈接一個正常的a標籤會做,沒有別的事情發生。

這是我用來生成視頻的代碼。

while ($row = mysqli_fetch_assoc($result)){ 
     echo " 
      <div class='portalVideo'> 
        <a class='fancybox fancybox.iframe' href='https://www.youtube.com/watch?v=DB7jsjt4Uec'> 
         <h3> 
          <strong>".$row["VideoName"]."</strong> 
         </h3> 
        </a> 
      </div>"; 
    } 

我所有的視頻鏈接是從數據庫中抽取,然後作爲一個文本鏈接,點擊時「應該」打開iframe顯示在頁面上。唯一不起作用的是iframe。其他所有內容都會正確顯示並顯示。

我已經正確地包含了這些文件,並且它們正確鏈接到資源。只爲鞏固見下文。

<script type="text/javascript" src="scripts/jquery.fancybox-1.3.4.pack.js"></script> 
<link rel="stylesheet" type="text/css" href="css/jquery.fancybox-1.3.4.css"> 

基於關閉this stack overflow post我試圖解決它。我的代碼包含下面的頭標記下面的jquery。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('a.fancybox').fancybox(); 
    }); 
</script> 

回答

1

你有兩個(無PHP相關的)問題:

1)。這的YouTube格式:

https://www.youtube.com/watch?v=DB7jsjt4Uec 

....不是一個iframe因爲很有可能它含有DENYX-Frame-options響應頭的支援。您可能需要將其轉換成嵌入格式,如:

https://www.youtube.com/embed/DB7jsjt4Uec 

2)。特殊類fancybox.iframe僅在fancybox v2.x中受支持,但您似乎正在使用fancybox v1.3.x。所以你寧可使用iframe或添加API選項

type: "iframe" 

...您的自定義的fancybox初始化腳本。

既然你是從數據庫中提取的視頻鏈接,好消息是,你不必在你的PHP代碼,但只有在你的jQuery代碼就像改變什麼:

jQuery(document).ready(function ($) { 
    $(".fancybox").each(function (i) { 
     // change links on the fly to embed format 
     this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'embed/') + "?autoplay=1"; 
    }).fancybox({ 
     // API options 
     type: "iframe" // needed for videos embed format 
    }); 
}); // ready 

通知我我將?autoplay=1添加到新的href,但如果您希望您的視頻autoplay曾經在fancybox的iframe中一次,那麼這是可選的。

JSFIDDLE