如何调试 CSS calc() 值?
问题
我有相对复杂的公式,例如:transform: scale(var(--image-scale)) translateY(calc((1px var(--element-height) (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y)))
如何调试计算出的值?
此外,是否有办法验证/突出显示公式错误?
我尝试这样做
position: fixed;</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">display: block; left:0; right: 0; background: yellow; padding: 5px; z-index: 100; content: calc((1px * var(--element-height) * (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y));
没有用
我发现的唯一方法是将计算的一部分放在未使用的数字属性中,例如,下面的 gif 中的 background-position-x,这样它将在计算选项卡上显示计算的值——有用但不太方便(notice background-position-x 在页面滚动时发生变化):
<div>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">cssProps: true
})
const results = Splitting();
var parallaxedElements = document.querySelectorAll('.section');
document.addEventListener('scroll', function(e) {
parallaxedElements Array.from(parallaxedElements).forEach((el) => { var bcr = el.getBoundingClientRect(); if (bcr.y < 0 && Math.abs(bcr.y) <= bcr.height) { el.style.setProperty("--scrolled-out-y", Math.round(Math.abs(bcr.y) * 10000 / bcr.height) / 10000); } }); </p> <p>})
html {</p><pre class="brush:php;toolbar:false">scroll-behavior: smooth;
}
body {
font-family: "Roboto"; font-size: 14px; line-height: 1.4; scroll-behavior: smooth;
}
.section {
position: relative; background-attachment: fixed; z-index: 1; --image-scale: 1.2; --scrolled-out-y: 0;
}
.section__background {
position: -webkit-sticky; position: sticky; top: 0; width: 100%; height: 100vh; overflow: hidden;
}
.section__background:after {
content: ""; position: absolute; bottom: 0; left: 0; right: 0; top: 0; z-index: 1; background: linear-gradient(to bottom, black, 100% white); background: rgba(0, 0, 0, 0.4); opacity: calc(1 + ((var(--viewport-y) * 1.5)));
}
.section__background>img {
height: 150vh; width: 100%; object-fit: cover; position: absolute; left: 0; top: 0px; user-select: none; transform: scale(var(--image-scale)) translateY(calc((-1px * var(--element-height) * (var(--image-scale) - 1)) * var(--scrolled-out-y)));
}
/* .indicator:after {
position: fixed; display: block; left: 0; right: 0; background: pink; padding: 5px; z-index: 100; content: calc((1px * var(--element-height) * (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y)); } */
.section__container {
padding-bottom: 50vh; overflow: hidden; align-items: flex-start; position: relative; z-index: 4;
}
.section__heading {
color: #fff; text-transform: uppercase; font-size: 45px; line-height: 1.2; font-weight: 800; letter-spacing: 8px; margin: 0; overflow: hidden; position: relative; padding-bottom: 50px; margin-bottom: 50px;
}
.section__heading:after {
content: ""; position: absolute; top: 200px; left: 0; right: 0; height: 2px; transform: translateX(calc(var(--scrolled-out-y) * 100% - 70%)); background: #b38c6b;
}
.section__content {
display: flex; color: white; flex-direction: column;
}
.section__content p p {
margin-top: 20px;
}
.splitting {
--char-percent: calc(var(--char-index) / var(--char-total));
}
.splitting .char {
display: inline-block; opacity: calc(1 + ((var(--viewport-y) * 1.5) - var(--char-percent)));
}
<link rel='stylesheet' href='https://unpkg.com/splitting/dist/splitting.css'></p> <p><section data-scroll> <div class="section__background"></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><div>
<div>
以上是如何调试 CSS `calc()` 值?的详细内容。更多信息请关注PHP中文网其他相关文章!