Javascript jQuery div在向下滚动时滑入,在向上滚动时滑出

Javascript jQuery div在向下滚动时滑入,在向上滚动时滑出,javascript,jquery,css,scroll,jquery-animate,Javascript,Jquery,Css,Scroll,Jquery Animate,这使我可以将#contactdiv滑入,但当我向上滚动时,它不会滑出 $(window).scroll(function(){ if ($(this).scrollTop() > 100) { $('#scrolltop').fadeIn(); $('#contact').animate({ right: "0px" }, 500 ); } else { $('#scrolltop').

这使我可以将
#contact
div滑入,但当我向上滚动时,它不会滑出

$(window).scroll(function(){
    if ($(this).scrollTop() > 100) {
        $('#scrolltop').fadeIn();
        $('#contact').animate({
            right: "0px"
        }, 500 );
    } else {
        $('#scrolltop').fadeOut();
        $('#contact').animate({
           right: "-115px"
        }, 500 );
    }
});

当用户滚动时,会多次触发scroll事件,并且使用您的代码,会快速连续多次调用animate函数,这似乎会导致问题。我建议添加一个标志来确定您是否已经调用了animate。这个代码对我有用:

var animated = false;
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
    if(!animated){
        $('#scrolltop').fadeIn();
        $('#contact').animate({
            left: 0
        }, 500 );
        animated = true;
    }
} else if(animated){
    $('#scrolltop').fadeOut();
    $('#contact').animate({
       left: -115
    }, 500 );
    animated = false;
}
编辑:

为了解决当用户快速上下滚动时重复进行多个动画调用的问题,我将另外跟踪元素当前是否正在动画,如下所示:

    var animated = false;
    var animating = false;
    $(window).scroll(scroll);

    function scroll(){
        if(!animating) {
            if ($(document).scrollTop() > 100) {
                    if(!animated){
                    animating = true;
                        $('#scrolltop').fadeIn();
                        $('#contact').animate({
                                left: 0
                        }, {"duration":500,"complete":complete});
                        animated = true;
                    }
            } else if(animated){
                animating = true;
                    $('#scrolltop').fadeOut();
                    $('#contact').animate({
                        left: -115
                    }, {"duration":500,"complete":complete} );
                    animated = false;
            }
        }
    }

    function complete(){
        animating = false;
        scroll();
    }

在此代码中,
animated
显示元素是否滑入或滑出屏幕,而
animating
显示当前是否正在使用动画(输入或输出)。我建议您这样做,而不是尝试使用超时。

当用户滚动时,滚动事件会多次触发,并且使用您的代码,动画功能会快速连续多次调用,这似乎会导致问题。我建议添加一个标志来确定您是否已经调用了animate。这个代码对我有用:

var animated = false;
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
    if(!animated){
        $('#scrolltop').fadeIn();
        $('#contact').animate({
            left: 0
        }, 500 );
        animated = true;
    }
} else if(animated){
    $('#scrolltop').fadeOut();
    $('#contact').animate({
       left: -115
    }, 500 );
    animated = false;
}
编辑:

为了解决当用户快速上下滚动时重复进行多个动画调用的问题,我将另外跟踪元素当前是否正在动画,如下所示:

    var animated = false;
    var animating = false;
    $(window).scroll(scroll);

    function scroll(){
        if(!animating) {
            if ($(document).scrollTop() > 100) {
                    if(!animated){
                    animating = true;
                        $('#scrolltop').fadeIn();
                        $('#contact').animate({
                                left: 0
                        }, {"duration":500,"complete":complete});
                        animated = true;
                    }
            } else if(animated){
                animating = true;
                    $('#scrolltop').fadeOut();
                    $('#contact').animate({
                        left: -115
                    }, {"duration":500,"complete":complete} );
                    animated = false;
            }
        }
    }

    function complete(){
        animating = false;
        scroll();
    }

在此代码中,
animated
显示元素是否滑入或滑出屏幕,而
animating
显示当前是否正在使用动画(输入或输出)。我建议您这样做,而不是尝试使用超时。

当用户滚动时,滚动事件会多次触发,并且使用您的代码,动画功能会快速连续多次调用,这似乎会导致问题。我建议添加一个标志来确定您是否已经调用了animate。这个代码对我有用:

var animated = false;
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
    if(!animated){
        $('#scrolltop').fadeIn();
        $('#contact').animate({
            left: 0
        }, 500 );
        animated = true;
    }
} else if(animated){
    $('#scrolltop').fadeOut();
    $('#contact').animate({
       left: -115
    }, 500 );
    animated = false;
}
编辑:

