当内容滚动到视图中时激活CSS3关键帧动画。

5

我一直在尝试弄清楚如何在滚动到可见时发生动画,但是运气不太好。我找到了一个名为“Waypoints”的JQuery插件,但我缺乏利用它的技能。我正在尝试使用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代码,当它被滚动到时,将触发类名为“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”类的项目被滚动到时,它会触发所有具有相同类的项目。我想知道是否有一种方法可以使它们以相同的类别单独触发。任何帮助都将不胜感激。
谢谢!
1个回答

2

将您的 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');
        }
    });
}

没有那么幸运。当第一个项目滚动到视图中时,每个项目同时触发。这是我的正在进行的工作: http://cbitcomputer.com/about/computer-repair/ - simpleminded
这是因为您的元素具有“display:none”,因此它们没有大小,并被认为都在同一位置。因此,您的“isElementInViewport”函数会在页面顶部发现它们全部出现。如果您从“.bounceinright,.bounceinleft”规则中删除“display:none”,它将可以与我的代码一起使用。 - Gabriele Petrioli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接