I had inspired floating submenu like, http://my-jpn.com/aboutcontest/ and http://lesscss.org/ .
But they have one problem that forgot about footer like below.
/* floating Submenu*/ $(function () { var msie6 = $.browser == 'msie' && $.browser.version < 7; if (!msie6) { var top = $('#Submenu').offset().top - parseFloat($('#Submenu').css('margin-top').replace(/auto/, 0)); $(window).scroll(function (event) { // what the y position of the scroll is var y = $(this).scrollTop(); // whether that's below the form if (y >= top) { // if so, ad the fixed class $('#Submenu').addClass('fixed'); } else { // otherwise remove it $('#Submenu').removeClass('fixed'); } }); } });
Source: http://my-jpn.com/aboutcontest/
So, I've add some fix, here is full source code.
<style> .fixed { position: fixed; top: 0; } </style> <div id="left" style="submenu"> Some menu items here... </div> <script type="text/javascript"> /* floating Submenu*/ $(function () { var $menu = $('#left'); var buttom_margin = 310; // px. normally height of footer. var msie6 = $.browser == 'msie' && $.browser.version < 7; if (!msie6) { var top = $menu.offset().top - parseFloat($menu.css('margin-top').replace(/auto/, 0)); // Remember original top position. var menu_height = $menu.outerHeight(); $(window).scroll(function (event) { // what the y position of the scroll is var y = $(this).scrollTop(); var doc_height = $(document).height() - buttom_margin; var bottom = y + menu_height; // Test code //console.log(y + '/' + top + '/' + bottom + '/' + doc_height + '/' + $(document).height()); // whether that's below the form if (y >= top) { // if so, ad the fixed class $menu.addClass('fixed'); // limit of fix if ( bottom < doc_height ) { $menu.css("top","0"); } else { $menu.css("top","-" + (bottom - doc_height) + "px"); } } else { // otherwise remove it $menu.removeClass('fixed'); } }); } }); </script>
You can set Bottom of Margin. Simply change value, buttom_margin = 000.
DEMO - Cafetalk (Need login.)
Don't you want to jQuery and CSS code? Check it. (but, this code is not considered Bottom of Margin.)
<script type="text/javascript"> (function () { if (/Microsoft/.test(navigator.appName)) { return } window.onload = function () { var menu = document.getElementById('[DOM_ID_HERE]'); var init = menu.offsetTop; var docked; window.onscroll = function () { if (!docked && (menu.offsetTop - scrollTop() < 0)) { menu.style.top = 0; menu.style.position = 'fixed'; menu.className = 'docked'; docked = true; } else if (docked && scrollTop() <= init) { menu.style.position = 'absolute'; menu.style.top = init + 'px'; menu.className = menu.className.replace('docked', ''); docked = false; } }; }; function scrollTop() { return document.body.scrollTop || document.documentElement.scrollTop; } })(); </script>
Source: http://lesscss.org/
メニューが Main Content より縦幅が大きい場合、メニューが Floating し始めると(つまり、Position: Fixed されることによって)、Main Content の縦幅が縮んでしまう。それで画面が大きく揺れる(瞬時に歪んで、戻るを繰り返す)バグがあって修正。
解決方法として、Main Content の縦幅を事前に取得し、Menu の縦幅と比較して、Menu が大きい場合、Menu を Fixed しない用に工夫。
/* floating Submenu*/ $(function () { var $menu = $('#left'); var $main = $('#main'); var buttom_margin = 310; // px. normally height of footer. var msie6 = $.browser == 'msie' && $.browser.version < 7; if (!msie6) { var top = $menu.offset().top - parseFloat($menu.css('margin-top').replace(/auto/, 0)); // Remember original top position. var menu_height = $menu.outerHeight(); var main_height = $main.outerHeight(); $(window).scroll(function (event) { // what the y position of the scroll is var y = $(this).scrollTop(); var doc_height = $(document).height() - buttom_margin; var bottom = y + menu_height; // whether that's below the form if ( y >= top && menu_height < main_height ) { // if so, ad the fixed class $menu.addClass('fixed'); // limit of fix if ( bottom < doc_height ) { $menu.css("top","0"); } else { $menu.css("top","-" + (bottom - doc_height) + "px"); } } else { // otherwise remove it $menu.removeClass('fixed'); } }); } });