Easy jQuery Affix,Scrollspy And Reading Progress Plugin - newsAffix

File Size: 11.2 KB
Views Total: 1494
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Easy jQuery Affix,Scrollspy And Reading Progress Plugin - newsAffix

newsAffix is a simple-to-use jQuery affix / scrollspy / reading progress plugin used to generate a sticky header navigation & scroll progress indicator for your long web content.

Features:

  • Highlights the nav items when you you scroll past their content sections.
  • Displays a progress bar to indicate the reading progress of current content section.
  • Scrolls smoothly between content sections by clicking on the nav items.

How to use it:

1. Create content sections where you want to apply the newsAffix plugin.

<div id="targeted-wrapper" class="content">

  <section class="content__section">
    <h2 class="content__title js-na-header-start" data-na-header="1">Header title 1</h2>
    <p class="content__text">Section 1</p>

    <h3 class="content__subtitle">This is sub-title 1</h3>
    <p class="content__text">Sub Section 1</p>
  </section>

  <section class="content__section">
    <h2 class="content__title" data-na-header="2">Header title 2</h2>
    <p class="content__text">Section 2</p>

    <h3 class="content__subtitle">This is sub-title 2</h3>
    <p class="content__text">Sub Section 2</p>
  </section>

  <section class="content__section">
    <h2 class="content__title" data-na-header="3">Header title 3</h2>
    <p class="content__text">Section 3</p>

    <h3 class="content__subtitle">This is sub-title 3</h3>
    <p class="content__text">Sub Section 3</p>
  </section>

  <section class="content__section">
    ...
  </section>

</div>

2. Load jQuery library and the jQuery newsAffix plugin at the end of the document.

<script src="//code.jquery.com/jquery.min.js"></script>
<script src="js/jquery.newsAffix.js"></script>

3. Initialize the plugin with default options.

$('body').newsAffix();

4. Let's start to style the plugin as follows:

.news-affix {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  left: -100%;
  -webkit-transition: 0.3s ease-in-out;
  -o-transition: 0.3s ease-in-out;
  -moz-transition: 0.3s ease-in-out;
  transition: 0.3s ease-in-out;
}

.news-affix__items {
  margin: 0;
  padding: 0;
  background-color: #333;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -webkit-flex-direction: row;
  -moz-box-orient: horizontal;
  -moz-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.news-affix__action {
  display: block;
  color: #fff;
  padding: 15px 10px;
}

.news-affix__action:hover, .news-affix__action:focus, .news-affix__action:active {
  text-decoration: none;
  color: #e6e6e6;
  background-color: #1a1a1a;
}

.news-affix__item {
  position: relative;
  overflow: hidden;
  -webkit-box-ordinal-group: 3;
  -webkit-order: 2;
  -moz-box-ordinal-group: 3;
  -ms-flex-order: 2;
  order: 2;
}

.news-affix__item.active {
  display: block;
  -webkit-box-ordinal-group: 2;
  -webkit-order: 1;
  -moz-box-ordinal-group: 2;
  -ms-flex-order: 1;
  order: 1;
  -webkit-box-flex: 1;
  -webkit-flex: 1 0 100%;
  -moz-box-flex: 1;
  -ms-flex: 1 0 100%;
  flex: 1 0 100%;
}

.news-affix__item.active .news-affix__action { background-color: #2980b9; }

.news-affix__item.active .news-affix__bar {
  position: absolute;
  z-index: 10;
  background: #226998;
  left: 0;
  top: 0;
  height: 100%;
  width: 0;
}

.news-affix__item:not(.active) .news-affix__action { padding: 5px 10px; }

.news-affix__item:not(.active) .news-affix__chapter { font-size: 14px; }

.news-affix__item:not(.active) .news-affix__title { font-size: 10px; }

.news-affix__item.completed { opacity: .2; }

.news-affix__chapter {
  margin: 0;
  position: relative;
  z-index: 20;
}

.news-affix__title {
  margin: 0;
  position: relative;
  z-index: 20;
}

.news-affix__bar-inverse { display: none; }

.news-affix.active { left: 0; }

5. Default plugin options.

$('body').newsAffix({

  // selctors
  headerWrapper: "#targeted-wrapper",
  headerClass: ".content__title",
  sidebar: ".js-news-affix-items",
  newsAffixSel: ".js-news-affix",
  setAffixClasses: {
    cssClass: '.news-affix__bar',
    cssRule: 'width'
  },
  onClickElements: '.js-affix-click',

  // animation speed
  scrollAnimationSpeed: 750
  
});

This awesome jQuery plugin is developed by tmatijev. For more Advanced Usages, please check the demo page or visit the official website.