1.2.5 • Published 2 years ago

float-sidebar v1.2.5

Weekly downloads
175
License
MIT
Repository
github
Last release
2 years ago

FloatSidebar.js

NPM version

A lightweight (2kb gzipped), zero-dependency javascript library for making a sidebar float.

Demo

The library is designed on top of the finite state machine pattern, which results in a straightforward solution that is pretty easy to understand and maintain.

More about the approach in the article on medium (in Russian)

Install

npm install float-sidebar --save

or

yarn add float-sidebar

or

<script src="./path/to/float-sidebar.min.js"></script>

Usage

<div class="wrapper">
  <div class="content">
    <!-- Content -->
  </div>

  <div class="sidebar">
    <div class="sidebar__inner">
      <!-- Sidebar content -->
    </div>
  </div>
</div>
.wrapper {
  display: flex;
  /* Required in case of using an infinite scroll */
  align-items: flex-start;
}

.sidebar {
  /* Required */
  position: relative;

  /* Required. The sidebar element should have a fixed width */
  width: 220px;
}
import FloatSidebar from 'float-sidebar';

const sidebar  = document.querySelector('.sidebar');
const relative = document.querySelector('.content');

const floatSidebar = FloatSidebar({
  sidebar,
  relative,
  topSpacing: 20,
  bottomSpacing: 20
});

// ...

floatSidebar.forceUpdate();

// ...

floatSidebar.destroy();

Options

NameTypeDefaultDescription
sidebarHTMLElementRequiredThe sidebar element
relativeHTMLElementRequiredThe sidebar relative element, e.g. the main content
viewportHTMLElementwindowThe viewport element
sidebarInnerHTMLElementsidebar.firstElementChildThe sidebar inner element
topSpacingnumber0The space from the top of the viewport. It is used when the sidebar is fixed.
bottomSpacingnumber0The space from the bottom of the viewport. It is used when the sidebar is fixed.

Instance API

MethodDescription
forceUpdate()Recalculates all the dimensions and finally updates the position of the sidebar.
destroy()Disposes the DOM listeners.

Similar libraries

License

FloatSidebar.js is released under the MIT license.

Author Sergey Vinogradov

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago