Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The easiest way to implement this behaviour is by calling the <code>pauseVideo</code> and <code>playVideo</code> methods, when necessary. Inspired by the result of my <a href="https://stackoverflow.com/questions/7443578/youtube-iframe-api-control-a-iframe-player-thats-already-in-the-html#7513356">previous answer</a>, I have written a pluginless function to achieve the desired behaviour.</p> <p>The only adjustments:</p> <ul> <li>I have added a function, <code>toggleVideo</code></li> <li>I have added <code>?enablejsapi=1</code> to YouTube's URL, to enable the feature</li> </ul> <p>Demo: <a href="http://jsfiddle.net/ZcMkt/" rel="noreferrer">http://jsfiddle.net/ZcMkt/</a><br /> Code:</p> <pre><code>&lt;script&gt; function toggleVideo(state) { // if state == 'hide', hide. Else: show video var div = document.getElementById("popupVid"); var iframe = div.getElementsByTagName("iframe")[0].contentWindow; div.style.display = state == 'hide' ? 'none' : ''; func = state == 'hide' ? 'pauseVideo' : 'playVideo'; iframe.postMessage('{"event":"command","func":"' + func + '","args":""}', '*'); } &lt;/script&gt; &lt;p&gt;&lt;a href="javascript:;" onClick="toggleVideo();"&gt;Click here&lt;/a&gt; to see my presenting showreel, to give you an idea of my style - usually described as authoritative, affable and and engaging.&lt;/p&gt; &lt;!-- popup and contents --&gt; &lt;div id="popupVid" style="position:absolute;left:0px;top:87px;width:500px;background-color:#D05F27;height:auto;display:none;z-index:200;"&gt; &lt;iframe width="500" height="315" src="http://www.youtube.com/embed/T39hYJAwR40?enablejsapi=1" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt; &lt;br /&gt;&lt;br /&gt; &lt;a href="javascript:;" onClick="toggleVideo('hide');"&gt;close&lt;/a&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload