Well, thatβs how it will look like this:
HTML
<div id="accordion"> <h3>Video 1</h3> <div class="content" data-link="//www.youtube.com/embed/nlcIKh6sBtc"> <iframe width="200" height="200" src="" frameborder="0"></iframe> </div> <h3>Video 2</h3> <div class="content" data-link="//www.youtube.com/embed/My2FRPA3Gf8"> <iframe width="200" height="200" src="" frameborder="0"></iframe> </div> <h3>Video 3</h3> <div class="content" data-link="//www.youtube.com/embed/CevxZvSJLk8"> <iframe width="200" height="200" src="" frameborder="0"></iframe> </div> </div>
Javascript
var elements = document.getElementsByClassName("content"), bubbles = false; var observer = new WebKitMutationObserver(function (mutations) { mutations.forEach(attrModified); }); for(var i = 0; i < elements.length; i++){ observer.observe(elements[i], { attributes: true, subtree: bubbles }); } function attrModified(mutation) { var contentStyle = mutation.target.getAttribute("style"); var IsVisible = contentStyle.indexOf("block") != -1; if(!IsVisible){ var $currentIframe = $(mutation.target).children("iframe"); $currentIframe.attr("src", ""); } else{ var link = $(mutation.target).data("link"); var $currentIframe = $(mutation.target).children("iframe"); $currentIframe.attr("src", link); } } $("#accordion").accordion();
Example: AccordionLoadFrame
source share