tracky.js
tracky.js is a helper streamlining user interaction into css-classes. This means tracky transforms user interaction like scrolling, device orientation changes or cursor movements on defined breakpoints into css classes.
Install && Getting started
Installation
npm install tracky --save
Example ES2015:
; let bodyTracker = 'body' options: 'example' ;
Example common.js:
let Tracky = ; let bodyTracker = 'body' options: 'example' ;
Example ES5:
<script> var bodyTracker = 'body' options: 'example' ;</script>
Documentation
Tracky
Tracky( selector, { options } )
Arguments
- selector (String | Array | default: 'body') - selector or array of strings that fits into
querySelectAll()
. - options (Object | default: null) - options for your Tracky instance
Returns
Tracky instance
Example
// apply trackers to body'body'; // apply trackers to .selector1 and .selector2'.selector1' '.selector2'; // apply trackers to any div with the default options'div' classPrefix: 'tracky-' classSuffix: '' classGtPrefix: 'gt-' classLtPrefix: 'lt-' classEqPrefix: 'eq-' classBtPrefix: 'bt-' events: scroll: enable: false breakpoints: edge: enable: false breakpoints: orientation: enable: false breakpoints: ;
Tracky.addSelector()
Adds one or many selector to the existing selectors and attach events.
.addSelector( selector )
Arguments
- selector (String | Array) - selector or array of strings that fits into
querySelectAll()
.
Returns
Tracky instance
Example
// apply trackers to bodylet t = 'body'; // add .selector1 and .selector2t; // add #single-selectort;
Tracky.removeSelector()
Removes one or many selector from the existing selectors and unbind events.
.removeSelector( selector )
Arguments
- selector (String | Array) - selector or array of selectors that are currently added to the instance.
Returns
Tracky instance
Example
// apply trackerslet t = 'body''.s2''.s3''#s4'; // remove .s2 and .s3t; // remove #s4t;
Tracky.disable()
Disables a given tracker and unbind the event listeners.
.disable( tracker )
Arguments
- tracker (String) - tracker as a string that is currently enabled.
Returns
Tracky instance
Example
// apply events to bodylet t = 'body' events : scroll : enable: true breakpoints: 255 '99%' ; // disabling scroll eventst;
Tracky.enable()
Enables a given event and unbind the event listeners.
.enable( tracker )
Arguments
- tracker (String) - tracker as a string from the listeners.
Returns
Tracky instance
Example
// apply events to bodylet t = 'body' events : orientation : enable: false breakpoints: 255 '99%' ; // enabling orientation eventst;
Event Options
scroll
Tracks horizontal and vertical scroll.
(Notice: console recording and demo above are not synced)
Default classNames
If no custom css class-name is defined. Tracky creates a unique name by the given values by default. Note that the %-digit will be replaced by 'pc' (percent).
// value: [value]optionsclassPrefix'scroll'optionsclassLtPrefixvalueoptionsclassSuffix // when lower than [value]optionsclassPrefix'scroll'optionsclassGtPrefixvalueoptionsclassSuffix // when greater than [value]optionsclassPrefix'scroll'optionsclassEqPrefixvalueoptionsclassSuffix // when equal than [value]
// value: { min, max } // as a rangeoptionsclassPrefix'scroll'optionsclassBtPrefixmin'-'maxoptionsclassSuffix // when between {min} and {max}
// value: 50tracky-scroll-lt-50 // when lower than 50pxtracky-scroll-gt-50 // when greater than 50pxtracky-scroll-eq-50 // when equal than 50px
// value: 77%tracky-scroll-lt-77pc // when lower than 77%tracky-scroll-gt-77pc // when greater than 77%tracky-scroll-eq-77pc // when equal than 77%
// min: 5% / max: 200tracky-scroll-bt-5pc-200 // when between 5% and 200 pixel
Example configuration:
events: scroll: /* Enable the scroll tracker */ enable: true /* Array with breakpoints */ breakpoints: /* Breakpoints can be set as a single number */ 0 /* Breakpoint can be set as percentage */ '50%' /* Breakpoint set as object with property value given */ value: '99%' /* Breakpoint set as object with min and max property */ min: 60 max: '40%' /* CSS class as a string will be applied when between 40% and 60% */ css: 'element-scroll-is-in-range' min: '40%' max: '60%' /* complex custom breakpoint example */ /* css as an object */ css: /* class applied when scroll < value */ lt: 'this-is-my-special-class-under-350' /* class applied when scroll > value */ gt: 'this-is-my-special-class-over-350' /* class applied when scroll === value */ eq: 'this-is-my-special-class-at-350' /* scroll direction measured ['x' | 'y'] (x = horizontal / y = vertical) (Default: 'y') */ axis: 'x' /* Breakpoint as value */ value: 350 /* Callback when 'eq' -> scroll === value | min-max-range */ { console; } /* Callback when scroll position does not match anymore */ { console; } /* Callback when scroll > value (fired only once per condition change) */ { console; } /* Callback when scroll < value (fired only once condition change) */ { console; } /* min-max-range example */ /* css as an object */ css: /* class applied when scroll >= min && scroll <= max */ /* lt / gt / eq do not work with a min and max range */ bt: 'this-is-my-special-class-match-range' /* min value */ min: 15 /* max value */ max: '60%'
edge
Tracks mousemove on a certain area of an elements edge.
(Notice: console recording and demo above are not synced)
Default classNames
If no custom css class-name is defined. Tracky creates a unique name by the given values by default. Note that the %-digit will be replaced by 'pc' (percent).
// value: [value]optionsclassPrefix'edge'matchedPosition='top | right | bottom | left'valueoptionsclassSuffix
// in case of a corner the values will be combined// value: [ y | x ]optionsclassPrefix'edge'matchedPosition='top-right | top-left | bottom-right | bottom-left'valuey'-'valuexoptionsclassSuffix
// value: 50tracky-edge-top-50 // when touches toptracky-edge-right-50 // when touches righttracky-edge-bottom-50 // when touches bottomtracky-edge-left-50 // when touches lefttracky-edge-top-right-50-50 // when touches top-righttracky-edge-top-left-50-50 // when touches top-lefttracky-edge-bottom-right-50-50 // when touches bottom righttracky-edge-bottom-left-50-50 // when touches bottom left
// value: [50,'25%',10,90]tracky-edge-top-50 // when touches toptracky-edge-right-25pc // when touches righttracky-edge-bottom-10 // when touches bottomtracky-edge-left-90 // when touches lefttracky-edge-top-right-50-25pc // when touches top-righttracky-edge-top-left-50-90 // when touches top-lefttracky-edge-bottom-right-10-25pc // when touches bottom righttracky-edge-bottom-left-10-90 // when touches bottom left
Example configuration object
events: edge: breakpoints: /* defines an edge area of 50 pixel */ 50 /* defines an edge area of 20 percent */ '20%' /* defines an edge area as array */ /* of top: 18px right: 55px, bottom: 66%, left: 77px */ 18 55 '66%' 77 /* define an edge area with a more complex object */ css: /* class applied when cursor hits the top edge area */ top: 'edge-tracker-top' /* class applied when cursor hits the right edge area */ right: 'edge-tracker-right' /* class applied when cursor hits the bottom edge area */ bottom: 'edge-tracker-bottom' /* class applied when cursor hits the left edge area */ left: 'edge-tracker-left' /* class applied when cursor hits the overlapping coner of top and right edge area */ topRight: 'edge-tracker-top-right' /* class applied when cursor hits the overlapping coner of top and left edge area */ topLeft: 'edge-tracker-top-left' /* class applied when cursor hits the overlapping coner of bottom and right edge area */ bottomRight: 'edge-tracker-bottom-right' /* class applied when cursor hits the overlapping coner of bottom and left edge area */ bottomLeft: 'edge-tracker-bottom-left' /* value defines the edge area around the element */ value: '15%' /* callback when cursor touches any edge area */ { console; } /* callback when cursor has left any edge area */ { console; } /* callback when cursor touches top edge area */ { console; } /* callback when cursor touches right edge area */ { console; } /* callback when cursor touches bottom edge area */ { console; } /* callback when cursor touches left edge area */ { console; } /* callback when cursor touches top right corner */ { console; } /* callback when cursor touches top left corner */ { console; } /* callback when cursor touches bottom right corner */ { console; } /* callback when cursor touches bottom left corner */ { console; } /* callback when cursor left top edge */ { console; } /* callback when cursor left right edge */ { console; } /* callback when cursor left bottom edge */ { console; } /* callback when cursor left left edge */ { console; } /* callback when cursor left top right corner */ { console; } /* callback when cursor left top left corner */ { console; } /* callback when cursor left bottom right corner */ { console; } /* callback when cursor left bottom left corner */ { console; } /* define a value as an array of (top/right/buttom/left) */ value: '15%' '88%' 45 66 /* define a value as an object with specific areas */ value: top: 44 right: 88 bottom: '66%' left: 99 /* define css class as string will apply this class whenever an area is touched */ css: 'hit-any-edge' /* define a value with an array of 2 elements will follow the css rules for padding/margin */ /* so this will autocomplete bottom and left like - top: 14px, right: 66px, bottom: 14px, left: 66px */ value: 14 66
orientation
Tracks device orientation on x/y/z axis. Defined by the angle of alpha/beta/gamma. Values are in degree or percent.
(Notice: console recording and demo above are not synced)
Default classNames
If no custom css class-name is defined. Tracky creates a unique name by the given values by default. Note that the %-digit will be replaced by 'pc' (percent).
// value: [value]optionsclassPrefix'orientation'axis='alpha | beta | gamma'direction='(empty) | left | right | up | down'valueoptionsclassSuffix
// value: 50tracky-orientation-alpha-50 // when alpha > valuetracky-orientation-alpha-left-50 // when alpha > value && direction === lefttracky-orientation-alpha-right-50 // when alpha > value && direction === righttracky-orientation-beta-50 // when alpha > valuetracky-orientation-beta-up-50 // when beta > value && direction === upwardstracky-orientation-beta-down-50 // when beta > value && direction === downwardstracky-orientation-gamma-50 // when gamma > valuetracky-orientation-gamma-left-50 // when gamma > value && direction === lefttracky-orientation-gamma-right-50 // when gamma > value && direction === right
// value: [50,'25%',10]tracky-orientation-alpha-50 // when alpha > valuetracky-orientation-alpha-left-50 // when alpha > value && direction === lefttracky-orientation-alpha-right-50 // when alpha > value && direction === righttracky-orientation-beta-25pc // when alpha > valuetracky-orientation-beta-up-25pc // when beta > value && direction === upwardstracky-orientation-beta-down-25pc // when beta > value && direction === downwardstracky-orientation-gamma-10 // when gamma > valuetracky-orientation-gamma-left-10 // when gamma > value && direction === lefttracky-orientation-gamma-right-10 // when gamma > value && direction === right
Example configuration object
events: orientation: /* Enable the orientation tracker */ enable: true breakpoints: /* breakpoint as number will apply this value for alpha/beta/gamma */ 50 /* breakpoint as array with a length of 3 will deconstruct the values to alpha[0]/beta[1]/gamma[2] */ 18 55 66 /* breakpoint as percent string will apply this value for alpha/beta/gamma - consider: */ /* alpha 100% = 360deg */ /* beta 100% = 180deg */ /* gamma 100% = 90deg */ '30%' /* breakpoint values can be set as object with property alpha/beta/gamma */ value: alpha: 44 beta: 88 gamma: '66%' /* breakpoint as complex object */ css: /* css class applied when alpha > value.alpha */ alpha: 'edge-tracker-alpha' /* css class applied when beta > value.beta */ beta: 'edge-tracker-beta' /* css class applied when gamma > value.gamma */ gamma: 'edge-tracker-gamma' /* css class applied when alpha direction is heading left */ alphaLeft: 'edge-tracker-alpha-left' /* css class applied when alpha direction is heading right */ alphaRight: 'edge-tracker-alpha-right' /* css class applied when beta direction is heading downwards */ betaDown: 'edge-tracker-beta-down' /* css class applied when beta direction is heading upwards */ betaUp: 'edge-tracker-beta-up' /* css class applied when gamma direction is heading right */ gammaRight: 'edge-tracker-gamma-right' /* css class applied when gamma direction is heading left */ gammaLeft: 'edge-tracker-gamma-left' /* values set as array (alpha/beta/gamma) */ value: '15%' 66 99 /* callback when any value matches the condition */ { console; } /* callback when any value left condition */ { console; } /* callback when alpha value match the condition */ { console; } /* callback when beta value match the condition */ { console; } /* callback when gamma value match the condition */ { console; } /* callback when alpha value left the condition */ { console; } /* callback when beta value left the condition */ { console; } /* callback when gamma value left the condition */ { console; } /* callback when alpha value match the condition and is heading left */ { console; } /* callback when alpha value match the condition and is heading right */ { console; } /* callback when beta value match the condition and is heading upwards */ { console; } /* callback when beta value match the condition and is heading downwards */ { console; } /* callback when gamma value match the condition and is heading left */ { console; } /* callback when gamma value match the condition and is heading right */ { console; } /* callback when condition left -> alpha heading left */ { console; } /* callback when condition left -> alpha heading right */ { console; } /* callback when condition left -> beta heading up */ { console; } /* callback when condition left -> beta heading down */ { console; } /* callback when condition left -> gamma heading left */ { console; } /* callback when condition left -> gamma heading right */ { console; } /* small configuration with css as string applied for any match */ css: 'hit-any-orientation' value: 40 /* value defined as object with only alpha and gamma defined */ value: alpha: 13 gamma: 88
Features
Scroll Tracker
Tracking a scroll event on defined breakpoints or within a certain range. Including horizontal and vertical scroll.
Cursor-Edge Tracker
Tracks if a the cursor touches the edge of an element within a defined range.
Device orientation Tracker
Detects device orientation tilting on x/y/z-axis.
Live bindings
DOM-changes are on track and will bind and unbind events on a selector when the elements appears or removes. This makes it easy to use in Angular, React or Ionic.
Debounced events
Tracker events are debounced by default to avoid unnecessary DOM-rendering.
ES2015 (ES6)-Modules
You can easily import the Tracky-Module with ES2015 module-loaders. It also have a backward compatible version for ES5-Support.
Vanilla JS
Written in plain Javascript without the need for jQuery or other libraries.