javascript - 向下滚动触发动画

标签 javascript jquery css events animation

我在这里创建了从彩色变为灰度的动画,但我希望它仅在用户向下滚动时启动(因为它在我的站点中有很多图像,需要向下滚动才能到达那里)

这是 fiddle 的例子: http://jsfiddle.net/4tHWg/7/

 .box {
       float: left;
       position: relative;
       width: 14.285714286%;



    }

    .boxInner img {
       width: 100%;
       display: block;

    }

    .boxInner img:hover {
      -webkit-filter: grayscale(0%);
    }

@-webkit-keyframes toGrayScale {
    to {
        -webkit-filter: grayscale(100%);
    }
}

.box:nth-child(1) img {
    -webkit-animation: toGrayScale 1s 0.5s forwards;
}

.box:nth-child(2) img {
    -webkit-animation: toGrayScale 2s 1s forwards;
}

.box:nth-child(3) img {
    -webkit-animation: toGrayScale 3s 1.5s forwards;
}

最佳答案

这应该可以解决问题。

$( window ).scroll(function() {
    $(".box").each(function (index){
        if (index == 1)
        {
            $(":nth-child("+index+")", $(this)).css('-webkit-animation','toGrayScale 1s 0.5s forwards');
        }
        if (index == 2)
        {
            $(":nth-child("+index+")", $(this)).css('-webkit-animation','toGrayScale 2s 1s forwards');
        }
        if (index == 2)
        {
            $(":nth-child("+index+")", $(this)).css('-webkit-animation','toGrayScale 3s 1.5s forwards');
        }
    });

关于javascript - 向下滚动触发动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894968/

相关文章:

javascript - watch 是异步的吗?

javascript - React Hook 不能在函数内调用

javascript - 我怎样才能清除 Canvas ?

jquery - 在移动 View 中使用 jquery 修改 css (>720px)

javascript - Angularjs 双向数据绑定(bind)不起作用?

javascript - 我该如何优化这段代码?

css - Twitter Bootstrap - 使用 media-grip 的标题

javascript - IOS Safari,在javascript中从鼠标点击的屏幕获取绝对位置,不包括滚动区域

javascript - Uncaught ReferenceError : $ is not defined Error

javascript - Bootstrap "page-scroll"类在模式中不起作用