{"id":1389,"date":"2013-07-03T09:59:35","date_gmt":"2013-07-03T16:59:35","guid":{"rendered":"http:\/\/www.designersgate.com\/blogs\/?p=1389"},"modified":"2013-07-03T09:59:35","modified_gmt":"2013-07-03T16:59:35","slug":"making-videos-responsive-screen-resolution-jquery","status":"publish","type":"post","link":"https:\/\/designersgate.com\/blog\/making-videos-responsive-screen-resolution-jquery\/","title":{"rendered":"Making your videos responsive to screen resolution with JQuery"},"content":{"rendered":"<p>This is a nice piece of code I found in <a title=\"Making You Tube iFrame Responsive\" href=\"http:\/\/stackoverflow.com\/questions\/7737557\/how-can-i-make-the-youtube-player-scale-to-the-width-of-the-page-but-also-keep-t\">Stack Overflow<\/a>, but I love the solution posted by Darwin in October 12 2012 and I tweaked to fit my needs and here is my code:<\/p>\n<pre>function adjustVideoRatioToParent(arr){\n    var element = (arr.e === undefined)? jQuery('iframe[src*=\"youtube\"]') : jQuery(arr.e); \/\/set element or elements\n    var eParent = element.parent(); \/\/capture parent\n    var mxWidth = eParent.width(); \/\/get parent width\n    var aRatio = (arr.r === undefined || (arr.r &amp;&amp; arr.r === 'ws'))? 'ws' : 'std'; \/\/set aspect ratio to be used\n    var useRatio;\n    \/\/setting ratio\n    switch(aRatio){\n        case 'std': useRatio = 3\/4; break;\n        default: useRatio = 9\/16;\n    }\n    element.css({'width':'100%','height':(mxWidth*useRatio)+'px'});\n}<\/pre>\n<p>The code will look for the parent of the iframe and will calculate the width of this parent and adjust the aspect ratio of the iframe. The parent could be the BODY of the document or any other element as DIV or ARTICLE or SECTION. You choose.<\/p>\n<p><strong style=\"font-size: 18px;\">Usage:<\/strong><\/p>\n<p>Use with any independent object like:<br \/>\n<code>&lt;iframe src=\"your.youtube.url\/id\" onload=\"adjustVideoRatioToParent({e:this,r:'std'});\"&gt;&lt;\/iframe&gt;<\/code><\/p>\n<p>Or just enter it in you jQuery(document).ready() function if all the videos in the screen will have the same aspect ratio.<\/p>\n<pre>jQuery(document).ready(function(){\n     adjustVideoRatioToParent({r:'std'}); \/\/for Standard Ratio\n     adjustVideoRatioToParent(); \/\/default for Widescreen Ratio\n     \/\/also you can use this\n     adjustVideoRatioToParent({e:'.standard',r:'std'});\n     adjustVideoRatioToParent({e:'.widescreen',r:'ws'});\n});<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1403\" alt=\"Making YouTube iFrame Responsive\" src=\"http:\/\/www.designersgate.com\/blogs\/wp-content\/uploads\/2013\/07\/responsiveYouTube.jpg\" width=\"846\" height=\"560\" srcset=\"https:\/\/designersgate.com\/blog\/wp-content\/uploads\/2013\/07\/responsiveYouTube.jpg 846w, https:\/\/designersgate.com\/blog\/wp-content\/uploads\/2013\/07\/responsiveYouTube-600x397.jpg 600w, https:\/\/designersgate.com\/blog\/wp-content\/uploads\/2013\/07\/responsiveYouTube-300x199.jpg 300w, https:\/\/designersgate.com\/blog\/wp-content\/uploads\/2013\/07\/responsiveYouTube-768x508.jpg 768w\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" \/><\/p>\n<p>A sample of a code use in a web I&#8217;m working on.<\/p>\n<p>How to make it with plain JavaScript? <a title=\"Making Responsive iFrame with plain JavaScript\" href=\"http:\/\/youtu.be\/HpH64BDBBbQ\" target=\"_blank\" rel=\"noopener noreferrer\">Just watch this video<\/a><\/p>\n<p><iframe id=\"widscreen\" src=\"\/\/www.youtube.com\/embed\/HpH64BDBBbQ?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Hope this helps!<br \/>\nHappy Coding<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a nice piece of code I found in Stack Overflow, but I love the solution posted by Darwin in October 12 2012 and I tweaked to fit my needs and here is my code: function adjustVideoRatioToParent(arr){ var element = (arr.e === undefined)? jQuery(&#8216;iframe[src*=&#8221;youtube&#8221;]&#8217;) : jQuery(arr.e); \/\/set element or elements var eParent = element.parent(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1417,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[51,13,7],"tags":[66,147,148,149,16,73,150,151,152,153,154],"class_list":["post-1389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-resources","category-tips","category-tutorials","tag-code","tag-dom","tag-elements","tag-iframe","tag-javascript","tag-jquery","tag-resources-2","tag-responsive","tag-tips-2","tag-tutorials-2","tag-youtube"],"acf":[],"jetpack_featured_media_url":"https:\/\/designersgate.com\/blog\/wp-content\/uploads\/2013\/07\/DG-RESPONSIVE-IFRAMES.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/1389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/comments?post=1389"}],"version-history":[{"count":0,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/1389\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/media\/1417"}],"wp:attachment":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/media?parent=1389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/categories?post=1389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/tags?post=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}