2015-10-18 75 views
3

我正在做一個響應式網站,我有第一個屏幕的視頻背景,我想隱藏視頻在移動設備上,使網站加載更快。確定顯示:無;不會從網上下載禁用內容.. 我想這兩個JavaScript從裝在小屏幕上的內容禁用內容,但它仍然加載的背景:有沒有辦法阻止內容加載在最小寬度的屏幕

if (screen.width < 768){ 
var removeelement = document.getElementById("videoClass"); 
removeelement.innerHTML = ""; 
} 

或者:

$("#videoClass").remove(); 

他們做CSS標籤的相同工作:display-none。 有沒有什麼辦法在javascript中防止內容在後臺加載內容?

+1

它取決於你如何加載視頻,視頻加載前添加條件。 –

+1

閱讀[鏈接1](http://stackoverflow.com/questions/12158540/does-displaynone-prevent-an-image-from-loading)和[鏈接2](http://timkadlec.com/2012/04/media-query-asset-download-results /) – Alex

+0

我會做一個if語句來檢查是否從移動設備上查看站點。然後,相應地注入視頻代碼。 – Vandervidi

回答

0

我認爲解決這個內建自測的方法是像什麼Laxmikant丹哥VanderVidi說, 所有這些JavaScript和jqueries是罰款和工作,但問題是我如何加載視頻.. 我使用此代碼下載寬度> 768的屏幕上的內容

<video id="videoClass"> 
<!-- removed the content from here --> 
</video> 
<script> 
$(document).ready(function() { 
if (screen.width > 768){ 
var showElement = document.getElementById("videoClass"); 
//pasted the content here: 
showElement.innerHTML = '<source src="video.mp4" type="video/mp4">'; 
} 
}); 
</script> 

解決了這個問題,內容現在只在更大的屏幕上加載.. 非常感謝你的幫助。

1

添加將使所選div的html爲空的bellowed代碼。

if(window.innerWidth < 768)//screen.width < 768 
    { 
    $("#videoClass").html(""); 
    } 
1

你可以使用:

if (screen.width < 768){ 
    $("#videoClass").empty(); 
} 
相關問題