gpt4 book ai didi

javascript - 如何使 IntersectionObserver 与变换平移动画一起使用?

转载 作者:行者123 更新时间:2023-12-05 00:39:27 24 4
gpt4 key购买 nike

我想为具有以下属性的元素创建动画:

  • 它在 时为元素设置动画它进入视口(viewport)
  • 如果元素离开视口(viewport)然后再次进入它应该是再次动画
  • 根据滚动方向/相交边(从顶部或底部),动画应该不同

  • 为此,我使用 IntersectionObserver 我接近了预期的结果。
    我面临的唯一问题是, 当我在动画 期间沿滚动方向(在本例中为 transform: translateY )平移元素时.这将导致 IntersectionObserver触发多次甚至无限次。

    enter image description here

    function isIntersectingFromTop(entry){
    return entry.boundingClientRect.bottom != entry.intersectionRect.bottom;
    }

    function isIntersectingFromBottom(entry){
    return entry.boundingClientRect.top != entry.intersectionRect.top;
    }

    var count = 0;

    function incrementCounter(entry){
    document.querySelector(".counter").textContent += "intersectionRation (" + count + "): " + entry.intersectionRatio + "\n";
    count++;
    }

    let observer = new IntersectionObserver(
    function (entries, observer) {
    entries.forEach(function(entry){
    incrementCounter(entry)
    if (entry.isIntersecting) {
    if(isIntersectingFromTop(entry)){
    entry.target.classList.add("animated--up-in");
    } else if(isIntersectingFromBottom(entry)) {
    entry.target.classList.add("animated--down-in")
    }
    } else {
    /** element is not in viewport anymore
    * this will be triggered right after the animation starts
    * since the translate is moving the elment out of the view
    * which is causing a new intersection (isIntersecting = false)
    */
    entry.target.classList.remove("animated--up-in");
    entry.target.classList.remove("animated--down-in");
    }
    });
    });

    observer.observe(document.querySelector(".to-animate"));
    .container {
    height: 1000px;
    width: 100%;
    }

    .box{
    position: static;
    width: 100px;
    height: 100px;
    background: red;
    margin-top: 10px;
    }

    .to-animate{
    background: blue;
    opacity: 0;
    }

    .animated--up-in {
    animation: animateUpIn 1.5s forwards ease;
    }

    .animated--down-in {
    animation: animateDownIn 1.5s forwards ease;
    }

    @keyframes animateUpIn {
    from {
    transform: translateY(100px);
    opacity: 0;
    }
    to {
    transform: translateY(0);
    opacity: 1;
    }
    }

    @keyframes animateDownIn {
    from {
    transform: translateY(-100px);
    opacity: 0;
    }
    to {
    transform: translateY(0);
    opacity: 1;
    }
    }



    .counter {
    position: fixed;
    top: 10%;
    left: 30%;
    color: black;
    height: 80%;
    width: 50%;
    overflow-y: auto;
    padding: 10px;
    }
    <div class="container">
    <pre class="counter"></pre>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box to-animate"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    </div>


    问题

    我怎样才能“告诉” IntersectionObserver忽略翻译位置,只使用初始/原始位置来计算交点(没有额外的元素)?这甚至可能吗?

    最佳答案

    我认为在这种情况下,您需要跟踪将嵌套动画元素的固定容器在屏幕上的位置。

    关于javascript - 如何使 IntersectionObserver 与变换平移动画一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61699283/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com