カラム固定

ライブドアみたいにスクロールしたらサイドバーの広告を固定する ぼくはまちちゃん!

<div id="main">
<h2>メイン</h2>
</div><!-- /#main -->

<div id="side">
<h2>サイド</h2>
<div class="section" id="ad1">
<h3>広告</h3>
	<div id="googleAds">
	アドセンス 160x600
	</div>
</div>
</div><!-- /#side -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">
(function($) {
	$(document).ready(function() {
		/*
		Ads Sidewinder
		by Hamachiya2. http://d.hatena.ne.jp/Hamachiya2/20120820/adsense_sidewinder
		*/
		var main = $('#main'); // メインカラムのID
		var side = $('#side'); // サイドバーのID
		var wrapper = $('#ad1'); // 広告を包む要素のID

		var w = $(window);
		var wrapperHeight = wrapper.outerHeight();
		var wrapperTop = wrapper.offset().top;
		var sideLeft = side.offset().left;

		var sideMargin = {
			top: side.css('margin-top') ? side.css('margin-top') : 0,
			right: side.css('margin-right') ? side.css('margin-right') : 0,
			bottom: side.css('margin-bottom') ? side.css('margin-bottom') : 0,
			left: side.css('margin-left') ? side.css('margin-left') : 0
		};

		var winLeft;
		var pos;

		var scrollAdjust = function() {
			sideHeight = side.outerHeight();
			mainHeight = main.outerHeight();
			mainAbs = main.offset().top + mainHeight;
			var winTop = w.scrollTop();
			winLeft = w.scrollLeft();
			var winHeight = w.height();
			var nf = (winTop > wrapperTop) && (mainHeight > sideHeight) ? true : false;
			pos = !nf ? 'static' : (winTop + wrapperHeight) > mainAbs ? 'absolute' : 'fixed';
			if (pos === 'fixed') {
				side.css({
					position: pos,
					top: '',
					bottom: winHeight - wrapperHeight,
					left: sideLeft - winLeft,
					margin: 0
				});

			} else if (pos === 'absolute') {
				side.css({
					position: pos,
					top: mainAbs - sideHeight,
					bottom: '',
					left: sideLeft,
					margin: 0
				});

			} else {
				side.css({
					position: pos,
					marginTop: sideMargin.top,
					marginRight: sideMargin.right,
					marginBottom: sideMargin.bottom,
					marginLeft: sideMargin.left
				});
			}
		};

		var resizeAdjust = function() {
			side.css({
				position:'static',
				marginTop: sideMargin.top,
				marginRight: sideMargin.right,
				marginBottom: sideMargin.bottom,
				marginLeft: sideMargin.left
			});
			sideLeft = side.offset().left;
			winLeft = w.scrollLeft();
			if (pos === 'fixed') {
				side.css({
					position: pos,
					left: sideLeft - winLeft,
					margin: 0
				});

			} else if (pos === 'absolute') {
				side.css({
					position: pos,
					left: sideLeft,
					margin: 0
				});
			}
		};
		w.on('load', scrollAdjust);
		w.on('scroll', scrollAdjust);
		w.on('resize', resizeAdjust);
	});
})(jQuery);
</script>

最終更新: 2013-03-11 (月) 17:40:47