Skip to content

Instantly share code, notes, and snippets.

@clohr
Last active February 3, 2017 14:39
Show Gist options
  • Select an option

  • Save clohr/e3ce2e9e5130e41b422d34a09232bf84 to your computer and use it in GitHub Desktop.

Select an option

Save clohr/e3ce2e9e5130e41b422d34a09232bf84 to your computer and use it in GitHub Desktop.
Capture mouse click and touch events
const head = ([h, ...tail]) => h
const defaultTo = (defaultValue, testValue) => Boolean(testValue) ? testValue : defaultValue
const capture = (evt) => {
const eventTarget = evt.target
const touchTarget = evt.touches && evt.touches.length > 0
const xPos = touchTarget ? head(touchTarget).clientX : evt.clientX
const yPos = touchTarget ? head(touchTarget).clientY : evt.clientY
const href = defaultTo(eventTarget.href, eventTarget.parentNode.href)
const id = defaultTo(eventTarget.name, eventTarget.id)
const touchEvent = Boolean(touchTarget)
const data = { xPos, yPos, href, id, touchEvent }
if (data.href) {
console.log('Event data', data)
}
}
const init = () =>
['click', 'touchstart'].forEach(evt =>
document.body.addEventListener('click', capture)
)
export default init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment