swiped-events
Advanced tools
Comparing version 1.1.2 to 1.1.3
/*! | ||
* swiped-events.js - v1.1.2 | ||
* swiped-events.js - v1.1.3 | ||
* Pure JavaScript swipe events | ||
@@ -9,2 +9,2 @@ * https://github.com/john-doherty/swiped-events | ||
*/ | ||
!function(t,e){"use strict";"function"!=typeof t.CustomEvent&&(t.CustomEvent=function(t,n){n=n||{bubbles:!1,cancelable:!1,detail:void 0};var a=e.createEvent("CustomEvent");return a.initCustomEvent(t,n.bubbles,n.cancelable,n.detail),a},t.CustomEvent.prototype=t.Event.prototype),e.addEventListener("touchstart",function(t){if("true"===t.target.getAttribute("data-swipe-ignore"))return;l=t.target,s=Date.now(),n=t.touches[0].clientX,a=t.touches[0].clientY,u=0,i=0},!1),e.addEventListener("touchmove",function(t){if(!n||!a)return;var e=t.touches[0].clientX,s=t.touches[0].clientY;u=n-e,i=a-s},!1),e.addEventListener("touchend",function(t){if(l!==t.target)return;var e=parseInt(l.getAttribute("data-swipe-threshold")||"20",10),r=parseInt(l.getAttribute("data-swipe-timeout")||"500",10),o=Date.now()-s,c="",d=t.changedTouches||t.touches||[];Math.abs(u)>Math.abs(i)?Math.abs(u)>e&&o<r&&(c=u>0?"swiped-left":"swiped-right"):Math.abs(i)>e&&o<r&&(c=i>0?"swiped-up":"swiped-down");if(""!==c){var p={dir:c.replace(/swiped-/,""),xStart:parseInt(n,10),xEnd:parseInt((d[0]||{}).clientX||-1,10),yStart:parseInt(a,10),yEnd:parseInt((d[0]||{}).clientY||-1,10)};l.dispatchEvent(new CustomEvent("swiped",{bubbles:!0,cancelable:!0,detail:p})),l.dispatchEvent(new CustomEvent(c,{bubbles:!0,cancelable:!0,detail:p}))}n=null,a=null,s=null},!1);var n=null,a=null,u=null,i=null,s=null,l=null}(window,document); | ||
!function(t,e){"use strict";"function"!=typeof t.CustomEvent&&(t.CustomEvent=function(t,n){n=n||{bubbles:!1,cancelable:!1,detail:void 0};var a=e.createEvent("CustomEvent");return a.initCustomEvent(t,n.bubbles,n.cancelable,n.detail),a},t.CustomEvent.prototype=t.Event.prototype),e.addEventListener("touchstart",function(t){if("true"===t.target.getAttribute("data-swipe-ignore"))return;s=t.target,r=Date.now(),n=t.touches[0].clientX,a=t.touches[0].clientY,u=0,i=0},!1),e.addEventListener("touchmove",function(t){if(!n||!a)return;var e=t.touches[0].clientX,r=t.touches[0].clientY;u=n-e,i=a-r},!1),e.addEventListener("touchend",function(t){if(s!==t.target)return;var e=parseInt(l(s,"data-swipe-threshold","20"),10),o=parseInt(l(s,"data-swipe-timeout","500"),10),c=Date.now()-r,d="",p=t.changedTouches||t.touches||[];Math.abs(u)>Math.abs(i)?Math.abs(u)>e&&c<o&&(d=u>0?"swiped-left":"swiped-right"):Math.abs(i)>e&&c<o&&(d=i>0?"swiped-up":"swiped-down");if(""!==d){var b={dir:d.replace(/swiped-/,""),xStart:parseInt(n,10),xEnd:parseInt((p[0]||{}).clientX||-1,10),yStart:parseInt(a,10),yEnd:parseInt((p[0]||{}).clientY||-1,10)};s.dispatchEvent(new CustomEvent("swiped",{bubbles:!0,cancelable:!0,detail:b})),s.dispatchEvent(new CustomEvent(d,{bubbles:!0,cancelable:!0,detail:b}))}n=null,a=null,r=null},!1);var n=null,a=null,u=null,i=null,r=null,s=null;function l(t,n,a){for(;t&&t!==e.documentElement;){var u=t.getAttribute(n);if(u)return u;t=t.parentNode}return a}}(window,document); |
{ | ||
"name": "swiped-events", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "A 0.7k script that adds swipe events to the DOM for touch enabled devices", | ||
@@ -5,0 +5,0 @@ "main": "src/swiped-events.js", |
@@ -11,2 +11,11 @@ # swiped-events | ||
### swiped | ||
```js | ||
document.addEventListener('swiped', function(e) { | ||
console.log(e.target); // element that was swiped | ||
console.log(e.detail.dir); // swipe direction | ||
}); | ||
``` | ||
### swiped-left | ||
@@ -48,12 +57,2 @@ | ||
### swiped | ||
```js | ||
document.addEventListener('swiped', function(e) { | ||
console.log(e.target); // element that was swiped | ||
console.log(e.detail); // event data { dir: 'down', xStart: 196, xEnd: 230, yStart: 196, yEnd: 4 } | ||
console.log(e.detail.dir); // swipe direction | ||
}); | ||
``` | ||
### Configure | ||
@@ -79,2 +78,11 @@ | ||
To set defaults application wide, set config attributes on a parent/topmost element: | ||
```html | ||
<body data-swipe-threshold="100" data-swipe-timeout="250"> | ||
<div>Swipe me</div> | ||
<div>or me</div> | ||
</body> | ||
``` | ||
## Contributing | ||
@@ -81,0 +89,0 @@ |
@@ -49,4 +49,4 @@ /*! | ||
var swipeThreshold = parseInt(startEl.getAttribute('data-swipe-threshold') || '20', 10); // default 20px | ||
var swipeTimeout = parseInt(startEl.getAttribute('data-swipe-timeout') || '500', 10); // default 500ms | ||
var swipeThreshold = parseInt(getNearestAttribute(startEl, 'data-swipe-threshold', '20'), 10); // default 20px | ||
var swipeTimeout = parseInt(getNearestAttribute(startEl, 'data-swipe-timeout', '500'), 10); // default 500ms | ||
var timeDiff = Date.now() - timeDown; | ||
@@ -133,2 +133,26 @@ var eventType = ''; | ||
/** | ||
* Gets attribute off HTML element or nearest parent | ||
* @param {object} el - HTML element to retrieve attribute from | ||
* @param {string} attributeName - name of the attribute | ||
* @param {any} defaultValue - default value to return if no match found | ||
* @returns {any} attribute value or defaultValue | ||
*/ | ||
function getNearestAttribute(el, attributeName, defaultValue) { | ||
// walk up the dom tree looking for data-action and data-trigger | ||
while (el && el !== document.documentElement) { | ||
var attributeValue = el.getAttribute(attributeName); | ||
if (attributeValue) { | ||
return attributeValue; | ||
} | ||
el = el.parentNode; | ||
} | ||
return defaultValue; | ||
} | ||
}(window, document)); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
223327
181
124