2012-04-23 7 views
2

私はこれらのいくつかの例を見ましたが、私のものは少し異なり、これを理解することはできません。固定小数点を特定の点までスクロール

私はこれらのフォーラムで見つかったjQueryを使用して、fixed divを特定のポイントまでスクロールし、 'scrollTop'を使用して停止しています。

var windw = this; 

$.fn.followTo = function (pos) { 
    var $this = this, 
     $window = $(windw); 

    $window.scroll(function(e){ 
     if ($window.scrollTop() > pos) { 
      $this.css({ 
       position: 'absolute', 
       top: pos 
      }); 
     } else { 
      $this.css({ 
       position: 'fixed', 
       top: 40 
      }); 
     } 
    }); 
}; 

$('#scrollto-menu-nav').followTo(250); 

しかし、私はそれがトップからないからの高さに達したときにスクロールを停止することを必要としています。何か案は?

おかげで、 R

+0

下から停止したい項目の高さの合計を計算してから、その項目の数値を差し引いて停止したい場所に移動することができます。たとえば、150〜100は、50という位置値を与えます。 – KryptoniteDove

答えて

2

あなたはこのような何か行うことができます:

HTML

<img src=//ph.artsinn.de alt> 
<div> 
    <p>A short line of text, for better interpretation.</p> 
    <p>Followed by another line, filled with letter and words</p> 
</div> 

<img src=//ph.artsinn.de/240x160/666?text=scroll%20down alt id=obj> 

<img src=//ph.artsinn.de alt> 
<img src=//ph.artsinn.de alt> 
<img src=//ph.artsinn.de alt class=end> 

<div> 
    <p>A short line of text, for better interpretation.</p> 
    <p>Followed by another line, filled with letter and words</p> 
</div> 

<img src=//ph.artsinn.de alt> 
<img src=//ph.artsinn.de alt> 
<img src=//ph.artsinn.de alt> 
<img src=//ph.artsinn.de alt>​ 

CSS

/* not needed */ 
html,body{height:200%} 
img {display:block}​ 

JS fiddleとして

$(function() { 
    var $window = $(window), 
     $sidebar = $("#obj"), 
     sidebarTop = $sidebar.position().top, 
     sidebarHeight = $sidebar.height(), 
     $footer = $(".end"), 
     footerTop = $footer.position().top; 

    $sidebar.css('position', 'fixed'); 
    $window.scroll(function(e) { 
     scrollTop = $window.scrollTop(); 
     topPosition = Math.max(0, sidebarTop - scrollTop); 
     topPosition = Math.min(topPosition, (footerTop - scrollTop) - sidebarHeight); 
     $sidebar.css('top', topPosition); 
    }); 
});​ 

そして、再び、。

関連する問題