Ads

Tuesday 16 December 2014

Focusable





Focusable is an awesome and lightweight library for performing spotlight in your DOM elements, setting an animated overlay to the rest of the page.
1. INCLUDE JS FILES
<script src="jquery-2.1.1.js"></script>
2. HTML
<div class="example">
    <header>
      <button class="show" data-selector="header">Focus header</button>
      <button class="show" data-selector="ul">Focus list</button>
      <button class="show" data-selector="li:first">Focus first item</button>
      <button class="show" data-selector="img">Focus image</button>
    </header>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
    </ul>
</div>
<div class="options">
    <h2>Options:</h2>
    <ul>
      <li>- fadeDuration (seconds) <input type="text" id="fade-duration" value="700"></li>
      <li>- hideOnClick <input type="checkbox" id="hide-on-click" checked></li>
      <li>- hideOnEsc <input type="checkbox" id="hide-on-esc" checked></li>
      <li>- findOnResize <input type="checkbox" id="find-on-resize"></li>
    </ul>
</div>
3. JAVASCRIPT
(function() {
  $(document).ready(init);
 
  function init() {
    $('.show').on('click', show);
    $('.hide').on('click', hide);
 
    function show() {
      var selector = $(this).attr('data-selector');
      var options = getOptions();
      Focusable.setFocus($(selector), options);
    }
 
    function hide() {
      Focusable.hide();
    }
  }
 
  function getOptions() {
    return {
      fadeDuration: parseInt($('#fade-duration').val()),
      hideOnClick: $('#hide-on-click').is(':checked'),
      hideOnESC: $('#hide-on-esc').is(':checked'),
      findOnResize: $('#find-on-resize').is(':checked')
    };
  }
})();
4. API
Set spotlight (jQuery style)
$('#my-element').setFocus(options);
Set spotlight (through library)
Focusable.setFocus($('#my-element'), options);
Refresh current focused element
Focusable.refresh();
Hide spotlight
Focusable.hide();
5. OPTIONS
PropertyValueDefaultDescription
fadeDurationNumber700Duration of the overlay transition (milliseconds).
hideOnClickBooleanfalseHides the overlay when the user click into it.
hideOnESCBooleanfalseHides the overlay when the user press Esc.
findOnResizeBooleanfalseRefind the element in the DOM in case that the element don’t still exists.

No comments:

Post a Comment