2016-05-17 13 views
1

こんにちは私はスクロールに依存する要素をアニメーションする必要があります。それはしかし、それは1400pxにアニメーションを開始し、誰がどこスクロール位置でjqueryアニメーション

HTML

<div class="container"> 
    <section id="first-section"></section> 
    <section id="second-section"></section> 
</div> 

CSSを知っている終える1800pxを渡すとき、私のポイントは、トップの位置1200pxに要素を設定したが1000pxの上にアニメーションを開始し、それを終了しました

#second-section { 
    position: relative; 
    top: 1200px; 
    left: 0; 
    height: 200px; 
    border: 2px solid blue; 
    width: 100%; 
    background-color: red; 
    opacity: 1; 
} 

JAVASCRIPT

$(window).scroll(function() { 
    var top = $(this).scrollTop(); 
    if (top > 1000 && top < 1800) { 
     $("#second-section").stop().animate({ 
      opacity: "1" 
     }, 'slow'); 
    } else { 

     $("#second-section").stop().animate({ 
      opacity: "0" 
     }, 'slow'); 
    } 
}); 
+0

私の例を参照してください、あなたは(http://prinzhorn.github.io/skrollr/)[skrollr]について聞いたの。スクロール位置に応じて簡単に要素をアニメートできます。 – Hareesh

+0

いいえ、私はdidnt ..しかし、感謝私はそれを試みるつもりです:) – george

答えて

0

あなたの状態は正しくありません。この目的のために、heightpositionの要素を取得する必要があります。

$(window).scroll(function() { 
 
    var winHeight = $(this).height(); 
 
    var scrollTop = $(this).scrollTop(); 
 
    
 
    var elemHeight = $("#first-section").height(); 
 
    var elementTop = $("#first-section").position().top; 
 
     
 
    if (elementTop < scrollTop + winHeight && scrollTop < elementTop + elemHeight) 
 
     var opacity = 1; 
 
    else 
 
     var opacity = 0; 
 
    
 
    $("#first-section").stop().animate({ 
 
     opacity: opacity 
 
    }, "slow"); 
 
});
.container { 
 
    height: 2000px; 
 
} 
 

 
#first-section { 
 
    position: relative; 
 
    top: 1200px; 
 
    left: 0; 
 
    height: 200px; 
 
    border: 2px solid blue; 
 
    width: 100%; 
 
    background-color: red; 
 
    opacity: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    <section id="first-section"></section> 
 
</div>

関連する問題