HTML/CSS アプローチ
多くの JavaScript を必要としないオプションを探している場合 (および、それに伴う高速なスクロール イベント呼び出しなどの問題をすべて必要としないオプション) を探している場合は、ラッパー<div>
といくつかのスタイルを追加することで、同じ動作を得ることができます。次のアプローチを使用すると、はるかにスムーズなスクロール (遅れる要素がない) に気付きました。
JSフィドル
HTML
<div id="wrapper">
<div id="fixed">
[Fixed Content]
</div><!-- /fixed -->
<div id="scroller">
[Scrolling Content]
</div><!-- /scroller -->
</div><!-- /wrapper -->
CSS
#wrapper { position: relative; }
#fixed { position: fixed; top: 0; right: 0; }
#scroller { height: 100px; overflow: auto; }
JS
//Compensate for the scrollbar (otherwise #fixed will be positioned over it).
$(function() {
//Determine the difference in widths between
//the wrapper and the scroller. This value is
//the width of the scroll bar (if any).
var offset = $('#wrapper').width() - $('#scroller').get(0).clientWidth;
//Set the right offset
$('#fixed').css('right', offset + 'px');
});
もちろん、このアプローチは、実行時にコンテンツを獲得/喪失するスクロール領域用に変更できます (スクロールバーの追加/削除が発生します)。