javascript - JS window width html5 video element responsive -


alright have been trying lot of different things can't work. asking here.

i have 3 video elements on page. 1 desktop, tablet , smartphone. need remove video elements depending on screen size.

so when screen size < 767: smartphone = visible tablet = removed desktop = removed  screen size >=767 && <980 smartphone = removed tablet = visible desktop = removed  screen size >=980 smartphone = removed tablet = removed desktop = visible 

css media queries aint working because video elements keep playing. yes on mobile prevented autoplaying on desktop browser resize plays should not happen. display:none; out of question.

so need create js magic guess. have far trying fill in blank pages can come far did not result in desired effect.

$(document).ready(function () { $(window).resize(function () {     if ($(this).width() < 767)         should write here     else if ($(this).width() >= 767 && $(this).width() < 980)                     should write here                 else if ($(this).width() >= 980)                                 should write here             }); }); 

http://jsfiddle.net/36unpxj0/2/

in above fiddle can see code entirely , fiddle around it. thank helping.

ps: jquery solutions welcome pure js please tell if js or jquery.

edit 1:

as usual going keep searching , adding info post! found following trick:

$("#video-id").first().attr('src','') 

or

$("#video-id").empty().remove(); 

i tried both no luck: see fiddle http://jsfiddle.net/36unpxj0/3/

edit 2:

alright got answer citizendelta , working in jsfiddle therefore have accepted answer. not working inside joomla website. going keep looking.

here demo, hope, need : demo

steps :

  1. i hide every video (pauseandhideall function)
  2. i detect screen (showthegoodone function)
  3. i show , play video correpsonding

in javascript, have made :

$(document).ready(function () {  function pauseandhide($element) {     $element.get(0).pause();     $element.hide(); }  function showthegoodone() {     if ($(window).width() < 767) {         $('#smartphonevid').show();         $('#smartphonevid').get(0).play();     } else if ($(window).width() >= 767 && $(this).width() < 980)   {         $('#tabletvid').show();         $('#tabletvid').get(0).play();     } else if ($(window).width() >= 980)  {         $('#desktopvid').show();         $('#desktopvid').get(0).play();     }  }  function pauseandhideall() {        $('video').each(function() {         pauseandhide($(this));              });      }  pauseandhideall(); showthegoodone();  $(window).resize(function () {     pauseandhideall();     showthegoodone(); }); }); 

optionnal improvment :

maybe detect current time of video playing, resume other video time.


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -