Created
February 6, 2012 07:09
-
-
Save lbj96347/1750374 to your computer and use it in GitHub Desktop.
RemoveDedayOnclick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*when you use an onclick event in an html element,iPhone webkit browser would have a delay.So you should deal with it.This part of js would help you.You just need to add this file in your html page and use ontouchstart/ontouchend/ontouchmove to replace onclick event */ | |
/* 当你在使用onclick事件的时候,iPhone手机上面的safari浏览器会对事件产生一个延误(大概是0.3秒左右),这个时候你必须对这个事件作出一些处理。 然后用ontouchstart ontouchend ontouchmove去代替原本的onclick事件即可*/ | |
/* how to use it? <a onclick="alert('touch me');">Touch Me</a> that's it */ | |
/* 怎么用它,<a ontouchstart="alert('摸我一下');">摸我</a> that's it */ | |
function NoClickDelay(el) { | |
this.element = el; | |
if( window.Touch ) this.element.addEventListener('touchstart', this, false); | |
} | |
NoClickDelay.prototype = { | |
handleEvent: function(e) { | |
switch(e.type) { | |
case 'touchstart': this.onTouchStart(e); break; | |
case 'touchmove': this.onTouchMove(e); break; | |
case 'touchend': this.onTouchEnd(e); break; | |
} | |
}, | |
onTouchStart: function(e) { | |
e.preventDefault(); | |
this.moved = false; | |
this.element.addEventListener('touchmove', this, false); | |
this.element.addEventListener('touchend', this, false); | |
}, | |
onTouchMove: function(e) { | |
this.moved = true; | |
}, | |
onTouchEnd: function(e) { | |
this.element.removeEventListener('touchmove', this, false); | |
this.element.removeEventListener('touchend', this, false); | |
if( !this.moved ) { | |
// Place your code here or use the click simulation below | |
var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY); | |
if(theTarget.nodeType == 3) theTarget = theTarget.parentNode; | |
var theEvent = document.createEvent('MouseEvents'); | |
theEvent.initEvent('click', true, true); | |
theTarget.dispatchEvent(theEvent); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment