是否可以在通用转换Stylus mixin中创建滚动?

问题描述 投票:1回答:1

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'类被删除时,我想播放淡入淡出的动画......但是这段代码不起作用:

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;
    }
}
css transition stylus
1个回答
1
投票

首先,你似乎在寻找一个transition,而不是animation。我不是Stylus专家,但非常了解IntersectionObserverCSS。我现在有基本的演示工作。

关于调整后的fadeIn函数的一些注释。

  • is-hidden是一个从一开始就存在于DOM中的类,因此当transition不存在时提示它
  • 使用转换中的委托模式 - 也就是说,父级中的更改会影响子级(不要为每个子级/伪元素侦听一个类)
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)
);

enter image description here

CodePen Demo

© www.soinside.com 2019 - 2024. All rights reserved.