What is the best way to remove html5 video background for mobile users

北城以北 提交于 2019-12-10 12:47:25

问题


EXPLANATION: A customer of mine wants to have a background video running on his responsive website. However he would also like to remove it for tablet/mobile users. I know this can be done with media queries, but the video would still load as part of the DOM and that is what i would like to prevent.

QUESTIONS:

  1. Can the video element be removed using JavaScript/jQuery from the DOM when it loads view-port at certain widths?

  2. Can the same video be recovered when if the view port is manually increased in with? (i suspect this is a bad approach)

  3. Will a video with "display:none;" still affect loading/battery time on a tablet/mobile ?

Thanks you for you assistance.


回答1:


Based on mobile dimensions use $('video').remove(). this will removes the DOM element from webpage. so it will not render in html.




回答2:


See this answer to detect if you're on a mobile device.

Then, using this test, you can .hide() your element using jQuery, or set its src attribute to "", to be sure it's not downloading.




回答3:


You could also use css. This is much easier.

@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) {
    video {
        display:none;
    }
}

Then have an image with a negative z-index, that way when the video is not displayed, the image will appear.



来源:https://stackoverflow.com/questions/24609127/what-is-the-best-way-to-remove-html5-video-background-for-mobile-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!