为了解决当用户快速上下滚动时重复进行多个动画调用的问题,我将另外跟踪元素当前是否正在动画,如下所示:

    var animated = false;
    var animating = false;
    $(window).scroll(scroll);

    function scroll(){
        if(!animating) {
            if ($(document).scrollTop() > 100) {
                    if(!animated){
                    animating = true;
                        $('#scrolltop').fadeIn();
                        $('#contact').animate({
                                left: 0
                        }, {"duration":500,"complete":complete});
                        animated = true;
                    }
            } else if(animated){
                animating = true;
                    $('#scrolltop').fadeOut();
                    $('#contact').animate({
                        left: -115
                    }, {"duration":500,"complete":complete} );
                    animated = false;
            }
        }
    }

    function complete(){
        animating = false;
        scroll();
    }

在此代码中,
animated
显示元素是否滑入或滑出屏幕,而
animating
显示当前是否正在使用动画(输入或输出)。我建议您这样做,而不是尝试使用超时。

当用户滚动时,滚动事件会多次触发,并且使用您的代码,动画功能会快速连续多次调用,这似乎会导致问题。我建议添加一个标志来确定您是否已经调用了animate。这个代码对我有用:

var animated = false;
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
    if(!animated){
        $('#scrolltop').fadeIn();
        $('#contact').animate({
            left: 0
        }, 500 );
        animated = true;
    }
} else if(animated){
    $('#scrolltop').fadeOut();
    $('#contact').animate({
       left: -115
    }, 500 );
    animated = false;
}
编辑:

为了解决当用户快速上下滚动时重复进行多个动画调用的问题,我将另外跟踪元素当前是否正在动画,如下所示:

    var animated = false;
    var animating = false;
    $(window).scroll(scroll);

    function scroll(){
        if(!animating) {
            if ($(document).scrollTop() > 100) {
                    if(!animated){
                    animating = true;
                        $('#scrolltop').fadeIn();
                        $('#contact').animate({
                                left: 0
                        }, {"duration":500,"complete":complete});
                        animated = true;
                    }
            } else if(animated){
                animating = true;
                    $('#scrolltop').fadeOut();
                    $('#contact').animate({
                        left: -115
                    }, {"duration":500,"complete":complete} );
                    animated = false;
            }
        }
    }

    function complete(){
        animating = false;
        scroll();
    }


在此代码中,
animated
显示元素是否滑入或滑出屏幕,而
animating
显示当前是否正在使用动画(输入或输出)。我建议您这样做,而不是尝试使用超时。

谢谢,我还意识到我需要一个超时功能来阻止它变得烦人和来回滑动。我真的不知道该怎么办。你知道我怎么能加上这个吗?你说的“前后滑动”是什么意思?
#contact
元素设置为在某个点滑入,然后在向上滚动时滑出。这不是你想要的吗?是的,我需要它来滑入滑出,但如果有人快速上下滚动几次,“#联系人”也会左右滑动相同的次数,因此即使有人停止滚动,它也会不断滑入滑出。我需要限制它在一定时间内滑入滑出的次数。例如,每2秒只滑入一次。感谢您的努力,但它仍然有问题…如果我快速向下和向上滚动,它不会滑入。然后它开始向后做动画。我不确定您使用的是什么方法,但从我看到的情况来看,这是通过bind和setTimeout完成的。我只是不知道如何把它们放在一起。对不起,我的代码中有一个bug,但现在应该修复了。如果这仍然不起作用,我可以用
setTimeout
尝试一些东西,但这真的不必要。谢谢,我还意识到我需要一个timeout函数来阻止它变得烦人和来回滑动。我真的不知道该怎么办。你知道我怎么能加上这个吗?你说的“前后滑动”是什么意思?
#contact
元素设置为在某个点滑入,然后在向上滚动时滑出。这不是你想要的吗?是的,我需要它来滑入滑出,但如果有人快速上下滚动几次,“#联系人”也会左右滑动相同的次数,因此即使有人停止滚动,它也会不断滑入滑出。我需要限制它在一定时间内滑入滑出的次数。例如,每2秒只滑入一次。感谢您的努力,但它仍然有问题…如果我快速向下和向上滚动,它不会滑入。然后它开始向后做动画。我不确定您使用的是什么方法,但从我看到的情况来看,这是通过bind和setTimeout完成的。我只是不知道如何把它们放在一起。对不起,我的代码中有一个bug,但现在应该修复了。如果这仍然不起作用,我可以用
setTimeout
尝试一些东西,但这真的不必要。谢谢,我还意识到我需要一个timeout函数来阻止它变得烦人和来回滑动。我真的不知道该怎么办。你知道我怎么能加上这个吗?你说的“前后滑动”是什么意思?