Monday, June 21, 2010

CSS3 Full-Screen Gradient Flashlight (jQuery)

Want to share a small tech-demo which simulates a moving flashlight (spotlight) over your web-site (without using Adobe Flash). The light is following mouse cursor. Uses jQuery. See the screenshot and the corresponding code:
<html>
 <head>
  <style>
   .fl {
    background-image: -moz-radial-gradient(center 45deg, circle closest-corner, rgba(255, 255, 255, 0) 0%, rgba(0, 0, 0, 255) 10%);
       background-image: -webkit-gradient(radial, center center, 0, center center, 350, from(rgba(255, 255, 255, 0)), to(rgba(0, 0, 0, 255)));
    width: 5000px;
    height: 5000px;
    position: absolute;
   }
  </style>
  <script type="text/javascript"
 src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script type='text/javascript'>
$(document).ready(function(){
    $(document).mousemove(function(e){
        $("#fl").css({
            top: (e.pageY - 2500) + "px",
            left: (e.pageX - 2500) + "px"
        });
    });
});
</script>

 </head>
 <body style="
  height: 100%;
  margin: 0;
  padding: 0;
  background: url(http://www.onestopwebmasters.com/wp-content/uploads/2009/12/new-vista-wallpaper.jpg);
  overflow-x: hidden;
  ">
  <div class="fl" id="fl">
  </div>
 </body>
</html>

Update: the challenge was though to make links and buttons clickable still even though they are covered with a full-screen div. Luckily Firefox supports https://developer.mozilla.org/en/CSS/pointer-events which is just one line in CSS that makes the div transparent for the clicks. Naturally IE doesn't support this feature so a small jQuery code is looking for any links at the current mouse pointer coordinates and propagates a click on a covering DIV to the underlying links.

1 comment:

moooo said...

Thanks for sharing, do you have any tips for getting this to work on my canvas ipad animation? The height/width of 5000px seems to cause problems, I'm not sure how to fix it.