如果我有一个带有以下设置的photoshop投影
混合模式 - rgb(0,0,0)/不透明度 - 25%/角度 - 135度/距离4px/Spread - 0%/尺寸 - 4px
如何设置我的CSS3盒子阴影,以便它代表我的photoshop设计?
小智 21
我写了一篇文章,介绍了如何将Photoshop Drop Shadow属性转换为CSS3 box-shadow.如果你使用的是Sass/Compass,你可以使用photoshop-drop-shadow指南针插件.如果你想自己做数学,这并不是非常困难,下面是一个用JavaScript编写的简单例子.两个棘手的部分是将角度转换为X和Y偏移,并将扩散百分比转换为扩展半径.
// Assume we have the following values in Photoshop
// Blend Mode: Normal (no other blend mode is supported in CSS)
// Color: 0,0,0
// Opacity: 25%
// Angle: 135deg
// Distance: 4px
// Spread: 0%
// Size: 4px
// Here's some JavaScript that would do the math
function photoshopDropShadow2CSSBoxShadow(color, opacity, angle, distance, spread, size) {
// convert the angle to radians
angle = (180 - angle) * Math.PI / 180;
// the color is just an rgba() color with the opacity.
// for simplicity this function expects color to be an rgb string
// in CSS, opacity is a decimal between 0 and 1 instead of a percentage
color = "rgba(" + color + "," + opacity/100 + ")";
// other calculations
var offsetX = Math.round(Math.cos(angle) * distance) + "px",
offsetY = Math.round(Math.sin(angle) * distance) + "px",
spreadRadius = (size * spread / 100) + "px",
blurRadius = (size - parseInt(spreadRadius, 10)) + "px";
return offsetX + " " + offsetY + " " + blurRadius + " " + spreadRadius + " " + color;
}
// let's try it
// for simplicity drop all of the units
photoshopDropShadow2CSSBoxShadow("0,0,0", 25, 135, 4, 0, 4);
// -> 3px 3px 4px 0px rgba(0,0,0,0.25)
Run Code Online (Sandbox Code Playgroud)
此 CSS 类适用于在一个规则中收集的各种不透明的 Web 浏览器(已知支持:Firefox 3.5+、Chrome 5+、Safari 5+、Opera 10.6+、IE 9+):
.shadow {
-moz-box-shadow: 4px 4px 4px #000;
-webkit-box-shadow: 4px 4px 4px #000;
box-shadow: 4px 4px 4px #000;
/* For IE 8 */
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000')";
/* For IE 5.5 - 7 */
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#000000');
}
Run Code Online (Sandbox Code Playgroud)
...这个 CSS 类具有透明度支持:
.shadow {
-webkit-box-shadow:4px 4px 0px rgba(0, 0, 0, 0.25);
-moz-box-shadow:4px 4px 0px rgba(0, 0, 0, 0.25);
box-shadow:4px 4px 0px rgba(0, 0, 0, 0.25);
-webkit-transform:rotate(135deg);
-moz-transform:rotate(135deg);
-o-transform:rotate(135deg);
transform:rotate(135deg);
}
Run Code Online (Sandbox Code Playgroud)