This module introduces cascading variables as a new primitive value type that is accepted by all CSS properties, and custom properties for defining them.
.boo {
--color-red: #c00;
color: var(--color-red, #f00);
}
.heart {
--color-red: #f00;
--color-blue: #00f;
--color-♥: var(--color-red, #900);
color: var(--color-♥);
}
:root {
--base-font-size: 14px;
}
.h2 {
font-size: var(--base-font-size);
}
@media (min-width: 600px) {
:root {
--base-font-size: 16px;
}
}
:root {
--base-font-size: 14px;
}
.h1 {
font-size: calc(var(--base-font-size) * 1.5);
}
.h2 {
font-size: var(--base-font-size);
}
@media (min-width: 600px) {
:root {
--base-font-size: 16px;
}
}
font-size
.ooo {
width: 5em;
height: 5em;
font-size: 20px;
&.big {
font-size: 50px;
}
.o {
width: 1em;
height: 1em;
transform: translateX(2.5em);
}
}
@supports ((--a: 1)) {
:root {
--color-red: #f00;
}
}
if (window.CSS &&
window.CSS.supports &&
window.CSS.supports('--a', 0)) {
_html.classList.remove('no-css-variables');
}
// inline-style
element.style.setProperty('--a', 2);
// inline-style
element.style.getPropertyValue('--a');
// any case
window.getComputedStyle(element).getPropertyValue('--a');
.l {
transform:
translate(-50%, -50%)
translate(
calc((var(--dx) - .5) * var(--df) * 200%),
calc((var(--dy) - .5) * var(--df) * 200%)
);
}
.box { --dx: 1; --dy: .7; --df: 0; } /* default */
const setDxDy = (evt) => {
let dx = evt.layerX / boxWidth;
let dy = evt.layerY / boxHeight;
box.style.setProperty('--dx', dx);
box.style.setProperty('--dy', dy);
};
// [--x, --y] = [0, 0]
// [--x, --y] = [1, 1]
transform:
translate(
calc(0% - 100% * var(--x)),
calc(0% - 100% * var(--y))
);
變形:// JS
i = i + 1;
/* expected syntax for real CSS variables */
.l + .l {
--df: calc(.2 + var(--df));
}
order
solution
// init
tiles.forEach((tile, index) => {
well.style.setProperty(`--order-${index}`, index * BASE_NUMBER);
tile.style.order = `var(--order-${index})`;
});
// in key events
targetIndex = (targetIndex + operator + 9) % 9;
let dir = originIndex > targetIndex ? -1 : 1;
well.style.setProperty(
`--order-${originIndex}`,
(targetIndex + .5 * dir) * BASE_NUMBER
);
感謝 ♥