試したことをコメントに書きましたが、賛成票が集まるかもしれないので、回答にも書きます。:D。
var source = [0, 0], faktor = [10, 20];
ソースは光があるべき場所であり、因子は影の因子です ([0] は影の位置、[1] はぼかし)。
function addShadows() {
var i, j, position, sizeE, distance; for(i=0,j=arguments.length;i<j;++i) {
position = offset(arguments[i]); // Get position from element
sizeE = size(arguments[i]); // Get width and height from element
distance = parseInt(Math.sqrt(Math.pow(position[0] + sizeE[0] / 2 - source[0], 2) + Math.pow(position[1] + sizeE[1] / 2 - source[1], 2))) / faktor[1]; // calculate a distance for bluring (middle of element to source)
arguments[i].style.cssText += 'box-shadow: ' + ((position[0] + sizeE[0] - source[0]) / faktor[0]) + 'px ' + ((position[1] + sizeE[1] - source[1]) / faktor[0]) + 'px ' + distance + 'px #555555'; // add the shadow
}
}
この関数addShadows
は、すべてのパラメーターに影を追加します。
function getStyle(element, attribut) {
var style;
if(window.getComputedStyle) {
style = window.getComputedStyle(element, null);
} else {
style = element.currentStyle;
}
return style[attribut];
}
function offset(element) {
var pos = [parseInt(getStyle(element, 'border-top')), parseInt(getStyle(element, 'border-top'))];
while(element !== null) {
pos[0] += element.offsetLeft;
pos[1] += element.offsetTop;
element = element.offsetParent;
}
return pos;
}
function size(element) {
return [element.offsetWidth, element.offsetHeight];
}
function id(idE) {
return document.getElementById(idE);
}
上記の 4 つの関数は単なるヘルパー関数です。
JSfiddle: http://jsfiddle.net/R5UbL/1/