Skip to content

Commit

Permalink
✨ If a scrolling event occurs, the tap is not triggered.
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Nov 23, 2016
1 parent e6d80eb commit 84fa7de
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bower.json
Expand Up @@ -25,7 +25,7 @@
"example",
"tools"
],
"version": "0.0.3",
"version": "0.0.4",
"keywords": [
"event",
"tap",
Expand Down
43 changes: 41 additions & 2 deletions h5tap.js
Expand Up @@ -5,8 +5,8 @@
* Tap event trigger of mobile
* @author
* zswang (http://weibo.com/zswang)
* @version 0.0.3
* @date 2016-11-22
* @version 0.0.4
* @date 2016-11-23
*/
/*<function name="h5tap">*/
/**
Expand Down Expand Up @@ -171,6 +171,40 @@
// * done
}, 5)
```
* @example h5tap():Scroll event
```html
<div>
<button cmd="ok"></button>
</div>
```
```js
var count = 0;
h5tap('div', '[cmd]', function (target) {
count++;
});
var point = [100, 100];
var target = document.querySelector('[cmd="ok"]');
// ------ touchstart ------
var e = document.createEvent('UIEvent');
var point = [100, 100];
e.touches = [{pageX: point[0], pageY: point[1], clientX: point[0], clientY: point[1]}];
e.initUIEvent('touchstart', true, true, window, 1);
target.dispatchEvent(e);
// ------ scroll ------
var e = document.createEvent('UIEvent');
e.initUIEvent('scroll', true, true, window, 1);
window.dispatchEvent(e);
// ------ touchend ------
var e = document.createEvent('UIEvent');
e.changedTouches = [{pageX: point[0], pageY: point[1], clientX: point[0], clientY: point[1]}];
e.initUIEvent('touchend', true, true, window, 1);
target.dispatchEvent(e);
setTimeout(function () {
console.log(count);
// > 0
// * done
}, 5)
```
*/
function h5tap(parent, selector, callback) {
if (typeof parent === 'string') {
Expand Down Expand Up @@ -224,6 +258,11 @@
// 兼容 PC 端
parent.addEventListener('mousedown', startHandler, false);
document.addEventListener('mouseup', endHandler, false);
// 发生滚动则不触发
window.addEventListener('scroll', function() {
startPoint = null;
target = null;
});
}
/*</function>*/
exports = h5tap;
Expand Down
2 changes: 1 addition & 1 deletion h5tap.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "h5tap",
"version": "0.0.3",
"version": "0.0.4",
"main": "h5tap.js",
"description": "Tap event trigger of mobile",
"repository": {
Expand Down
45 changes: 45 additions & 0 deletions src/h5tap.js
Expand Up @@ -207,6 +207,45 @@
// * done
}, 5)
```
* @example h5tap():Scroll event
```html
<div>
<button cmd="ok"></button>
</div>
```
```js
var count = 0;
h5tap('div', '[cmd]', function (target) {
count++;
});
var point = [100, 100];
var target = document.querySelector('[cmd="ok"]');
// ------ touchstart ------
var e = document.createEvent('UIEvent');
var point = [100, 100];
e.touches = [{pageX: point[0], pageY: point[1], clientX: point[0], clientY: point[1]}];
e.initUIEvent('touchstart', true, true, window, 1);
target.dispatchEvent(e);
// ------ scroll ------
var e = document.createEvent('UIEvent');
e.initUIEvent('scroll', true, true, window, 1);
window.dispatchEvent(e);
// ------ touchend ------
var e = document.createEvent('UIEvent');
e.changedTouches = [{pageX: point[0], pageY: point[1], clientX: point[0], clientY: point[1]}];
e.initUIEvent('touchend', true, true, window, 1);
target.dispatchEvent(e);
setTimeout(function () {
console.log(count);
// > 0
// * done
}, 5)
```
*/
function h5tap(parent, selector, callback) {

Expand Down Expand Up @@ -265,6 +304,12 @@
// 兼容 PC 端
parent.addEventListener('mousedown', startHandler, false);
document.addEventListener('mouseup', endHandler, false);

// 发生滚动则不触发
window.addEventListener('scroll', function() {
startPoint = null;
target = null;
});
}
/*</function>*/

Expand Down
55 changes: 55 additions & 0 deletions test/h5tap.js
Expand Up @@ -246,5 +246,60 @@ describe("src/h5tap.js", function () {
}, 5)
});

it("jsdom@h5tap():Scroll event", function (done) {
jsdom.env(" <div>\n <button cmd=\"ok\"></button>\n </div>", {
features: {
FetchExternalResources : ["script", "link"],
ProcessExternalResources: ["script"]
}
},
function (err, window) {
global.window = window;
["document","navigator"].forEach(
function (key) {
global[key] = window[key];
}
);
assert.equal(err, null);
done();
}
);
});

it("h5tap():Scroll event", function (done) {
examplejs_printLines = [];
var count = 0;
h5tap('div', '[cmd]', function (target) {
count++;
});

var point = [100, 100];
var target = document.querySelector('[cmd="ok"]');

// ------ touchstart ------
var e = document.createEvent('UIEvent');
var point = [100, 100];
e.touches = [{pageX: point[0], pageY: point[1], clientX: point[0], clientY: point[1]}];
e.initUIEvent('touchstart', true, true, window, 1);
target.dispatchEvent(e);

// ------ scroll ------
var e = document.createEvent('UIEvent');
e.initUIEvent('scroll', true, true, window, 1);
window.dispatchEvent(e);

// ------ touchend ------
var e = document.createEvent('UIEvent');
e.changedTouches = [{pageX: point[0], pageY: point[1], clientX: point[0], clientY: point[1]}];
e.initUIEvent('touchend', true, true, window, 1);
target.dispatchEvent(e);

setTimeout(function () {
examplejs_print(count);
assert.equal(examplejs_printLines.join("\n"), "0"); examplejs_printLines = [];
done();
}, 5)
});

});

0 comments on commit 84fa7de

Please sign in to comment.