17. May 2009 by Janko in Tutorials | tags: ,

Some videos on YouTube have a cool feature called "Turn the lights down". Basically, when you turn lights down, the entire page darkens and let you watch video as if you are in the cinema. This tutorial will show you how to implement this simple effect.

View live demo Download source code

The problem

Our example is simple - it consists of header, the video, "turn off the lights" link and sidebar with some information about the video. At the code below you can se the "command" div that contains lightSwitcher link, "movie" div that contains the video and "description" div that acts as a sidebar:

    <div id="container">
        <div id="header">
            <h1>Janko At Warp Speed</h1>
            <h2>Turn off the lights - demo</h2>
            <div id="command">
                <a class="lightSwitcher" href="#">Turn off the lights</a>
            </div>
        </div>
        <div id="movie">
            <object width="560" height="340">
                <param name="movie" value="http://www.youtube.com/v/Mlahvvymkxc&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999" />
                <param name="allowFullScreen" value="true" />
                <param name="allowscriptaccess" value="always" />
                <embed src="http://www.youtube.com/v/Mlahvvymkxc&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999"
                    type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340">
                </embed>
            </object>
        </div>
        <div id="description">
            <p>Some description ges here</p>
        </div>
    </div>

CSS for this is simple:

body {font-family:Arial, Sans-Serif; font-size:13px; text-align:center; margin:0px; }
#container { width:960px; margin:0px auto; text-align:left; overflow:hidden;}
#movie {border:solid 1px #dcdcdc; float:left; width:560px; text-align:left; margin-right:20px;}
#description { float:left; width:320px;border:solid 1px #dcdcdc; padding: 10px 20px;}
.lightSwitcher {background-image:url(light_bulb_off.png); background-repeat:no-repeat; background-position:left;
                       padding: 0 0 0 20px; outline:none; text-decoration:none;}
.lightSwitcher:hover {text-decoration:underline;}

Let there be... dark

When link "Turn off the lights" is clicked, we need to darken the entire page. All except the movie. This can be achieved using a div that has to have the exact dimensions as the current document and semi-transparent black background. This div needs to be positioned absolutely inside its relatively positioned container (and that is BODY) and to stretch from the upper-left to the bottom-right corner.

Let's add a div to the end of our markup:

<div id="shadow"></div>

Now let's style it:

#shadow {background-image:url(shade1x1.png); position:absolute; left:0; top:0; width:100%; z-index:100;}

Important thing here is z-index. In order to make it cover the entire page, it has to have the largest z-index. Embedded element will be seen, anyway. It's easy to position a div to the top-left corner and set the width to 100%. But what about the height? If we set the height of our shadow div to be 100% it will cover the entire page only if contains larger than a window, or - if we have scrollbars. Otherwise, it will cover just the content.

Let's involve jQuery

$(document).ready(function(){
    $("#shadow").css("height", $(document).height()).hide();
});

Aaaaand.... Action!

This code set the height of shadow div to the same value as document height and hides it. Of course, we want this div visible, only when we click on "lightSwitcher". Now we need to ad a click handler for lightSwitcher:

$(".lightSwitcher").click(function(){
    $("#shadow").toggle();
});

If you try to run this now you will see that it works. LightSwitcher will toggle shadow div visibility and that will simulate turning lights on and off. The only problem is that the link itself will be "in the dark", and you won'ta be ble to click on it again, once you turn the lights off.

The solution is simple; LightSwitcher has to have z-index higher than shadow div. In order to do that, we have to position the link absolutely inside the relatively positioned container and set z-index to 101:

#command { position:relative; height:25px; display:block;}
.lightSwitcher {position:absolute; z-index:101; background-image:url(light_bulb_off.png);
                      background-repeat:no-repeat; background-position:left; padding: 0 0 0 20px;
                      outline:none; text-decoration:none;}

Now it will work. If you look at the demo you will notice that while you toggle the lights, link text and icon changes. In order to do that we have to extend our CSS and jQuery a little bit. When you turn off the lights, light bulb icon and text changes, and link color turn to yellow. We need to define a CSS class that will style the link and add some jQuery to change text and toggle this CSS class.

