这不是一般情况的真正解决方案,而是解决小提琴特定情况的技巧:
div{
box-shadow: 0 0 0 5px green, 0 0 0 10px #ff0000 , 0 0 0 15px blue;
position: relative;
}
div:nth-of-type(2):after {
content: '';
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
margin: -5px;
box-shadow: 0 0 0 5px yellow;
}
我正在创建一个伪元素来保存第二个阴影,并设置一个边距(负数)以使其超过第一个阴影,这样我就不会在它上面画了。
这是另一个演示,通过 JavaScript 更改值
演示2
脚本如下
function change () {
var elem = document.getElementById("test");
var style = window.getComputedStyle(elem);
var boxShadow = style.boxShadow;
var arrayBoxShadows = parseFirstComma (boxShadow);
var newData = elem.dataset.boxshadow;
var arrayNewData = parseFirstComma (newData);
boxShadow = "";
var nmax = Math.min (arrayBoxShadows.length, arrayNewData.length)
for (var n = 0, lenR = nmax; n < lenR; n++) {
if (n > 0) {
boxShadow = boxShadow + ", ";
}
if (arrayNewData[n] == "inherit") {
boxShadow = boxShadow + arrayBoxShadows[n];
} else {
boxShadow = boxShadow + arrayNewData[n];
}
}
if (arrayNewData.length > nmax) {
for (var n = nmax, lenR = arrayNewData.length; n < lenR; n++) {
if (n > 0) {
boxShadow = boxShadow + ", ";
}
boxShadow = boxShadow + arrayNewData[n];
}
}
if (arrayBoxShadows.length > nmax) {
for (var n = nmax, lenR = arrayBoxShadows.length; n < lenR; n++) {
if (n > 0) {
boxShadow = boxShadow + ", ";
}
boxShadow = boxShadow + arrayBoxShadows[n];
}
}
elem.style.boxShadow = boxShadow;
}
function parseFirstComma (property) {
var properties = new Array();
var curr = "";
var chr;
var nested = "";
for (inx = 0, len = property.length; inx < len; inx++) {
chr = property[inx];
if (chr == "(") nested += 1;
if (chr == ")") nested -= 1;
if (nested == 0 && chr == ",") {
properties.push (curr);
curr = "";
} else {
curr = curr + chr;
}
}
if (curr.length > 0) {
properties.push (curr);
}
return properties;
};
document.onkeydown = function (e) {
e = e || window.event;
switch(e.which || e.keyCode) {
case 37:
change ();
break;
}
}
我正在检索应用于元素的框阴影,并从数据值中检索所需的覆盖。然后,重新计算结果(使用继承作为可能的值)。按左箭头激活演示。