Code Monkey home page Code Monkey logo

direction-reveal's Introduction

Direction Reveal

A plugin that detects the direction a user enters or leaves an element allowing you to reveal or hide content based on this direction.

Installation

$ npm install direction-reveal --save-dev

Usage

Import JS

The script is an ES6(ES2015) module but the compiled version is included in the build as "src/scripts/direction-reveal-umd.js". You can also copy "src/scripts/direction-reveal.js" into your own site if your build process can accommodate ES6 modules.

import DirectionReveal from 'direction-reveal';

// Init with default setup
const directionRevealDemo = DirectionReveal();

// Init with all options at default setting
const directionRevealDefault = DirectionReveal({
  selector: '.direction-reveal',
  itemSelector: '.direction-reveal__card',
  animationName: 'swing',
  animationPostfixEnter: 'enter',
  animationPostfixLeave: 'leave',
  enableTouch: true,
  touchThreshold: 250
});

Options

Property Default Type Description
selector '.direction-reveal' String Container element selector.
itemSelector '.direction-reveal__card' String Item element selector.
animationName 'swing' String Animation class.
animationPostfixEnter 'enter' String Animation CSS class postfix for enter event.
animationPostfixLeave 'leave' String Animation CSS class postfix for leave event.
enableTouch true Boolean Adds touch event to show content on first click then follow link on the second click.
touchThreshold 250 Number(ms) The touch length in ms to trigger the reveal, this is to prevent triggering if user is scrolling.

Import SASS

@import "node_modules/direction-reveal/src/styles/direction-reveal.scss";

Markup

<div class="direction-reveal">

  <a href="#" class="direction-reveal__card">
    <img src="images/image.jpg" alt="Image" class="img-fluid">

    <div class="direction-reveal__overlay direction-reveal__anim--enter">
      <h3 class="direction-reveal__title">Title</h3>
      <p class="direction-reveal__text">Description text.</p>
    </div>
  </a>

  ...
</div>

Using other tags

The demos use <a> tags for the "direction-reveal__card" but a <div> can be used as below, specifying the tabindex ensures keyboard navigation works as expected. They can all have a value of 0 and will follow the source order of the divs.

<div class="direction-reveal__card" tabindex="0">
  ...
</div>

Inverted animations

Most of the animations above can be inverted so the overlay is visible by default and animates out on hover. Change the class 'direction-reveal__anim--enter' to 'direction-reveal__anim--leave' for this effect.

You can also add the class 'direction-reveal__anim--enter' or 'direction-reveal__anim--leave' to the image to animate it at the same time as overlay. This effect can be seen in the Slide & Push demo.

Events

A 'directionChange' event is broadcast once a user enters/leaves an item with information about the action(enter,leave) and direction(top, right, bottom, left).

document.querySelector('#test').addEventListener('directionChange', (event) => { 
  console.log(`Action: ${event.detail.action} Direction: ${event.detail.direction}`);
});

Compatibility

Touch support

The plugin will detect touch support and reveal the hidden content on first click then follow link on the second click. This can be disabled with the option enableTouch.

Browser support

Supports all modern browsers(Firefox, Chrome and Edge) released as of January 2018. For older browsers you may need to include polyfills for Nodelist.forEach, Element.classList and Passive Event Listeners.

Demo site

Clone or download from Github.

$ npm install
$ gulp serve

Credits

Inspired by a Codepen by Noel Delgado, this Stack overflow answer, the article Get an Element's position using javascript and Images from Unsplash..

License

MIT © Nigel O Toole

direction-reveal's People

Contributors

dependabot[bot] avatar nigelotoole avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

direction-reveal's Issues

Support Edge, IE11, IE10

for browser support (Edge, IE11, IE10)
add polyfill:

(function () {
    if ( typeof NodeList.prototype.forEach === "function" ) return false;
    NodeList.prototype.forEach = Array.prototype.forEach;

    if (!String.prototype.startsWith) {
      Object.defineProperty(String.prototype, 'startsWith', {
        enumerable: false,
        configurable: false,
        writable: false,
        value: function(searchString, position) {
          position = position || 0;
          return this.indexOf(searchString, position) === position;
        }
      });
    }
})();

PS. Maybe it will be useful to someone

main script missed parameters, but docs provide them

animationPostfixEnter and animationPostfixLeave are missed in the main script, but is mentioned in the docs/readme as well in /docs/script

When I use the usage script:

const directionRevealDefault = DirectionReveal({
  selector: '.direction-reveal',
  itemSelector: '.direction-reveal__card',
  animationName: 'swing',
  animationPostfixEnter: 'enter',
  animationPostfixLeave: 'leave',
  enableTouch: true,
  touchThreshold: 250
});

The animationPostfixEnter and animationPostfixLeave is not picked up. The generated classes are still outand leave and not enterand leave

It took me hours to find that out :-) , please be so kind and fix that to help others who stumble over this problem

PS: For libraries which uses static page generators (eg: React, Gatsby,...) documentand windoware not defined while build time, so a check for document in line around 23 makes it possible to use your script/library "out-of-the-box":

const containers = typeof document !== "undefined" ? document.querySelectorAll(selector) : []

Cannot get this to work

Imported styles, created my DirectionReveal() with no options and copied the demo markup exactly but nothing happens?

Direction calculation

The calculation for x/y (line 33, 34 in direction-reveal.js) is not correct. The right parenthesis for the normalization is set wrong. Correct calculation:
let x = (e.pageX - position.x - (w / 2)) * (w > h ? (h / w) : 1);
let y = (e.pageY - position.y - (h / 2)) * (h > w ? (w / h) : 1);
This causes a correct normalization for aspect ratios of the hovered element not equal 1, and subsequently the correct calculation of the direction.

Bug on mouse hover on right side of direction-reveal__card

Hey there, great plugin! I have installed this on my react app and seems to be working great! I have one issue: when I mouse over the right side of my images no matter where I exit, up or down, the animation it brings up is always swing--out-right or swing--in-right. See in the screenshots, I went from the top to the bottom of the two sandwiches on the left of the screen. Strangely though, if I do it on the left half of the images, they work just fine. Any help or insight would be greatly appreciated!

Thanks again for the useful script!

Atlante
screen shot 2018-02-15 at 4 00 11 am
screen shot 2018-02-15 at 3 59 56 am

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.