简体   繁体   English

CSS Display:没有更好的解决方案吗?

[英]CSS Display: none a better solution?

I am using DIVI, Wordpress and I have this video I set as a background for Desktop but I disable it for Mobile and Tablets but here's the thing, what the Disable option does is just a display:none so the video is still being loaded DOM. 我正在使用DIVI,Wordpress,并且我将此视频设置为桌面的背景,但在移动设备和平板电脑上却禁用了它,但实际上,“禁用”选项的作用只是显示:无,因此仍在加载视频DOM 。

Is there a way to totally block an object from loading? 有没有办法完全阻止对象加载? so it does affect the loading time on mobile devices? 会影响移动设备的加载时间吗?

Share your thoughts, Please 分享您的想法,请

Yes, flip your logic: 是的,翻转您的逻辑:

Never load the clip, unless it's a capable device. 除非它是有能力的设备, 否则切勿加载该剪辑。

You can use javascript to check if the screen width is broad enough (or even better: Check if the device is capable of your clip, some tablets in a wifi can often display it as well). 您可以使用javascript检查屏幕宽度是否足够宽(甚至更好:检查设备是否支持您的剪辑,wifi中的某些平板电脑通常也可以显示它)。
If you decide it can be played, add a video element to the page (or add the src to your element) and trigger a play event. 如果您决定可以播放,请在页面上添加视频元素(或将src添加到元素)并触发播放事件。

BTW: This trick can be applied to many resources, eg images as well. 顺便说一句:这个技巧可以应用于许多资源,例如图像。

Based on the suggestions Ian kindly made, here is my working solution. 根据Ian的建议,这是我的工作解决方案。

Firstly, I changed each video's child source elements to have an attribute data-src like so: 首先,我将每个视频的子源元素更改为具有data-src属性,如下所示:

<video id="my-id">    
   <source data-src="somevideo.mp4">
</video>

Then, after performing a mobile check using the script available at http://detectmobilebrowsers.com/ which I modified to include iPads etc (related SO answer here) I simply used the following script to automatically update the src attribute of each video (if we're on desktop in my case): 然后,使用http://detectmobilebrowsers.com/上提供的脚本执行移动检查后,我将其修改为包括iPad等(此处有相关的SO答案),我只是使用以下脚本自动更新每个视频的src属性(如果就我而言,我们在台式机上):

var sources = document.querySelectorAll('video#my-id source');
// Define the video object this source is contained inside
var video = document.querySelector('video#my-id');
for(var i = 0; i<sources.length;i++) {
  sources[i].setAttribute('src', sources[i].getAttribute('data-src'));
}
// If for some reason we do want to load the video after, for desktop as opposed to mobile (I'd imagine), use videojs API to load
video.load(); 

And that's it! 就是这样! Nothing loads on mobile devices anymore and I can have fairly granular control over the devices it will or won't load on. 移动设备上不再加载任何东西,我可以对其将要加载或将不会加载的设备进行相当精细的控制。

Hope this helps somebody. 希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM