11

Have a look at the picture below:

skyscrapers

The blue boxes are divs. Now what I am trying to do is to implement a sort of 2.5D functionality:

I would like the grey shadows to be somewhat 3D-ish. At first I was thinking to assign to the box-shadow value the "Y" axis like this:

"box-shadow: -5px -5px 10px" + value.tallness +  "#888"

but the result is the above image.

Any idea on how to make the shadow on one side only, like there was a light source from somewhere?

EXTRA - what about a moving "light source"?

3 Answers3

22

There you go: http://jsfiddle.net/KaCDN/15/

Drag light source to affect shadows.

Taller blocks:

  • have larger top,left border
  • drop shadow further
  • they're shadow is blurier
XCS
  • 27,244
  • 26
  • 101
  • 151
  • Updated: taller blocks have a lighter color. – XCS Aug 19 '12 at 14:58
  • Update2: Taller blocks drop shadow further than shorter blocks. – XCS Aug 19 '12 at 15:03
  • Hmm, one problem is that the shadow from one div appears on top of another div. Don't know if it's possible to make the shadows be bottom-most, as long as they're created using box-shadow. – XCS Aug 19 '12 at 15:23
  • Update: Distance between light source and block affects shadow's size. – XCS Aug 19 '12 at 15:58
  • that code seems so complicated! a great example by the way! I really got no clue where to start modifying it, aha. where do i change shadow color (they seem a bit too white-ish) and "definition"? so that the shadow border is less blurry? –  Aug 19 '12 at 16:02
  • I'll add comments to the code to make it easier to understand. – XCS Aug 19 '12 at 16:03
  • There you go, added some comments. – XCS Aug 19 '12 at 16:10
4

How to move the shadow a little simpler:

$(document).on('mousemove', function(e) {
    var elm = $("#test"),
        x = ~Math.round((e.pageX - elm[0].offsetLeft - 150) / 30),
        y = ~Math.round((e.pageY - elm[0].offsetTop - 150) / 30),
        z = 10+Math.abs(x)+Math.abs(y),
        cssVal = x+'px '+y+'px '+z+'px 10px #525252';

    elm.css({'-webkit-box-shadow' : cssVal, 'box-shadow' : cssVal });
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

A more up-to-date answer would be to use a library like Shine.js (http://bigspaceship.github.io/shine.js/) which would handle this exact problem for you.

Wassim Gr
  • 568
  • 2
  • 8
  • 16