未选取CSS转换内联变量

yuvru6vn  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(114)

内联变量未被CSS转换拾取,或者它们被拾取,但它们没有接收到正确的信息。
下面是如何在HTML中设置内联变量的示例
<p class="hidden" style="--order: 1;" >ABC</p>
如何在CSS中呼叫变数。

section p {
    transition-delay: calc(200ms * var(--order));
}

下面是一个使用JS的完整HTML代码的例子。目标是向下滚动,章节将过渡到,段落将在200ms后过渡到。

<html>
<head>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;700&display=swap');
        body {
            background-color: #131316;
            color: #ffffff;
            padding: 0;
            margin: 0;
            font-family: 'Poppins', sans-serif;
        }
        section {
            display: grid;
            place-items: center;
            align-content: center;
            min-height: 100vh;
            padding: 0px 20%;
        }
        .hidden {
            opacity: 0;
            filter: blur(5px);
            transform: translateX(-100%);
            transition: all 1s;
        }
        .show {
            opacity: 1;
            filter: blur(0);
            transform: translateX(0);
        }
        section p {
            transition-delay: calc(200ms * var(--order));
        }
    </style>
</head>
<body>
    <section class="hidden">
        <p class="hidden" style="--order: 1;" >Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>

        <p class="hidden" style="--order: 2;" >Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>

        <p class="hidden" style="--order: 3;" >It is a paradisematic country, in which roasted parts of sentences fly into your mouth.</p>

        <p class="hidden" style="--order: 4;" >Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.</p>

        <p class="hidden" style="--order: 5;" >The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.</p>
    </section>
    <section class="hidden">
        
        <p class="hidden" style="--order: 1;" >Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
        <p class="hidden" style="--order: 2;" >The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.</p>
    </section>

    <script>
        const observer = new IntersectionObserver((entries, observer) => {
            entries.forEach(entry => {
                entry.target.classList.toggle('show', entry.isIntersecting);
            });
        });

        const hiddenElements = document.querySelectorAll('.hidden');
        hiddenElements.forEach((el) => observer.observe(el));
    </script>
</body>
</html>
watbbzwu

watbbzwu1#

它实际上是阅读和获取它,但是.hidden类是重写的,当show类被切换时,你可以做什么来切换hidden类,并确保transition-delay使用transition向元素添加过渡:

<html>
<head>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400;700&display=swap');
        body {
            background-color: #131316;
            color: #ffffff;
            padding: 0;
            margin: 0;
            font-family: 'Poppins', sans-serif;
        }
        section {
            display: grid;
            place-items: center;
            align-content: center;
            min-height: 100vh;
            padding: 0px 20%;
        }
        .hidden {
            opacity: 0;
            filter: blur(5px);
            transform: translateX(-100%);
            transition: all 1s;
        }
        .show {
            opacity: 1;
            filter: blur(0);
            transform: translateX(0);
        }
        section p {
            transition: all calc(200ms * var(--order)); /* instead transition-delay */
        }
    </style>
</head>
<body>
    <section class="hidden">
        <p class="hidden" style="--order: 1;" >Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>

        <p class="hidden" style="--order: 2;" >Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>

        <p class="hidden" style="--order: 3;" >It is a paradisematic country, in which roasted parts of sentences fly into your mouth.</p>

        <p class="hidden" style="--order: 4;" >Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.</p>

        <p class="hidden" style="--order: 5;" >The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.</p>
    </section>
    <section class="hidden">
        
        <p class="hidden" style="--order: 1;" >Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
        <p class="hidden" style="--order: 2;" >The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way.</p>
    </section>

    <script>
        const observer = new IntersectionObserver((entries, observer) => {
            entries.forEach(entry => {
                entry.target.classList.toggle('show', entry.isIntersecting);
              entry.target.classList.toggle('hidden', entry.isIntersecting);
               entry.target.classList.toggle('hidden', !entry.isIntersecting);
            });
        });

        const hiddenElements = document.querySelectorAll('.hidden');
        hiddenElements.forEach((el) => observer.observe(el));
    </script>
</body>
</html>

相关问题