2016-04-14 6 views
1

以下は私のコードの一部です。私はjquery animationコールバック関数を使用しています。スクロールでコールバック関数が動作しないjqueryアニメーション

headertop:-141pxをアニメーション化したい場合は、時間ギャップをあけてアニメーションし、top:-63pxとします。

$(window).scroll(function() { 
 
    if ($(this).scrollTop() > 1){ 
 

 
    $("#header").animate({ 
 
     position: 'fixed', 
 
     top: '-141px', 
 
    },function(){ 
 
     $(this).animate({ 
 
     position: 'fixed', 
 
     top: '-63px', 
 
     }) 
 
    }); 
 
    } 
 
});
*{ 
 
    margin:0; 
 
    padding:0 
 
} 
 

 
#header{ 
 
    height:141px; 
 
    background:#333; 
 
    color:#fff; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
<header id="header"> 
 
    Header section 
 
</header> 
 

 
<div> 
 
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> 
 
</div>

答えて

1

アニメーション方法はpositionのようなプロパティをサポートするので、それ

$(window).scroll(function() { 
 
    if ($(this).scrollTop() > 1) { 
 

 
    $("#header").stop(true).css({ 
 
     position: 'fixed' 
 
    }).animate({ 
 
     top: '-141px', 
 
    }, function() { 
 
     $(this).animate({ 
 
     top: '-63px', 
 
     }) 
 
    }); 
 
    } 
 
});
* { 
 
    margin: 0; 
 
    padding: 0 
 
} 
 
#header { 
 
    height: 141px; 
 
    background: #333; 
 
    color: #fff; 
 
} 
 
div { 
 
    height: 1000px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
<header id="header"> 
 
    Header section 
 
</header> 
 

 
<div> 
 
</div>

+0

を設定するために、CSSを使用しない迅速な回答に感謝しますが、さようならあなたのコード '各スクロールをアニメートします。 –

+0

@Maddyこれは、 'if($(this).scrollTop()> 1){' - スクロールハンドラにあなたのニーズに合わせて調整する必要があるためです –

関連する問題