简体   繁体   English

CSS3和jQuery在滚动位置上设置动画

[英]CSS3 & jQuery animate on scroll position

having a few issues with my code here. 我的代码在这里有一些问题。 I want brackets to animate whenever they are visible on the screen on the users scroll. 我希望方括号能够在用户滚动屏幕上可见时进行动画处理。 My issue is that all of the h2 elements on the page animate at the same time instead when they show in screen. 我的问题是页面上的所有h2元素都同时制作动画,而不是在屏幕上显示时。 Below is the jQuery used, and here is a link to a jsfiddle: 以下是使用的jQuery,这是jsfiddle的链接:

http://jsfiddle.net/CbxvH/18/ http://jsfiddle.net/CbxvH/18/

Thanks in advance, Mike. 预先感谢,迈克。

    $(window).scroll( function(){

        var test1 = $('article h2 span.bracket1');
        var bottom_of_object1 = $(test1).position().top + $(test1).outerHeight();
        var bottom_of_window = $(window).scrollTop() + $(window).height();

        if( bottom_of_window > bottom_of_object1 ){

            $(test1).addClass( "slideLeft" );
            setTimeout(function() {
              $("article h2 .spanContent").animate({'opacity':'1'},1000);
            }, 1000);


    };

        var test2 = $('article h2 span.bracket2');

        var bottom_of_object2 = $(test2).position().top + $(test2).outerHeight();

        if( bottom_of_window > bottom_of_object2 ){

            $(test2).addClass( "slideRight" );

        };

        var test3 = $('article h2 span.bracket3');

        var bottom_of_object3 = $(test3).position().top + $(test3).outerHeight();

        if( bottom_of_window > bottom_of_object3 ){

            $(test3).addClass( "slideLeft" );
            setTimeout(function() {
              $("article h2 .spanContent2").animate({'opacity':'1'},1000);
            }, 1000);

        }

        var test4 = $('article h2 span.bracket4');

        var bottom_of_object4 = $(test4).position().top + $(test4).outerHeight();

        if( bottom_of_window > bottom_of_object4 ){

            $(test4).addClass( "slideRight" );

        }


});

each statement: 每个语句:

$('.featureImages img, article p, article ul li').each( function(i){

        var bottom_of_object = $(this).position().top + $(this).outerHeight();
        var bottom_of_window = $(window).scrollTop() + $(window).height();

        if( bottom_of_window > bottom_of_object ){

            $(this).animate({'opacity':'1'},1000);

        }

    }); 

Have you tried using offset() instead of position() ? 您是否尝试过使用offset()而不是position()

var offset =  $(test1).offset();  
var bottom_of_object1 = offset.top + $(test1).outerHeight();

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

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