This page has been robot translated, sorry for typos if any. Original content here.

How to fix a block or menu on the site

Как зафиксировать блок или меню на сайте

How to make a fixed floating menu or block on the site.

There are many solutions. Yes, and you can write yourself, if you understand JavaScript and jQuery.

Fixed top menu on the site

Probably the most elegant and simple solution on jQuery. Code for example:

 $ (function () {$ (window) .scroll (function () {var top = $ (document) .scrollTop (); if (top <100) $ (". floating"). css ({top: '0 ', position:' relative '}); else $ (". floating"). css ({top:' 10px ', position:' fixed '});});}); 

This code needs to be inserted into a separate js-file or directly into the markup, having framed the <script> code </ script> in the tag.

We'll figure out what's going on here. So the function $ (window) .scroll (function () {}); scrolls the window. The top variable is set to the value of the scroll indent from above $ (document) .scrollTop () ;. Further, it is checked if the value of top is less than 100 pixels, then the element with the .floating class is assigned CSS-styles: relative positioning and indentation from the top - 0 pixels. If, when scrolling the page, the value of top is greater than or equal to 100 pixels, then the .floating element is assigned styles: a fixed position and an indent from the top - 10 pixels.

Now when you scroll the page, the menu will always be on top.

The fixed menu in the WordPress sidebar

For WordPress there is an interesting plugin codeflavors-floating-menu . It can be installed directly from the admin panel of WordPress. Convenient plugin. Works with the standard menu of the site. Has several settings. Easy enough to use. The developer's website has documentation for this plugin.

If you want to make it a floating menu on WordPress, then feel free to use this plugin.

Fixed block / menu in the site's sidebar on jQuery

If you want to make not only a floating menu, but a block with any content fixed, then for this you need to slightly modify the code that is given above.

  $ (document) .ready (function () {
  var br = $ .browser;
  $ (window) .scroll (function () {
  var top = $ (document) .scrollTop ();
  if (top <61) {
  $ ("# sidebar"). css ({top: '0', position: 'relative', marginLeft: '25px'});
  } else if ((! br.msie) || ((br.msie) && (br.version> 7))) {
  $ ("# sidebar"). css ({top: '22px', position: 'fixed', marginLeft: '535px'});
  } else if ((br.msie) && (br.version <= 7)) {
  $ ("# sidebar"). css ({top: '22px', position: 'fixed', marginLeft: '25px'});

This code is exactly the same, but the margin-left style is added. This is done in order to place the fixed element on the place where it should be located. Additionally, in this example, a check is also performed for the Internet Explorer 7 browser. it does not correctly handle position: fixed; .

Do not forget to connect the jQuery library!

By the way, about the library ... Since version 1.9, jQuery does not support the $ .browser function. Therefore, to use this design, you can connect this function as a separate file:

  (function () {
  var matched, browser;
 // Use jQuery.browser is not approved.
 // More information:
 // jQuery.uaMatch is supported for compatibility
 jQuery.uaMatch = function (ua) {
  ua = ua.toLowerCase ();
  var match = / (chrome) [\ /] ([\ w.] +) /. exec (ua) ||
  / (webkit) [\ /] ([\ w.] +) /. exec (ua) ||
  / (opera) (?:. * version |) [\ /] ([\ w.] +) /. exec (ua) ||
  / (msie) ([\ w.] +) /. exec (ua) ||
  ua.indexOf ("compatible") <0 && /(mozilla)(?:.*? rv :( [\ w.] +) |) /. exec (ua) ||
  return {
  browser: match [1] ||  "",
  version: match [2] ||  "0"
  matched = jQuery.uaMatch (navigator.userAgent);
  browser = {};
  if (matched.browser) {
  browser [matched.browser] = true;
  browser.version = matched.version;
 // Chrome is Webkit, but Webkit is also Safari.
  if ( {
  browser.webkit = true;
  } else if (browser.webkit) {
  browser.safari = true;
  jQuery.browser = browser;
  jQuery.sub = function () {
  function jQuerySub (selector, context) {
  return new jQuerySub.fn.init (selector, context);
  jQuery.extend (true, jQuerySub, this);
  jQuerySub.superclass = this;
  jQuerySub.fn = jQuerySub.prototype = this ();
  jQuerySub.fn.constructor = jQuerySub;
  jQuerySub.sub = this.sub;
  jQuerySub.fn.init = function init (selector, context) {
  if (context && context instanceof jQuery &&! (context instanceof jQuerySub)) {
  context = jQuerySub (context);
  return (this, selector, context, rootjQuerySub);
  jQuerySub.fn.init.prototype = jQuerySub.fn;
  var rootjQuerySub = jQuerySub (document);
  return jQuerySub;
 }) ();

Fixed block in the WordPress sidebar

Another alternative, but much more effective: jQuery-plugin hcsticky .

It has many settings, but, at the same time, it is quite easy to use.

First of all we connect jQuery and the plugin itself:

  <! - jQuery -> 
<script type = "text / javascript" src = "jquery.js"> </ script>
<! - hcSticky script ->
<script type = "text / javascript" src = "jquery.hcsticky.js"> </ script>

Then for the element that should be floating, call the method:

 $ ('# sidebar'). hcSticky ();

All. Now the block will be floating.

This plugin is very useful especially if you want to fix a high block. But when scrolling, he should not get out of the permitted limits, for example, crawl on a foot.

If you use the options that were described above, then the blocks / menus will crawl to the bottom of the site, especially when the latter has a large height.

The plugin hcsticky provides the ability, so to speak, to stop the floating block at a certain distance from the bottom of the site. Example:

 $ (document) .ready (function () {
  $ ('# sidebar'). hcSticky ({
  top: 25,
  bottomEnd: 155,
  noContainer: true


  • top - indent from the top of the browser window while scrolling
  • bottomEnd - indent from the bottom of the site when the site is scrolled to the bottom
  • noContainer - if set to true, the element is positioned relative to the document