jQuery animate()页面单独上下滚动

时间:2015-06-24 12:34:32

标签: javascript jquery jquery-animate

嗨,我对jQuery很新。我试图让页面滚动窗口的高度,并使用jQuery顺利地这样做。当我使用下面的代码时,页面会不断地上下滚动。一个想法为什么?`

var body = jQuery('body, html');
var newDistance = 0;
var movement;
var newPosition = 0;

jQuery(window).on('scroll',function(){
    movement = window.pageYOffset;
    var winh = window.innerHeight;
    if(movement >= (newPosition + 100)){
            newDistance += winh;
            jQuery(body).animate({scrollTop:newDistance},{
            duration: 'slow'
            });

            console.log('newd', newDistance);
            newPosition = newDistance;
     }else if (movement <= (newPosition - 100)){
            newDistance -= winh;
            jQuery(body).animate({scrollTop:newDistance},{
            duration: 'slow'
            });
            newPosition = newDistance;
    }else{
            jQuery(body).animate({scrollTop:newPosition},{
            duration: 'slow'
            });
    };
 } );

0 个答案:

没有答案