The javascript code to make the sidebar float and stick to screen as you scroll down. This code is specific to twentythirteen wordpress theme and most probably won’t work with any other theme.
I didn’t like it when the sidebar area goes blank as i scroll down the page. The content in the main content area(towards left) is more than the content(widgets) in sidebar(towards right) so as one scrolls down the page, the sidebar area becomes empty.
The following javascript does the trick. As you scroll down the page and scroll past the bottom of sidebar, the sidebar sticks to the bottom right of the page. You keep going down and it stays there. And when you scroll up to a sufficient height, it again acquires its normal position. The script also prevents sidebar from overlapping with ‘header’ and ‘footer’ areas.
Any demo ? : yes, this very page(or any other page on this website) is a live demo of the script. Try scolling!! Its more apparent on longer pages like home page.
This script results in nice dirty trick on this website. On a fresh page reload, you don’t see ads in the sidebar becuase that widget at the end of the sidebar and becomes visible only when you scroll down. And once you scroll down, only those ads are visible, and you can’t get rid of them
..
Anyway so here’s the javascript, you can save it in any of the existing javascript of your theme or save it as a new file and enqueue the file properly:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
/* http://webdeveloperswall.com/javascript/floating-sidebar-for-twentythirteen */ var jq=jQuery.noConflict(); var wdw_fs_status = "unaltered"; jq(window).load(function(){ //if there is a sidebar, in the first place if( jq( "#secondary.sidebar-container" ).length == 0 ){ return false; } if( jq(window).width() < 1000 ){ //in twentythirteen if the browser width is less than 1000px, //the page becomes one columned and sidebar goes down, so no need to do anything return false; } jQuery(document).scroll(function(){ /* 1. if user has scrolled down more than the total effective height of the sidebar 2. make the sidebar fixed position with bottom as 0 3. calculate the left postion by calucalting sidebar's offset.left 4. if the scroll is decreased to less than the total effective height of sidebar revert it back to its original condition */ var sidebar_pl = jq( "#secondary.sidebar-container .widget-area" ); if( jq(document).scrollTop() > jq(sidebar_pl).outerHeight() ){ if( wdw_fs_status=="unaltered" ){ jq(sidebar_pl).css({ 'position' : 'fixed', 'left' : jq(sidebar_pl).offset().left + 'px' }); wdw_fs_status = "altered"; } //the sidebar shouldn't overlapp with footer, stop it just before footer var content_bottom_pos = jq("#primary").outerHeight(); if( jq(document).scrollTop() > content_bottom_pos ){ //we've now reached till footer, lets stop the sidebar var bottom = jq(document).scrollTop() - content_bottom_pos; var top = content_bottom_pos - jq(sidebar_pl).outerHeight(); jq(sidebar_pl).css({ 'position' : 'absolute', 'top' : top + 'px', 'bottom':'auto' }); wdw_fs_status = "altered"; } else{ jq(sidebar_pl).css({ 'position' : 'fixed', 'bottom' : '0', 'top':'auto' }); } } else{ if( wdw_fs_status=="altered" ){ jq(sidebar_pl).css({ 'position' : 'relative', 'bottom' : 'auto', 'left' : 'auto' }); wdw_fs_status = "unaltered"; } } }); }); |
That’s it here. I didn’t bother to check it in IE! so be warned

with this 