0

i am working on the parallax scroller and need a bit of help with the jquery animation.

if($(window).scrollTop() >= 180){
    $("#wasBorn").animate({"top": "+=20px"}, "fast");       }

as the page scrolls more than 180 the div is moving and how can i stop it at particular position let say top:350px

4

1 に答える 1

0
if( $(window).scrollTop() >= 180
    && parseInt( $('#wasBorn').css('top') )<=330 ){
  $("#wasBorn").animate({"top": "+=20px"}, "fast");
}

#wasBornただし、要素が 350px より前で停止する可能性があるため、これは完全な解決策ではありません(たとえば、top値が 320 のときにトリガーされた場合、アニメーションの後、340 になるため、ターゲットの 350 未満になります。ただし、330 のテストよりも大きい)。しかし、あなたがこの行動をどのように引き起こしているのか、これ以上わからないので、これは私が現時点で思いつくことができる最善の「腰からの」提案/解決策です.

交互に...

$wasBorn = $('#wasBorn');
....
if( $(window).scrollTop() >= 180 ){
  var curr = parseInt( $wasBorn.css('top') );
  if( curr<350 ){
    $wasBorn.animate(
      { top : Math.min( 350 , curr+20 ) } ,
      'fast'
    );
  }
}
于 2013-03-04T00:20:29.890 に答える