views:

230

answers:

2

How can I overlay a div with semi-transparent opacity over a youtube iframe embedded video?

<iframe class="youtube-player" type="text/html" width="520" height="330" src="http://www.youtube.com/embed/NWHfY_lvKIQ" frameborder="0"></iframe>
<div id="overlay"></div>

CSS

#overlay {
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background:#000;
    opacity:0.8;
    /*background:rgba(255,255,255,0.8); or just this*/
    z-index:50;
    color:#fff;
}

edit (added more clarification): HTML5 is approaching us, with more and more devices that use it instead of flash, which complicates the embedding of youtube videos, thankfully youtube provides a special embeddable iFrame with handles all of the video embedding compatibility issues, but now the previously working method of overlaying a video object with a semi-transparent div is no longer valid, I am now unable to add a <param name="wmode" value="transparent"> to the object, because it is now a iFrame, so how do I add a opaque div on top of the iframe embedded video?

solution: 1. get contents from youtube link 2. create page with altered content (and cache it) 3. use iframe with altered page

object markup (for personal reference):

<!doctype html>
<html>
    <body>
<div id="overlay"></div>
<object width="640" height="385"><param name="wmode" value="transparent"><param name="movie" value="http://www.youtube.com/v/NWHfY_lvKIQ?fs=1&amp;amp;hl=en_US"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed wmode="transparent" src="http://www.youtube.com/v/NWHfY_lvKIQ?fs=1&amp;amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>

<script>
    document.onclick = function(e) {
    //e.preventDefault();
    }
</script>
<style>
    * { margin:0; padding:0; }
    #overlay {
    position:fixed;
    width:100%;
    height:100%;
    z-index:5;
    opacity:0.5;
    background:#000;
    }
</style>
</body>
</html>
+3  A: 
meder
Yea, I found out what the problem was, in firefox and chrome it works with normal backgrounds, adding the opacity makes it shine through... yours also shines through.
YuriKolovsky
edited the iframe example to look the way that mine looks. http://jsfiddle.net/fdsaP/6/
YuriKolovsky
So... you want the opacity to apply to the object/embed element inside of the iframe?
meder
yes, I did not notice that the iframe was overlayed because for me the object is covering the full iframe, I need the video to be covered if its possible, else ill replace it with a black div of the same width/height and position.
YuriKolovsky
+1 it's wmode which is the problem with flash
annakata
@meder thanks for the link and solution! will try it out right now, but the flash is still click-able though :)
YuriKolovsky
@YuriKolovsky - I fail to see where you specified that you didn't want it to be clickable?
meder
@YuriKolovsky - since I have a better idea of what you would like now, try http://jsfiddle.net/fdsaP/16/. I embed the opacity layer in the iframe page.
meder
@meder, the last #16 jsfiddle looks great, although doing it this way pretty much removes the whole point of the youtube iframe embedding method, http://youtubeking10.blogspot.com/2010/07/embed-youtube-vidoes-with-iframe.html :)
YuriKolovsky
Yeah, I'm afraid you really don't have much other options unless I'm aware of something. Though I just recently thought of actually displaying an overlay SWF that takes up 100% of the page and has the opacity layer, which should float over the iframe's object. One could also setup some dynamic service which returns the html source of my yt2.html page modified to serve the overlay or somethin' like that..
meder
or I can try asking youtube/google nicely to add the wmode transparent option :)
YuriKolovsky
You could also try invoking `preventDefault` on the document's click event... too sleepy to think of any more ideas though
meder
thank you for the ideas meder :) your option B would be best, option A is a no-go, because I might as well just embed the video right on the page, this issue that I'm describing will probably get solved by youtube if they notice it.
YuriKolovsky
Even if you get youtube to render `wmode=transparent` on their video, it will still be clickable without further action. What's wrong with just embedding the code, again? Do you really need the entire youtube page in the iframe?
meder
The idea behind iframing video instead of embedding video is that it puts the responsibility of maintaining the video onto youtubes side, imagine if some new better way of embedding videos came out, this way I don't need to update any the vids on my sites.
YuriKolovsky
I have another theoretical solution, maybe I could somehow automatically take a screenshot of the iframe video with javascript and then replace the iframe with the screenshot while the opacity is overlaying it? its not as fancy as overlaying a video that is playing, but it would be better than a plain black box or the click-through video.
YuriKolovsky
Actually I had the same idea (screenshotting) but it would involve hosting the image, which didnt seem ideal given you just wanting to iframe src it solely.
meder
@YuriKolovsky - if you're not serving this to hundreds of thousands of people and it isn't resource intensive, then my idea which I proposed, maybe you didnt understand it fully, still stands I think. It was that you could do `<iframe src="file.php?url=http://www.youtube.com/blah">` where your `file.php` grabs the contents of the youtube page, renders them fully, and just simply adds the wmode and/or the overlay. If youtube ever changed the method you'd just have to update this one page alone and it would be fixed all across the board wherever its referenced.
meder
If the above is resource intensive though, then you could easily use some caching system which would lookup urls that havent been indexed in say, the last week or so... and the new ones would be appended to that cache.
meder
oh now I see it, I could even host this on some dedicated page of my own that provides the "fixed" youtube markup! currently it's really not intensive with "admin only" usage and caching would work great. Thanks again for your original solution that you might even document on your bugs archive website :)
YuriKolovsky
I actually wrote an article already since technically it isn't really a "bug" but just an odd behaviour. But yeah, the site needs a lot of work and the article probably needs to be modified.
meder
+1  A: 

Is the opaque overlay for aesthetic purposes?

If so, you can use:

#overlay {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 50;
        background: #000;
        pointer-events: none;
        opacity: 0.8;
        color: #fff;
}

'pointer-events: none' will change the overlay behavior so that it can be physically opaque. Of course, this will only work in good browsers.

lofto
safari 4, chrome and firefox3.6 still clicks through??
YuriKolovsky
Isn't that wat you want?
Mr. Matt
I'm trying to put the youtube embedded video behind a opaque background for completely aesthetic purposes :)
YuriKolovsky
In that case, your original code works fine.
lofto
no it does not? http://jsfiddle.net/fdsaP/8/
YuriKolovsky