通过页面滚动显示动画

时间:2013-09-11 03:42:57

标签: javascript css animation scroll delay

所以我一直在尝试按照本教程http://www.netmagazine.com/tutorials/getting-animations-trigger-right-time进行操作,并认识到这些效果仅适用于包含图像的元素。我想对不包含图像的元素(即:一块文本)产生这种影响,但无法使其工作。有没有人知道如何实现这一目标?谢谢你们!

这是脚本:

<script>
 if (Modernizr.csstransitions) {
    function preloadImages(imgs, callback) {
     var cache = [],
       imgsTotal = imgs.length,
       imgsLoaded = 0;

       $(imgs).each(function (i, img) {
         var cacheImage = document.createElement('img');
              cacheImage.onload = function () {
              if (++imgsLoaded == imgsTotal) callback();
                                                };
               cacheImage.src = $(img).attr('src');
               cache.push(cacheImage);
                                        });
                                };
                                $.fn.trans = function () {
                                        var t = arguments[0],
                                                d = arguments[1] || '';
                                        if (t) {
                                                $.each(this, function (i, e) {
                                                        $(['-webkit-', '-moz-', '-o-', '-ms-', '']).each(function (i, p) {
                                                                $(e).css(p + 'transition' + d, t);
                                                        });
                                                });
                                        }
                                };

                                document.write('<link rel="stylesheet" href="animations.css" />');

                                $(function(){
                                        //preload images contained within elements that need to animate
                                        preloadImages($('.services img, .featured img'), function () {
                                                $('.services, .featured').appear({
                                                        once: true,
                                                        forEachVisible: function (i, e) {
                                                                $(e).data('delay', i);
                                                        },
                                                        appear: function () {
                                                                var delay = 150,
                                                                        stagger = 800,
                                                                        sequential_delay = stagger * parseInt($(this).data('delay')) || 0;

                                                                $(this).children().each(function (i, e) {
                                                                        $(e).trans(i * delay + sequential_delay + 'ms', '-delay');
                                                                });
                                                                $(this).removeClass('animationBegin');
                                                        }
                                                });
                                        });
                                });
                        }
                </script>  

1 个答案:

答案 0 :(得分:1)

你可以找到处理视口css3动画的轻量级jQuery滚动脚本 SEE HERE

我创建了一个演示如何工作的演示,滚动小提琴,当元素在视口中时你会看到动画。

<强> SEE THE DEMO HERE