简体   繁体   English

CSS animation 在 Chrome 中滚动到视图时未运行。 Class 未添加到元素中

[英]CSS animation isnt running when scrolled into view in Chrome. Class isn't being added to the element

I have a CSS keyframe animation activate when the element is scrolled into view by adding the class to the elements i want to animate only when they are scrolled into view.我有一个 CSS 关键帧 animation 在元素滚动到视图时激活,方法是将 class 添加到我只想在滚动到视图时动画的元素中。 It works as expected in Firefox, but for some reason it's not working in Chrome.它在 Firefox 中按预期工作,但由于某种原因它在 Chrome 中不起作用。 Developer tools has shown that the element isn't getting the start class added to it even when it's in the view port.开发人员工具显示,即使在视口中,元素也没有start添加 class。 Any ideas why this won't work in chrome?任何想法为什么这在 chrome 中不起作用?

JQuery: JQuery:

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>

I've implemented a basic Intersection Observer for you to look over.我已经实现了一个基本的 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM