$(window).scroll函数只做一次

时间:2015-07-01 14:29:25

标签: jquery

你好,我有这个脚本,我想要那个

$(window).scroll(function(){

    var FromTop = $(window).scrollTop(); //scroll margin from top
    var RightWrapper = $('.right_wrapper').height(); // Right wrapper height

    var Margin = false;

    var ConentHeight = $('.content_wrapper').height() + RightWrapper; //content height

    if(FromTop > 125 && RightWrapper < 500){
        $('.right_wrapper').addClass('right-fixed-top');
        Margin = true;
    }else{
        $('.right_wrapper').removeClass('right-fixed-top');
    }

    if(Margin){ // <<<<<<PROBLEM IS HERE each scroll 
        $('.content_wrapper').css({height:ConentHeight+'px'});
    }

});

$('.content_wrapper').css({height:ConentHeight+'px'});只是第一次。

1 个答案:

答案 0 :(得分:4)

Margin的值替换为false

$(window).scroll(function(){

    var FromTop = $(window).scrollTop(); //scroll margin from top
    var RightWrapper = $('.right_wrapper').height(); // Right wrapper height

    var Margin = false;

    var ConentHeight = $('.content_wrapper').height() + RightWrapper; //content height

    if(FromTop > 125 && RightWrapper < 500){
        $('.right_wrapper').addClass('right-fixed-top');
        Margin = true;
    }else{
        $('.right_wrapper').removeClass('right-fixed-top');
    }

    if(Margin){
        $('.content_wrapper').css({height:ConentHeight+'px'});
        Margin = false;
    }

});