【问题标题】:Activate CSS3 keyframe animation when the content scrolls into view当内容滚动到视图中时激活 CSS3 关键帧动画
【发布时间】:2013-05-26 10:24:41
【问题描述】:

我一直在试图弄清楚当它被滚动到视图中时如何产生动画,但几乎没有运气。我找到了一个 JQuery 插件“Waypoints”,但我缺乏使用它的技能。我正在尝试使用滚动到视图中时动画的 CSS3 关键帧。我取得了突破,并且接近我想要完成的目标,但它并不是我想要的。我有一个名为“bounceinright”的类,它使项目反弹到屏幕的右侧。这是用于此的 CSS:

@-webkit-keyframes bounceinright {
0% {
    opacity: 0;
    -webkit-transform: translateX(2000px);
}

60% {
    opacity: 1;
    -webkit-transform: translateX(-30px);
}

80% {
    -webkit-transform: translateX(10px);
}

100% {
    -webkit-transform: translateX(0);
}
}

@-moz-keyframes bounceinright {
0% {
    opacity: 0;
    -moz-transform: translateX(2000px);
}

60% {
    opacity: 1;
    -moz-transform: translateX(-30px);
}

80% {
    -moz-transform: translateX(10px);
}

100% {
    -moz-transform: translateX(0);
}
}

@-o-keyframes bounceinright {
-o-transform: translateX(2000px);
}

60% {
opacity: 1;
-o-transform: translateX(-30px);
}

80% {
-o-transform: translateX(10px);
}

100% {
-o-transform: translateX(0);
}   
}

@keyframes bounceinright {
0% {
    opacity: 0;
    transform: translateX(2000px);
}

60% {
    opacity: 1;
    transform: translateX(-30px);
}

80% {
    transform: translateX(10px);
}

100% {
    transform: translateX(0);
}
 }

.bounceinright {
display: none;
}

.bounceinright.start {
-webkit-animation: bounceinright 1s ease-out forwards;
-moz-animation: bounceinright 1s ease-out forwards;
-ms-animation: bounceinright 1s ease-out forwards;
-o-animation: bounceinright 1s ease-out forwards;
animation: eigbounceinrighthty 1s ease-out forwards;
display: block;
}

然后我有一个小 JQuery sn-p,它会在滚动到时触发带有“start”类的动画。

 ////////////////////////////////
 //scroll events
 ////////////////////////////////       
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 = $('.row .wpb_content_element');

    // 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();
});

我的问题是,当滚动到一个带有“bounceinright”的项目时,它会触发所有具有相同类的项目。我想知道是否有办法让它们单独触发但使用相同的类。任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: jquery html css scroll css-animations


    【解决方案1】:

    将您的 checkAnimation() 更改为

    // Check if it's time to start the animation.
    function checkAnimation() {
        var $elem = $('.row .wpb_content_element');
    
        $elem.each(function(){
            var $singleElement = $(this);
            // If the animation has already been started
            if ($singleElement.hasClass('start')) return;
    
            if (isElementInViewport($singleElement)) {
                // Start the animation
                $singleElement.addClass('start');
            }
        });
    }
    

    【讨论】:

    • 运气不好。当第一个项目滚动到视图中时,每个项目都会同时触发。这是我正在进行的工作:cbitcomputer.com/about/computer-repair
    • 发生这种情况是因为您的元素有 display:none,因此它们没有大小,并且被认为都在同一个位置。所以您的 isElementInViewport 会在页面顶部找到它们。如果您从.bounceinright, .bounceinleft 规则中删除display:none,它将适用于我的代码..
    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2023-03-18
    相关资源
    最近更新 更多