2015-12-01 22 views
1

當插入fluidvids.js代碼之前視頻嵌入時,我無法讓fluidsvids.js在我的github頁面託管的網站(運行於Poole/Jekyll)上工作。爲什麼javascript(fluidvids.js)只能在*視頻嵌入後加載時才能工作?

但是,在之後插入代碼時,視頻會嵌入視頻,因爲fluidvids.js會將視頻調整爲佈局。 這是爲什麼?


我萃取整個fluidvids zip文件到/public/js/fluidvids

在我_includes文件夾中,我創建了一個HTML文件,fluidvids.html

<!-- fluidvids.js --> 
<script src="{{ site.baseurl }}public/js/fluidvids/dist/fluidvids.js"></script> 
<script> 
fluidvids.init({ 
    selector: ['iframe'], 
    players: ['www.youtube.com', 'player.vimeo.com'] 
}); 
</script> 

而且在我default.html佈局,我插{% include fluidvids.html %}

<!DOCTYPE html> 
<html lang="en-us"> 

    {% include head.html %} 
    {% include google_analytics.html %} 

    <body> 
    {% include fluidvids.html %} 
    {% include sidebar.html %} 

    <div class="content container"> 
     {{ content }} 
    </div> 

    </body> 
</html> 

最後,我的嵌入式測試的兩個視頻一個帖子; 2007-02-10-Island-of-Lost-Souls.md

<!DOCTYPE html> 
<html lang="en-us"> 

    {% include head.html %} 
    {% include google_analytics.html %} 

    <body> 
    {% include sidebar.html %} 

    <div class="content container"> 
     {{ content }} 
     {% include fluidvids.html %} 
    </div> 

    </body> 
</html> 

這是爲什麼:

<iframe src="https://player.vimeo.com/video/9685584" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> 
<iframe width="420" height="315" src="https://www.youtube.com/embed/ZdyzjKDWpYU" frameborder="0" allowfullscreen></iframe> 

但是,影片沒有與佈局,直到我把包括博客文章的內容後,這樣的調整?

Here's呈現的帖子,當fluidvid.js加載的博客文章內容。

回答

1

因爲您在調用fluidvids.init之前並未等待文檔準備就緒(onload)。如果該腳本塊位於視頻元素之前,您需要等待文檔onload事件,否則初始化將(默默無聞地)失敗,因爲由於瀏覽器沒有機會處理其標記,所以選擇器不會返回任何元素然而。 因此,要麼綁定到onload事件,然後運行fluidvids初始化,要麼在有問題的元素之後包含腳本塊。

順便說一句,最佳實踐表明,腳本塊將包含在body元素的末尾。

相關問題