.turnedOff {color:#ffff00; background-image:url(light_bulb.png);}

We'll extend the click handler a little bit to get this result:

$(".lightSwitcher").click(function(){
    $("#shadow").toggle();
        if ($("#shadow").is(":hidden"))
            $(this).html("Turn off the lights").removeClass("turnedOff");
        else
            $(this).html("Turn on the lights").addClass("turnedOff");
});

Now we have fully functional light switcher functionality. In the end, the complete code will look like this:

CSS

body {font-family:Arial, Sans-Serif; font-size:13px; text-align:center; margin:0px; position:relative;}
#container { width:960px; margin:0px auto; text-align:left; overflow:hidden; position:relative;}
#movie {border:solid 1px #dcdcdc; float:left; width:560px; text-align:left; margin-right:20px;}
#description { float:left; width:320px;border:solid 1px #dcdcdc; padding: 10px 20px;}
#command { position:relative; height:25px; display:block; margin: 25px 0 0 0;}
.lightSwitcher {position:absolute; z-index:101; background-image:url(light_bulb_off.png);
                      background-repeat:no-repeat; background-position:left; padding: 0 0 0 20px;
                      outline:none; text-decoration:none;}
.lightSwitcher:hover {text-decoration:underline;}
#shadow {background-image:url(shade1x1.png); position:absolute; left:0; top:0; width:100%; z-index:100;}
.turnedOff {color:#ffff00; background-image:url(light_bulb.png);}

jQuery

$(document).ready(function(){
    $("#shadow").css("height", $(document).height()).hide();
    $(".lightSwitcher").click(function(){
        $("#shadow").toggle();
        if ($("#shadow").is(":hidden"))
            $(this).html("Turn off the lights").removeClass("turnedOff");
        else
            $(this).html("Turn on the lights").addClass("turnedOff");
    });
});

Update: Thanks to @Joranovski fix for Mac would be: "position:relative; and the z-index 101 or 102 for the Movie div".

Do you like this effect? I think it's really funny!

Comments

Pingbacks and trackbacks

  1. Trackback from DotNetShoutout Use jQuery to "turn off the lights" while watching videos
  2. Pingback from designm.ag Use jQuery to “turn off the lights” while watching videos
  3. Trackback from progg.ru Эфект выключенного света, во время просмотра видео
  4. Pingback from n3t.ir Use jQuery to “Turn Off the Lights” While Watching Videos
  5. Pingback from design-newz.com Use jQuery to “turn off the lights” while watching videos | Design Newz
  6. Pingback from smallfarmdesign.com Small Farm Design » Blog Archive » links for 2009-05-19
  7. Pingback from markdvl.com Realizzare l’effetto “turn off the lights” con jQuery | Markdvl Killer Web Designer
  8. Pingback from andysowards.com #WDNDL For 5/20/2009 - CSS, WordPress, and Type Edition! | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?
  9. Pingback from netcrema.com Use jQuery to “turn off the lights” while watching videos « Netcrema - creme de la social news
  10. Pingback from fabianlink.com Fabián Link » Use jQuery to “turn off the lights” while watching videos
  11. Pingback from cnet.ro Webmasteri: articole ÅŸi resurse utile | CNET.ro
  12. Pingback from garcon.cz Link Backup from Delicious.com » Blog Archive » links for 2009-05-21
  13. Pingback from qianduan.net 看电影时请关灯——一个很酷的页面效果| Ajax| 前端观察
  14. Pingback from arbent.net Arbenting's Weekly Inspiration and Best of the Web #6 | Arbenting
  15. Pingback from simonedamico.it Realizzare l’effetto “turn off the lights” con jQuery | Simone D'Amico
  16. Pingback from juliusdesign.net   jQuery: Effetto Buio in sala | JuliusDesign
  17. Pingback from effectivedevelopment.net Web Tools Tips and Talk: #7 | Effective Development
  18. Pingback from tutorial-pro.com Use jQuery to “turn off the lights” while watching videos - Tutorial Pro
  19. Pingback from elwebmaster.com Usa Jquery para "apagar las luces" al ver un video - elWebmaster.com
  20. Pingback from answerspluto.com list of urls - 5 « Answers Pluto
  21. Pingback from jquery.wisdomplug.com jQuery Tutorial – Use jQuery to “turn off the lights” while watching videos | jQuery Wisdom
  22. Pingback from gwzo.sitepedia.jp jQueryを使って電気の〝ON〟〝OFF〟を表現しよう! | Gzone
  23. Pingback from webdeveloperjuice.com CSS: Create YouTube-like adaptable view using CSS and jQuery « Web Developer Juice
  24. Pingback from linhadecomando.com JQuery: Efeito de Apagar a Luz | linhadecomando.com
  25. Pingback from susuzkuyu.com jQuery Tutorial – Use jQuery to turn off the lights while watching videos — susuzkuyu
  26. Pingback from blog.websourcing.fr [Webdesign] « Ã©teignez la lumière » avec jQuery pour lire vos vidéos - Websourcing.fr
  27. Pingback from moviesberry.com Nothing to Lose, 1997
  28. Pingback from viciao2k3.net Enlaces de la semana 14 — Viciao2k3
  29. Pingback from webexpedition18.com 4 Superb jQuery Plugins – Webexpedition18

Add comment

   
        Country flag
biuquote
Loading