2015-10-18 74 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(); 
} 
相关问题