亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Chrome 中滾動到視圖時,CSS 動畫沒有運行。類未添加到元素中

在 Chrome 中滾動到視圖時,CSS 動畫沒有運行。類未添加到元素中

慕工程0101907 2022-07-15 10:24:56
我有一個 CSS 關鍵幀動畫,當元素滾動到視圖中時,通過將類添加到我只想在它們滾動到視圖中時設置動畫的元素。它在 Firefox 中按預期工作,但由于某種原因它在 Chrome 中不起作用。開發人員工具表明,即使元素在視口中,也沒有將start類添加到其中。任何想法為什么這在 chrome 中不起作用?查詢:function isElementInViewport(elem) {    var $elem = $(elem);    // Get the scroll position of the page.    var scrollElem = ((navigator.userAgent.toLowerCase().indexOf('webkit') != -1) ? 'body' : 'html');    var viewportTop = $(scrollElem).scrollTop();    var viewportBottom = viewportTop + $(window).height();    // Get the position of the element on the page.    var elemTop = Math.round($elem.offset().top);    var elemBottom = elemTop + $elem.height();    return ((elemTop < viewportBottom) && (elemBottom > viewportTop));}// Check if it's time to start the animation.function checkAnimation() {    var $elem = $('.parent-content-block .slide-in');    // If the animation has already been started    if ($elem.hasClass('start')) return;    if (isElementInViewport($elem)) {        // Start the animation        $elem.addClass('start');    }}// Capture scroll events$(window).scroll(function () {    checkAnimation();});document.addEventListener("DOMContentLoaded", function() {  var elements = document.querySelectorAll(".slide-in");  // Intersection observer  var observer = new IntersectionObserver((entries) => {    entries.forEach((entry) => {      if (entry.intersectionRatio > 0) {        entry.target.classList.add("start");      }    });  });  elements.forEach((el) => {    observer.observe(el);  });});<div>  <img class="slide-in slide1" src="img.png">  <div style="height: 200vh">    scroll down  </diV>  <img class="slide-in slide1" src="img.png">  <img class="slide-in slide2" src="img.png">  <!-- other html... --></div>
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

我已經實現了一個基本的 Intersection Observer 供您查看。


document.addEventListener("DOMContentLoaded", function () {

    var elements = document.querySelectorAll(".slide-in");


    // Intersection observer

    var observer = new IntersectionObserver((entries) => {

        entries.forEach((entry) => {

            if (entry.intersectionRatio > 0) {

                entry.target.classList.add("animate");

            } else {

                entry.target.classList.remove("animate");

            }

        });

    });


    elements.forEach((el) => {

        observer.observe(el);

    });

});

.pre-scroll {

  min-height: 100vh;

}


.container {

    min-height: 110vh;

}


.slide-in {

    position: relative;

    background-color: #333333;

    height: 300px;

    width: 100%;

    margin-bottom: 50px;

    animation: animateInit 0.7s ease-in-out;

}


.slide-in.animate {

    animation: animate 0.7s ease-in-out;

}


@keyframes animate {

    0% {

        opacity: 0;

        transform: translateX(-20rem);

    }

    100% {

        opacity: 1;

        transform: translateX(0rem);

    }

}


@keyframes animateInit {

  0% {

        opacity: 0;

        transform: translateX(-20rem);

    }

    100% {

        opacity: 1;

        transform: translateX(0rem);

    }

}

<div class="container">

  <div class="slide-in"></div>

  <div class="slide-in"></div>

  <div class="pre-scroll"></div>

  <div class="slide-in"></div>

</div>


查看完整回答
反對 回復 2022-07-15
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號