是否可以在通用过渡 Stylus 混合中创建滚动?

Is it possible to create scroll in generic transition Stylus mixin?

JavaScript:

const show = entries => entries[0].isIntersecting ? entries[0].classList.remove('is-hidden') : null;
const observer = new IntersectionObserver(show, {threshold:0});
Array.from(document.querySelectorAll('.js-hidden')).forEach(element => observer.observe(element));

我想在“.js-hidden”class 被移除时播放淡入淡出的动画...但是这段代码不起作用:

HTML:

<div class="my-component js-hidden is-hidden">
    <p class="text text-1">Hello</p>
    <p class="text text-2">World</p>
</div>

手写笔:

fadeIn(duration=1s, delay=0s) {
    opacity: 1;
    transition: opacity duration ease delay;
    &.is-hidden { // <- yeah, this is wrong... but, any ideas? I want to apply transition both element and pseudo element.
        opacity: 0;
    }
}

.my-component {
    .text,
    &::before,
    &::after {
        fadeIn();
    }
    .text-2 {
        transition-delay: .3s;
    }
    &::before {
        content: 'foo';
        transition-delay: .5s;
    }
    &::after {
        content: 'bar';
        transition-delay: .7s;
    }
}

而且,如果淡入淡出的元素嵌套更多?

<div class="my-component js-hidden is-hidden">
    <div class="wrapper-1">
        <div class="wrapper-2">
            <p class="text text-1">Hello</p>
            <p class="text text-2">World</p>
        </div>
    </div>
</div>

我想对元素和伪元素都应用过渡。但是我不知道该怎么办...

谢谢。


终于...

谢谢安迪。

终于到了下面的代码。 XD

JavaScript:

const show = entries => entries[0].isIntersecting ? entries[0].target.classList.remove('is-hidden') : null;
const observer = new IntersectionObserver(show, {threshold:0});
Array.from(document.querySelectorAll('.js-hidden')).forEach(element => observer.observe(element));

HTML:

<div class="my-component js-hidden is-hidden">
  <p class="text text-1">Hello</p>
  <p class="text text-2">World</p>
  <p class="text text-3">Hello</p>
  <p class="text text-4">World</p>
  <p class="text text-5">Hello</p>
  <p class="text text-6">World</p>
  <p class="text text-7">Hello</p>
  <p class="text text-8">World</p>
  <p class="text text-9">Hello</p>
</div>

手写笔:

fadeIn(target, duration=1s, delay=0s, property=all, easing=ease) {
    {target} {
        opacity: 1;
        transition: duration property easing delay;
    }
    &.is-hidden {
        {target} {
            opacity: 0;
        }
    }
}

.my-component {
    duration = .3s;
    delay = 1s;
    fadeIn('.text', duration:duration, delay:delay);
    fadeIn('&::before', duration:duration, delay:delay);
    fadeIn('&::after', duration:duration, delay:delay);

    interval = duration;
    amount = 9;
    for i in 2..amount {
        .text-{i} {
            transition-delay: (interval * (i - 2) + duration + delay)s;
        }
    }

    &::before {
        content: 'FOOOOOOOOOOOOOOO';
        transition-delay: (interval * ((amount + 1) - 2) + duration + delay)s;
    }
    &::after {
        content: 'BARRRRRRRRRRRRRR';
        transition-delay: (interval * ((amount + 2) - 2) + duration + delay)s;
    }
}

首先,您似乎在寻找 transition,而不是 animation。我不是 Stylus 专家,但非常了解 IntersectionObserverCSS。我现在可以使用基本演示了。

关于调整后的 fadeIn 函数的一些注意事项。

  • is-hidden 是一个 class 从一开始就存在于 DOM 中,所以提示 transition 当它 不是 那里
  • 使用转换中的委托模式——也就是说,让父项中的更改影响子项(不要为每个 child/pseudo 元素监听 class)
fadeIn(duration=1s, delay=0s) {
  .text,
  &::before,
  &::after {
    opacity: 0;
    transition: 0.5s opacity ease;
  }

  &:not(.is-hidden) {
    .text,
    &::before,
    &::after {
      opacity: 1;
    }    
  }  
}

此外,由于一些错误,我无法让您的 JavaScript 工作,因此我重写了它以适应演示。这是重写的 JavaScript:

const components = document.querySelectorAll(".my-component");

const observer = new IntersectionObserver(components => {
  components.forEach(component => {
    if (component.intersectionRatio > 0) {
      component.target.classList.remove("is-hidden")
    } else {
      component.target.classList.add("is-hidden")
    }
  })
});

Array.from(document.querySelectorAll(".js-hidden")).forEach(element =>
  observer.observe(element)
);

CodePen Demo