Commit d761bdc5 authored by Nature's avatar Nature

添加键盘滚动

parent 8b4d3638
;(function () { ;(function () {
'use strict'; 'use strict'
/** /**
* @preserve FastClick: polyfill to remove click delays on browsers with touch UIs. * @preserve FastClick: polyfill to remove click delays on browsers with touch UIs.
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
*/ */
/*jslint browser:true, node:true*/ /*jslint browser:true, node:true*/
/*global define, Event, Node*/
/*global define, Event, Node*/
/** /**
* Instantiate fast-clicking listeners on the specified layer. * Instantiate fast-clicking listeners on the specified layer.
...@@ -20,142 +20,134 @@ ...@@ -20,142 +20,134 @@
* @param {Element} layer The layer to listen on * @param {Element} layer The layer to listen on
* @param {Object} [options={}] The options to override the defaults * @param {Object} [options={}] The options to override the defaults
*/ */
function FastClick(layer, options) { function FastClick (layer, options) {
var oldOnClick; var oldOnClick
options = options || {}; options = options || {}
/** /**
* Whether a click is currently being tracked. * Whether a click is currently being tracked.
* *
* @type boolean * @type boolean
*/ */
this.trackingClick = false; this.trackingClick = false
/** /**
* Timestamp for when click tracking started. * Timestamp for when click tracking started.
* *
* @type number * @type number
*/ */
this.trackingClickStart = 0; this.trackingClickStart = 0
/** /**
* The element being tracked for a click. * The element being tracked for a click.
* *
* @type EventTarget * @type EventTarget
*/ */
this.targetElement = null; this.targetElement = null
/** /**
* X-coordinate of touch start event. * X-coordinate of touch start event.
* *
* @type number * @type number
*/ */
this.touchStartX = 0; this.touchStartX = 0
/** /**
* Y-coordinate of touch start event. * Y-coordinate of touch start event.
* *
* @type number * @type number
*/ */
this.touchStartY = 0; this.touchStartY = 0
/** /**
* ID of the last touch, retrieved from Touch.identifier. * ID of the last touch, retrieved from Touch.identifier.
* *
* @type number * @type number
*/ */
this.lastTouchIdentifier = 0; this.lastTouchIdentifier = 0
/** /**
* Touchmove boundary, beyond which a click will be cancelled. * Touchmove boundary, beyond which a click will be cancelled.
* *
* @type number * @type number
*/ */
this.touchBoundary = options.touchBoundary || 10; this.touchBoundary = options.touchBoundary || 10
/** /**
* The FastClick layer. * The FastClick layer.
* *
* @type Element * @type Element
*/ */
this.layer = layer; this.layer = layer
/** /**
* The minimum time between tap(touchstart and touchend) events * The minimum time between tap(touchstart and touchend) events
* *
* @type number * @type number
*/ */
this.tapDelay = options.tapDelay || 200; this.tapDelay = options.tapDelay || 200
/** /**
* The maximum time for a tap * The maximum time for a tap
* *
* @type number * @type number
*/ */
this.tapTimeout = options.tapTimeout || 700; this.tapTimeout = options.tapTimeout || 700
if (FastClick.notNeeded(layer)) { if (FastClick.notNeeded(layer)) {
return; return
} }
// Some old versions of Android don't have Function.prototype.bind // Some old versions of Android don't have Function.prototype.bind
function bind(method, context) { function bind (method, context) {
return function() { return method.apply(context, arguments); }; return function () { return method.apply(context, arguments) }
} }
var methods = ['onMouse', 'onClick', 'onTouchStart', 'onTouchMove', 'onTouchEnd', 'onTouchCancel']
var methods = ['onMouse', 'onClick', 'onTouchStart', 'onTouchMove', 'onTouchEnd', 'onTouchCancel']; var context = this
var context = this;
for (var i = 0, l = methods.length; i < l; i++) { for (var i = 0, l = methods.length; i < l; i++) {
context[methods[i]] = bind(context[methods[i]], context); context[methods[i]] = bind(context[methods[i]], context)
} }
// Set up event handlers as required // Set up event handlers as required
if (deviceIsAndroid) { if (deviceIsAndroid) {
layer.addEventListener('mouseover', this.onMouse, true); layer.addEventListener('mouseover', this.onMouse, true)
layer.addEventListener('mousedown', this.onMouse, true); layer.addEventListener('mousedown', this.onMouse, true)
layer.addEventListener('mouseup', this.onMouse, true); layer.addEventListener('mouseup', this.onMouse, true)
} }
layer.addEventListener('click', this.onClick, true); layer.addEventListener('click', this.onClick, true)
layer.addEventListener('touchstart', this.onTouchStart, false); layer.addEventListener('touchstart', this.onTouchStart, false)
layer.addEventListener('touchmove', this.onTouchMove, false); layer.addEventListener('touchmove', this.onTouchMove, false)
layer.addEventListener('touchend', this.onTouchEnd, false); layer.addEventListener('touchend', this.onTouchEnd, false)
layer.addEventListener('touchcancel', this.onTouchCancel, false); layer.addEventListener('touchcancel', this.onTouchCancel, false)
// Hack is required for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2) // Hack is required for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2)
// which is how FastClick normally stops click events bubbling to callbacks registered on the FastClick // which is how FastClick normally stops click events bubbling to callbacks registered on the FastClick
// layer when they are cancelled. // layer when they are cancelled.
if (!Event.prototype.stopImmediatePropagation) { if (!Event.prototype.stopImmediatePropagation) {
layer.removeEventListener = function(type, callback, capture) { layer.removeEventListener = function (type, callback, capture) {
var rmv = Node.prototype.removeEventListener; var rmv = Node.prototype.removeEventListener
if (type === 'click') { if (type === 'click') {
rmv.call(layer, type, callback.hijacked || callback, capture); rmv.call(layer, type, callback.hijacked || callback, capture)
} else { } else {
rmv.call(layer, type, callback, capture); rmv.call(layer, type, callback, capture)
}
} }
};
layer.addEventListener = function(type, callback, capture) { layer.addEventListener = function (type, callback, capture) {
var adv = Node.prototype.addEventListener; var adv = Node.prototype.addEventListener
if (type === 'click') { if (type === 'click') {
adv.call(layer, type, callback.hijacked || (callback.hijacked = function(event) { adv.call(layer, type, callback.hijacked || (callback.hijacked = function (event) {
if (!event.propagationStopped) { if (!event.propagationStopped) {
callback(event); callback(event)
} }
}), capture); }), capture)
} else { } else {
adv.call(layer, type, callback, capture); adv.call(layer, type, callback, capture)
}
} }
};
} }
// If a handler is already declared in the element's onclick attribute, it will be fired before // If a handler is already declared in the element's onclick attribute, it will be fired before
...@@ -165,11 +157,11 @@ ...@@ -165,11 +157,11 @@
// Android browser on at least 3.2 requires a new reference to the function in layer.onclick // Android browser on at least 3.2 requires a new reference to the function in layer.onclick
// - the old one won't work if passed to addEventListener directly. // - the old one won't work if passed to addEventListener directly.
oldOnClick = layer.onclick; oldOnClick = layer.onclick
layer.addEventListener('click', function(event) { layer.addEventListener('click', function (event) {
oldOnClick(event); oldOnClick(event)
}, false); }, false)
layer.onclick = null; layer.onclick = null
} }
} }
...@@ -178,53 +170,71 @@ ...@@ -178,53 +170,71 @@
* *
* @type boolean * @type boolean
*/ */
var deviceIsWindowsPhone = navigator.userAgent.indexOf("Windows Phone") >= 0; var deviceIsWindowsPhone = navigator.userAgent.indexOf('Windows Phone') >= 0
/** /**
* Android requires exceptions. * Android requires exceptions.
* *
* @type boolean * @type boolean
*/ */
var deviceIsAndroid = navigator.userAgent.indexOf('Android') > 0 && !deviceIsWindowsPhone; var deviceIsAndroid = navigator.userAgent.indexOf('Android') > 0 && !deviceIsWindowsPhone
/** /**
* iOS requires exceptions. * iOS requires exceptions.
* *
* @type boolean * @type boolean
*/ */
var deviceIsIOS = /iP(ad|hone|od)/.test(navigator.userAgent) && !deviceIsWindowsPhone; var deviceIsIOS = /iP(ad|hone|od)/.test(navigator.userAgent) && !deviceIsWindowsPhone
/** /**
* iOS 4 requires an exception for select elements. * iOS 4 requires an exception for select elements.
* *
* @type boolean * @type boolean
*/ */
var deviceIsIOS4 = deviceIsIOS && (/OS 4_\d(_\d)?/).test(navigator.userAgent); var deviceIsIOS4 = deviceIsIOS && (/OS 4_\d(_\d)?/).test(navigator.userAgent)
/** /**
* iOS 6.0-7.* requires the target element to be manually derived * iOS 6.0-7.* requires the target element to be manually derived
* *
* @type boolean * @type boolean
*/ */
var deviceIsIOSWithBadTarget = deviceIsIOS && (/OS [6-7]_\d/).test(navigator.userAgent); var deviceIsIOSWithBadTarget = deviceIsIOS && (/OS [6-7]_\d/).test(navigator.userAgent)
/** /**
* BlackBerry requires exceptions. * BlackBerry requires exceptions.
* *
* @type boolean * @type boolean
*/ */
var deviceIsBlackBerry10 = navigator.userAgent.indexOf('BB10') > 0; var deviceIsBlackBerry10 = navigator.userAgent.indexOf('BB10') > 0
var clickElement
/**
* 当前滚动条的滚动高度
* @type {number}
*/
var scrollTop
/**
* 当前content的paddingBottom距离
*/
var contentPaddingBottom
/**
* 是否滚动
* @type {boolean}
*/
var scollFlag = false
var innerHeight
/** /**
* Determine whether a given element requires a native click. * Determine whether a given element requires a native click.
* *
* @param {EventTarget|Element} target Target DOM element * @param {EventTarget|Element} target Target DOM element
* @returns {boolean} Returns true if the element needs a native click * @returns {boolean} Returns true if the element needs a native click
*/ */
FastClick.prototype.needsClick = function(target) { FastClick.prototype.needsClick = function (target) {
switch (target.nodeName.toLowerCase()) { switch (target.nodeName.toLowerCase()) {
// Don't send a synthetic click to disabled inputs (issue #62) // Don't send a synthetic click to disabled inputs (issue #62)
...@@ -232,27 +242,26 @@ ...@@ -232,27 +242,26 @@
case 'select': case 'select':
case 'textarea': case 'textarea':
if (target.disabled) { if (target.disabled) {
return true; return true
} }
break; break
case 'input': case 'input':
// File inputs need real clicks on iOS 6 due to a browser bug (issue #68) // File inputs need real clicks on iOS 6 due to a browser bug (issue #68)
if ((deviceIsIOS && target.type === 'file') || target.disabled) { if ((deviceIsIOS && target.type === 'file') || target.disabled) {
return true; return true
} }
break; break
case 'label': case 'label':
case 'iframe': // iOS8 homescreen apps can prevent events bubbling into frames case 'iframe': // iOS8 homescreen apps can prevent events bubbling into frames
case 'video': case 'video':
return true; return true
} }
return (/\bneedsclick\b/).test(target.className); return (/\bneedsclick\b/).test(target.className)
}; }
/** /**
* Determine whether a given element requires a call to focus to simulate click into element. * Determine whether a given element requires a call to focus to simulate click into element.
...@@ -260,12 +269,12 @@ ...@@ -260,12 +269,12 @@
* @param {EventTarget|Element} target Target DOM element * @param {EventTarget|Element} target Target DOM element
* @returns {boolean} Returns true if the element requires a call to focus to simulate native click. * @returns {boolean} Returns true if the element requires a call to focus to simulate native click.
*/ */
FastClick.prototype.needsFocus = function(target) { FastClick.prototype.needsFocus = function (target) {
switch (target.nodeName.toLowerCase()) { switch (target.nodeName.toLowerCase()) {
case 'textarea': case 'textarea':
return true; return true
case 'select': case 'select':
return !deviceIsAndroid; return !deviceIsAndroid
case 'input': case 'input':
switch (target.type) { switch (target.type) {
case 'button': case 'button':
...@@ -274,16 +283,15 @@ ...@@ -274,16 +283,15 @@
case 'image': case 'image':
case 'radio': case 'radio':
case 'submit': case 'submit':
return false; return false
} }
// No point in attempting to focus disabled inputs // No point in attempting to focus disabled inputs
return !target.disabled && !target.readOnly; return !target.disabled && !target.readOnly
default: default:
return (/\bneedsfocus\b/).test(target.className); return (/\bneedsfocus\b/).test(target.className)
}
} }
};
/** /**
* Send a click event to the specified element. * Send a click event to the specified element.
...@@ -291,101 +299,98 @@ ...@@ -291,101 +299,98 @@
* @param {EventTarget|Element} targetElement * @param {EventTarget|Element} targetElement
* @param {Event} event * @param {Event} event
*/ */
FastClick.prototype.sendClick = function(targetElement, event) { FastClick.prototype.sendClick = function (targetElement, event) {
var clickEvent, touch; var clickEvent, touch
// On some Android devices activeElement needs to be blurred otherwise the synthetic click will have no effect (#24) // On some Android devices activeElement needs to be blurred otherwise the synthetic click will have no effect (#24)
if (document.activeElement && document.activeElement !== targetElement) { if (document.activeElement && document.activeElement !== targetElement) {
document.activeElement.blur(); document.activeElement.blur()
} }
touch = event.changedTouches[0]; touch = event.changedTouches[0]
// Synthesise a click event, with an extra attribute so it can be tracked // Synthesise a click event, with an extra attribute so it can be tracked
clickEvent = document.createEvent('MouseEvents'); clickEvent = document.createEvent('MouseEvents')
clickEvent.initMouseEvent(this.determineEventType(targetElement), true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null); clickEvent.initMouseEvent(this.determineEventType(targetElement), true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null)
clickEvent.forwardedTouchEvent = true; clickEvent.forwardedTouchEvent = true
targetElement.dispatchEvent(clickEvent); targetElement.dispatchEvent(clickEvent)
}; }
FastClick.prototype.determineEventType = function(targetElement) { FastClick.prototype.determineEventType = function (targetElement) {
//Issue #159: Android Chrome Select Box does not open with a synthetic click event //Issue #159: Android Chrome Select Box does not open with a synthetic click event
if (deviceIsAndroid && targetElement.tagName.toLowerCase() === 'select') { if (deviceIsAndroid && targetElement.tagName.toLowerCase() === 'select') {
return 'mousedown'; return 'mousedown'
} }
return 'click'; return 'click'
}; }
/** /**
* @param {EventTarget|Element} targetElement * @param {EventTarget|Element} targetElement
*/ */
FastClick.prototype.focus = function(targetElement) { FastClick.prototype.focus = function (targetElement) {
var length; var length
// Issue #160: on iOS 7, some input elements (e.g. date datetime month) throw a vague TypeError on setSelectionRange. These elements don't have an integer value for the selectionStart and selectionEnd properties, but unfortunately that can't be used for detection because accessing the properties also throws a TypeError. Just check the type instead. Filed as Apple bug #15122724. // Issue #160: on iOS 7, some input elements (e.g. date datetime month) throw a vague TypeError on setSelectionRange. These elements don't have an integer value for the selectionStart and selectionEnd properties, but unfortunately that can't be used for detection because accessing the properties also throws a TypeError. Just check the type instead. Filed as Apple bug #15122724.
var disallowedTypes = ['time', 'month', 'email', 'number']; var disallowedTypes = ['time', 'month', 'email', 'number']
if (deviceIsIOS && if (deviceIsIOS &&
targetElement.setSelectionRange && targetElement.setSelectionRange &&
targetElement.type.indexOf('date') !== 0 && targetElement.type.indexOf('date') !== 0 &&
disallowedTypes.indexOf(targetElement.type) === -1) { disallowedTypes.indexOf(targetElement.type) === -1) {
length = targetElement.value.length; length = targetElement.value.length
targetElement.focus(); targetElement.focus()
targetElement.setSelectionRange(length, length); targetElement.setSelectionRange(length, length)
} else { } else {
targetElement.focus(); targetElement.focus()
}
} }
};
/** /**
* Check whether the given target element is a child of a scrollable layer and if so, set a flag on it. * Check whether the given target element is a child of a scrollable layer and if so, set a flag on it.
* *
* @param {EventTarget|Element} targetElement * @param {EventTarget|Element} targetElement
*/ */
FastClick.prototype.updateScrollParent = function(targetElement) { FastClick.prototype.updateScrollParent = function (targetElement) {
var scrollParent, parentElement; var scrollParent, parentElement
scrollParent = targetElement.fastClickScrollParent;
scrollParent = targetElement.fastClickScrollParent
// Attempt to discover whether the target element is contained within a scrollable layer. Re-check if the // Attempt to discover whether the target element is contained within a scrollable layer. Re-check if the
// target element was moved to another parent. // target element was moved to another parent.
if (!scrollParent || !scrollParent.contains(targetElement)) { if (!scrollParent || !scrollParent.contains(targetElement)) {
parentElement = targetElement; parentElement = targetElement
do { do {
if (parentElement.scrollHeight > parentElement.offsetHeight) { // if (parentElement.scrollHeight > parentElement.offsetHeight) {
scrollParent = parentElement; if (parentElement.classList.contains('content')) {
targetElement.fastClickScrollParent = parentElement; scrollParent = parentElement
break; targetElement.fastClickScrollParent = parentElement
targetElement.fastClickScrollParent.style.paddingBottom = '0px'
targetElement.fastClickScrollParent.style.transform = "translate(0px, 0px) scale(1) translateZ(0px)"
break
} }
parentElement = parentElement.parentElement
parentElement = parentElement.parentElement; } while (parentElement)
} while (parentElement);
} }
// Always update the scroll top tracker if possible. // Always update the scroll top tracker if possible.
if (scrollParent) { if (scrollParent) {
scrollParent.fastClickLastScrollTop = scrollParent.scrollTop; scrollParent.fastClickLastScrollTop = scrollParent.scrollTop
}
} }
};
/** /**
* @param {EventTarget} targetElement * @param {EventTarget} targetElement
* @returns {Element|EventTarget} * @returns {Element|EventTarget}
*/ */
FastClick.prototype.getTargetElementFromEventTarget = function(eventTarget) { FastClick.prototype.getTargetElementFromEventTarget = function (eventTarget) {
// On some older browsers (notably Safari on iOS 4.1 - see issue #56) the event target may be a text node. // On some older browsers (notably Safari on iOS 4.1 - see issue #56) the event target may be a text node.
if (eventTarget.nodeType === Node.TEXT_NODE) { if (eventTarget.nodeType === Node.TEXT_NODE) {
return eventTarget.parentNode; return eventTarget.parentNode
} }
return eventTarget; return eventTarget
}; }
/** /**
* On touch start, record the position and scroll offset. * On touch start, record the position and scroll offset.
...@@ -393,23 +398,23 @@ ...@@ -393,23 +398,23 @@
* @param {Event} event * @param {Event} event
* @returns {boolean} * @returns {boolean}
*/ */
FastClick.prototype.onTouchStart = function(event) { FastClick.prototype.onTouchStart = function (event) {
var targetElement, touch, selection; var targetElement, touch, selection
// Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111). // Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
if (event.targetTouches.length > 1) { if (event.targetTouches.length > 1) {
return true; return true
} }
targetElement = this.getTargetElementFromEventTarget(event.target); targetElement = this.getTargetElementFromEventTarget(event.target)
touch = event.targetTouches[0]; touch = event.targetTouches[0]
if (deviceIsIOS) { if (deviceIsIOS) {
// Only trusted events will deselect text on iOS (issue #49) // Only trusted events will deselect text on iOS (issue #49)
selection = window.getSelection(); selection = window.getSelection()
if (selection.rangeCount && !selection.isCollapsed) { if (selection.rangeCount && !selection.isCollapsed) {
return true; return true
} }
if (!deviceIsIOS4) { if (!deviceIsIOS4) {
...@@ -423,11 +428,11 @@ ...@@ -423,11 +428,11 @@
// which causes all touch events to be ignored. As this block only applies to iOS, and iOS identifiers are always long, // which causes all touch events to be ignored. As this block only applies to iOS, and iOS identifiers are always long,
// random integers, it's safe to to continue if the identifier is 0 here. // random integers, it's safe to to continue if the identifier is 0 here.
if (touch.identifier && touch.identifier === this.lastTouchIdentifier) { if (touch.identifier && touch.identifier === this.lastTouchIdentifier) {
event.preventDefault(); event.preventDefault()
return false; return false
} }
this.lastTouchIdentifier = touch.identifier; this.lastTouchIdentifier = touch.identifier
// If the target element is a child of a scrollable layer (using -webkit-overflow-scrolling: touch) and: // If the target element is a child of a scrollable layer (using -webkit-overflow-scrolling: touch) and:
// 1) the user does a fling scroll on the scrollable layer // 1) the user does a fling scroll on the scrollable layer
...@@ -435,25 +440,24 @@ ...@@ -435,25 +440,24 @@
// then the event.target of the last 'touchend' event will be the element that was under the user's finger // then the event.target of the last 'touchend' event will be the element that was under the user's finger
// when the fling scroll was started, causing FastClick to send a click event to that layer - unless a check // when the fling scroll was started, causing FastClick to send a click event to that layer - unless a check
// is made to ensure that a parent layer was not scrolled before sending a synthetic click (issue #42). // is made to ensure that a parent layer was not scrolled before sending a synthetic click (issue #42).
this.updateScrollParent(targetElement);
} }
} }
this.updateScrollParent(targetElement)
this.trackingClick = true
this.trackingClickStart = event.timeStamp
this.targetElement = targetElement
this.trackingClick = true; this.touchStartX = touch.pageX
this.trackingClickStart = event.timeStamp; this.touchStartY = touch.pageY
this.targetElement = targetElement;
this.touchStartX = touch.pageX;
this.touchStartY = touch.pageY;
// Prevent phantom clicks on fast double-tap (issue #36) // Prevent phantom clicks on fast double-tap (issue #36)
if ((event.timeStamp - this.lastClickTime) < this.tapDelay) { if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
event.preventDefault(); event.preventDefault()
} }
return true; return true
}; }
/** /**
* Based on a touchmove event object, check whether the touch has moved past a boundary since it started. * Based on a touchmove event object, check whether the touch has moved past a boundary since it started.
...@@ -461,16 +465,15 @@ ...@@ -461,16 +465,15 @@
* @param {Event} event * @param {Event} event
* @returns {boolean} * @returns {boolean}
*/ */
FastClick.prototype.touchHasMoved = function(event) { FastClick.prototype.touchHasMoved = function (event) {
var touch = event.changedTouches[0], boundary = this.touchBoundary; var touch = event.changedTouches[0], boundary = this.touchBoundary
if (Math.abs(touch.pageX - this.touchStartX) > boundary || Math.abs(touch.pageY - this.touchStartY) > boundary) { if (Math.abs(touch.pageX - this.touchStartX) > boundary || Math.abs(touch.pageY - this.touchStartY) > boundary) {
return true; return true
} }
return false; return false
}; }
/** /**
* Update the last position. * Update the last position.
...@@ -478,20 +481,19 @@ ...@@ -478,20 +481,19 @@
* @param {Event} event * @param {Event} event
* @returns {boolean} * @returns {boolean}
*/ */
FastClick.prototype.onTouchMove = function(event) { FastClick.prototype.onTouchMove = function (event) {
if (!this.trackingClick) { if (!this.trackingClick) {
return true; return true
} }
// If the touch has moved, cancel the click tracking // If the touch has moved, cancel the click tracking
if (this.targetElement !== this.getTargetElementFromEventTarget(event.target) || this.touchHasMoved(event)) { if (this.targetElement !== this.getTargetElementFromEventTarget(event.target) || this.touchHasMoved(event)) {
this.trackingClick = false; this.trackingClick = false
this.targetElement = null; this.targetElement = null
} }
return true; return true
}; }
/** /**
* Attempt to find the labelled control for the given label element. * Attempt to find the labelled control for the given label element.
...@@ -499,23 +501,22 @@ ...@@ -499,23 +501,22 @@
* @param {EventTarget|HTMLLabelElement} labelElement * @param {EventTarget|HTMLLabelElement} labelElement
* @returns {Element|null} * @returns {Element|null}
*/ */
FastClick.prototype.findControl = function(labelElement) { FastClick.prototype.findControl = function (labelElement) {
// Fast path for newer browsers supporting the HTML5 control attribute // Fast path for newer browsers supporting the HTML5 control attribute
if (labelElement.control !== undefined) { if (labelElement.control !== undefined) {
return labelElement.control; return labelElement.control
} }
// All browsers under test that support touch events also support the HTML5 htmlFor attribute // All browsers under test that support touch events also support the HTML5 htmlFor attribute
if (labelElement.htmlFor) { if (labelElement.htmlFor) {
return document.getElementById(labelElement.htmlFor); return document.getElementById(labelElement.htmlFor)
} }
// If no for attribute exists, attempt to retrieve the first labellable descendant element // If no for attribute exists, attempt to retrieve the first labellable descendant element
// the list of which is defined here: http://www.w3.org/TR/html5/forms.html#category-label // the list of which is defined here: http://www.w3.org/TR/html5/forms.html#category-label
return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea'); return labelElement.querySelector('button, input:not([type=hidden]), keygen, meter, output, progress, select, textarea')
}; }
/** /**
* On touch end, determine whether to send a click event at once. * On touch end, determine whether to send a click event at once.
...@@ -523,108 +524,106 @@ ...@@ -523,108 +524,106 @@
* @param {Event} event * @param {Event} event
* @returns {boolean} * @returns {boolean}
*/ */
FastClick.prototype.onTouchEnd = function(event) { FastClick.prototype.onTouchEnd = function (event) {
var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement; var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement
if (!this.trackingClick) { if (!this.trackingClick) {
return true; return true
} }
// Prevent phantom clicks on fast double-tap (issue #36) // Prevent phantom clicks on fast double-tap (issue #36)
if ((event.timeStamp - this.lastClickTime) < this.tapDelay) { if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
this.cancelNextClick = true; this.cancelNextClick = true
return true; return true
} }
if ((event.timeStamp - this.trackingClickStart) > this.tapTimeout) { if ((event.timeStamp - this.trackingClickStart) > this.tapTimeout) {
return true; return true
} }
// Reset to prevent wrong click cancel on input (issue #156). // Reset to prevent wrong click cancel on input (issue #156).
this.cancelNextClick = false; this.cancelNextClick = false
this.lastClickTime = event.timeStamp; this.lastClickTime = event.timeStamp
trackingClickStart = this.trackingClickStart; trackingClickStart = this.trackingClickStart
this.trackingClick = false; this.trackingClick = false
this.trackingClickStart = 0; this.trackingClickStart = 0
// On some iOS devices, the targetElement supplied with the event is invalid if the layer // On some iOS devices, the targetElement supplied with the event is invalid if the layer
// is performing a transition or scroll, and has to be re-detected manually. Note that // is performing a transition or scroll, and has to be re-detected manually. Note that
// for this to function correctly, it must be called *after* the event target is checked! // for this to function correctly, it must be called *after* the event target is checked!
// See issue #57; also filed as rdar://13048589 . // See issue #57; also filed as rdar://13048589 .
if (deviceIsIOSWithBadTarget) { if (deviceIsIOSWithBadTarget) {
touch = event.changedTouches[0]; touch = event.changedTouches[0]
// In certain cases arguments of elementFromPoint can be negative, so prevent setting targetElement to null // In certain cases arguments of elementFromPoint can be negative, so prevent setting targetElement to null
targetElement = document.elementFromPoint(touch.pageX - window.pageXOffset, touch.pageY - window.pageYOffset) || targetElement; targetElement = document.elementFromPoint(touch.pageX - window.pageXOffset, touch.pageY - window.pageYOffset) || targetElement
targetElement.fastClickScrollParent = this.targetElement.fastClickScrollParent; targetElement.fastClickScrollParent = this.targetElement.fastClickScrollParent
} }
var pointY = touch = event.changedTouches[0].pageY;
targetTagName = targetElement.tagName.toLowerCase(); targetTagName = targetElement.tagName.toLowerCase()
if (targetTagName === 'label') { if (targetTagName === 'label') {
forElement = this.findControl(targetElement); forElement = this.findControl(targetElement)
if (forElement) { if (forElement) {
this.focus(targetElement); scollContent(pointY,targetElement)
this.focus(targetElement)
if (deviceIsAndroid) { if (deviceIsAndroid) {
return false; return false
} }
targetElement = forElement; targetElement = forElement
} }
} else if (this.needsFocus(targetElement)) { } else if (this.needsFocus(targetElement)) {
// Case 1: If the touch started a while ago (best guess is 100ms based on tests for issue #36) then focus will be triggered anyway. Return early and unset the target element reference so that the subsequent click will be allowed through. // Case 1: If the touch started a while ago (best guess is 100ms based on tests for issue #36) then focus will be triggered anyway. Return early and unset the target element reference so that the subsequent click will be allowed through.
// Case 2: Without this exception for input elements tapped when the document is contained in an iframe, then any inputted text won't be visible even though the value attribute is updated as the user types (issue #37). // Case 2: Without this exception for input elements tapped when the document is contained in an iframe, then any inputted text won't be visible even though the value attribute is updated as the user types (issue #37).
if ((event.timeStamp - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) { if ((event.timeStamp - trackingClickStart) > 100 || (deviceIsIOS && window.top !== window && targetTagName === 'input')) {
this.targetElement = null; this.targetElement = null
return false; return false
} }
scollContent(pointY,targetElement)
this.focus(targetElement); this.focus(targetElement)
this.sendClick(targetElement, event); this.sendClick(targetElement, event)
// Select elements need the event to go through on iOS 4, otherwise the selector menu won't open. // Select elements need the event to go through on iOS 4, otherwise the selector menu won't open.
// Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others) // Also this breaks opening selects when VoiceOver is active on iOS6, iOS7 (and possibly others)
if (!deviceIsIOS || targetTagName !== 'select') { if (!deviceIsIOS || targetTagName !== 'select') {
this.targetElement = null; this.targetElement = null
event.preventDefault(); event.preventDefault()
} }
return false; return false
} }
if (deviceIsIOS && !deviceIsIOS4) { if (deviceIsIOS && !deviceIsIOS4) {
// Don't send a synthetic click event if the target element is contained within a parent layer that was scrolled // Don't send a synthetic click event if the target element is contained within a parent layer that was scrolled
// and this tap is being used to stop the scrolling (usually initiated by a fling - issue #42). // and this tap is being used to stop the scrolling (usually initiated by a fling - issue #42).
scrollParent = targetElement.fastClickScrollParent; scrollParent = targetElement.fastClickScrollParent
if (scrollParent && scrollParent.fastClickLastScrollTop !== scrollParent.scrollTop) { if (scrollParent && scrollParent.fastClickLastScrollTop !== scrollParent.scrollTop) {
return true; return true
} }
} }
// Prevent the actual click from going though - unless the target node is marked as requiring // Prevent the actual click from going though - unless the target node is marked as requiring
// real clicks or if it is in the whitelist in which case only non-programmatic clicks are permitted. // real clicks or if it is in the whitelist in which case only non-programmatic clicks are permitted.
if (!this.needsClick(targetElement)) { if (!this.needsClick(targetElement)) {
event.preventDefault(); event.preventDefault()
this.sendClick(targetElement, event); this.sendClick(targetElement, event)
} }
return false; return false
}; }
/** /**
* On touch cancel, stop tracking the click. * On touch cancel, stop tracking the click.
* *
* @returns {void} * @returns {void}
*/ */
FastClick.prototype.onTouchCancel = function() { FastClick.prototype.onTouchCancel = function () {
this.trackingClick = false; this.trackingClick = false
this.targetElement = null; this.targetElement = null
}; }
/** /**
* Determine mouse events which should be permitted. * Determine mouse events which should be permitted.
...@@ -632,20 +631,20 @@ ...@@ -632,20 +631,20 @@
* @param {Event} event * @param {Event} event
* @returns {boolean} * @returns {boolean}
*/ */
FastClick.prototype.onMouse = function(event) { FastClick.prototype.onMouse = function (event) {
// If a target element was never set (because a touch event was never fired) allow the event // If a target element was never set (because a touch event was never fired) allow the event
if (!this.targetElement) { if (!this.targetElement) {
return true; return true
} }
if (event.forwardedTouchEvent) { if (event.forwardedTouchEvent) {
return true; return true
} }
// Programmatically generated events targeting a specific element should be permitted // Programmatically generated events targeting a specific element should be permitted
if (!event.cancelable) { if (!event.cancelable) {
return true; return true
} }
// Derive and check the target element to see whether the mouse event needs to be permitted; // Derive and check the target element to see whether the mouse event needs to be permitted;
...@@ -655,24 +654,23 @@ ...@@ -655,24 +654,23 @@
// Prevent any user-added listeners declared on FastClick element from being fired. // Prevent any user-added listeners declared on FastClick element from being fired.
if (event.stopImmediatePropagation) { if (event.stopImmediatePropagation) {
event.stopImmediatePropagation(); event.stopImmediatePropagation()
} else { } else {
// Part of the hack for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2) // Part of the hack for browsers that don't support Event#stopImmediatePropagation (e.g. Android 2)
event.propagationStopped = true; event.propagationStopped = true
} }
// Cancel the event // Cancel the event
event.stopPropagation(); event.stopPropagation()
event.preventDefault(); event.preventDefault()
return false; return false
} }
// If the mouse event is permitted, return true for the action to go through. // If the mouse event is permitted, return true for the action to go through.
return true; return true
}; }
/** /**
* On actual clicks, determine whether this is a touch-generated click, a click action occurring * On actual clicks, determine whether this is a touch-generated click, a click action occurring
...@@ -682,112 +680,110 @@ ...@@ -682,112 +680,110 @@
* @param {Event} event * @param {Event} event
* @returns {boolean} * @returns {boolean}
*/ */
FastClick.prototype.onClick = function(event) { FastClick.prototype.onClick = function (event) {
var permitted; var permitted
// It's possible for another FastClick-like library delivered with third-party code to fire a click event before FastClick does (issue #44). In that case, set the click-tracking flag back to false and return early. This will cause onTouchEnd to return early. // It's possible for another FastClick-like library delivered with third-party code to fire a click event before FastClick does (issue #44). In that case, set the click-tracking flag back to false and return early. This will cause onTouchEnd to return early.
if (this.trackingClick) { if (this.trackingClick) {
this.targetElement = null; this.targetElement = null
this.trackingClick = false; this.trackingClick = false
return true; return true
} }
// Very odd behaviour on iOS (issue #18): if a submit element is present inside a form and the user hits enter in the iOS simulator or clicks the Go button on the pop-up OS keyboard the a kind of 'fake' click event will be triggered with the submit-type input element as the target. // Very odd behaviour on iOS (issue #18): if a submit element is present inside a form and the user hits enter in the iOS simulator or clicks the Go button on the pop-up OS keyboard the a kind of 'fake' click event will be triggered with the submit-type input element as the target.
if (event.target.type === 'submit' && event.detail === 0) { if (event.target.type === 'submit' && event.detail === 0) {
return true; return true
} }
permitted = this.onMouse(event); permitted = this.onMouse(event)
// Only unset targetElement if the click is not permitted. This will ensure that the check for !targetElement in onMouse fails and the browser's click doesn't go through. // Only unset targetElement if the click is not permitted. This will ensure that the check for !targetElement in onMouse fails and the browser's click doesn't go through.
if (!permitted) { if (!permitted) {
this.targetElement = null; this.targetElement = null
} }
// If clicks are permitted, return true for the action to go through. // If clicks are permitted, return true for the action to go through.
return permitted; return permitted
}; }
/** /**
* Remove all FastClick's event listeners. * Remove all FastClick's event listeners.
* *
* @returns {void} * @returns {void}
*/ */
FastClick.prototype.destroy = function() { FastClick.prototype.destroy = function () {
var layer = this.layer; var layer = this.layer
if (deviceIsAndroid) { if (deviceIsAndroid) {
layer.removeEventListener('mouseover', this.onMouse, true); layer.removeEventListener('mouseover', this.onMouse, true)
layer.removeEventListener('mousedown', this.onMouse, true); layer.removeEventListener('mousedown', this.onMouse, true)
layer.removeEventListener('mouseup', this.onMouse, true); layer.removeEventListener('mouseup', this.onMouse, true)
} }
layer.removeEventListener('click', this.onClick, true); layer.removeEventListener('click', this.onClick, true)
layer.removeEventListener('touchstart', this.onTouchStart, false); layer.removeEventListener('touchstart', this.onTouchStart, false)
layer.removeEventListener('touchmove', this.onTouchMove, false); layer.removeEventListener('touchmove', this.onTouchMove, false)
layer.removeEventListener('touchend', this.onTouchEnd, false); layer.removeEventListener('touchend', this.onTouchEnd, false)
layer.removeEventListener('touchcancel', this.onTouchCancel, false); layer.removeEventListener('touchcancel', this.onTouchCancel, false)
}; }
/** /**
* Check whether FastClick is needed. * Check whether FastClick is needed.
* *
* @param {Element} layer The layer to listen on * @param {Element} layer The layer to listen on
*/ */
FastClick.notNeeded = function(layer) { FastClick.notNeeded = function (layer) {
var metaViewport; var metaViewport
var chromeVersion; var chromeVersion
var blackberryVersion; var blackberryVersion
var firefoxVersion; var firefoxVersion
// Devices that don't support touch don't need FastClick // Devices that don't support touch don't need FastClick
if (typeof window.ontouchstart === 'undefined') { if (typeof window.ontouchstart === 'undefined') {
return true; return true
} }
// Chrome version - zero for other browsers // Chrome version - zero for other browsers
chromeVersion = +(/Chrome\/([0-9]+)/.exec(navigator.userAgent) || [,0])[1]; chromeVersion = +(/Chrome\/([0-9]+)/.exec(navigator.userAgent) || [, 0])[1]
if (chromeVersion) { if (chromeVersion) {
if (deviceIsAndroid) { if (deviceIsAndroid) {
metaViewport = document.querySelector('meta[name=viewport]'); metaViewport = document.querySelector('meta[name=viewport]')
if (metaViewport) { if (metaViewport) {
// Chrome on Android with user-scalable="no" doesn't need FastClick (issue #89) // Chrome on Android with user-scalable="no" doesn't need FastClick (issue #89)
if (metaViewport.content.indexOf('user-scalable=no') !== -1) { if (metaViewport.content.indexOf('user-scalable=no') !== -1) {
return true; return true
} }
// Chrome 32 and above with width=device-width or less don't need FastClick // Chrome 32 and above with width=device-width or less don't need FastClick
if (chromeVersion > 31 && document.documentElement.scrollWidth <= window.outerWidth) { if (chromeVersion > 31 && document.documentElement.scrollWidth <= window.outerWidth) {
return true; return true
} }
} }
// Chrome desktop doesn't need FastClick (issue #15) // Chrome desktop doesn't need FastClick (issue #15)
} else { } else {
return true; return true
} }
} }
if (deviceIsBlackBerry10) { if (deviceIsBlackBerry10) {
blackberryVersion = navigator.userAgent.match(/Version\/([0-9]*)\.([0-9]*)/); blackberryVersion = navigator.userAgent.match(/Version\/([0-9]*)\.([0-9]*)/)
// BlackBerry 10.3+ does not require Fastclick library. // BlackBerry 10.3+ does not require Fastclick library.
// https://github.com/ftlabs/fastclick/issues/251 // https://github.com/ftlabs/fastclick/issues/251
if (blackberryVersion[1] >= 10 && blackberryVersion[2] >= 3) { if (blackberryVersion[1] >= 10 && blackberryVersion[2] >= 3) {
metaViewport = document.querySelector('meta[name=viewport]'); metaViewport = document.querySelector('meta[name=viewport]')
if (metaViewport) { if (metaViewport) {
// user-scalable=no eliminates click delay. // user-scalable=no eliminates click delay.
if (metaViewport.content.indexOf('user-scalable=no') !== -1) { if (metaViewport.content.indexOf('user-scalable=no') !== -1) {
return true; return true
} }
// width=device-width (or less than device-width) eliminates click delay. // width=device-width (or less than device-width) eliminates click delay.
if (document.documentElement.scrollWidth <= window.outerWidth) { if (document.documentElement.scrollWidth <= window.outerWidth) {
return true; return true
} }
} }
} }
...@@ -795,30 +791,76 @@ ...@@ -795,30 +791,76 @@
// IE10 with -ms-touch-action: none or manipulation, which disables double-tap-to-zoom (issue #97) // IE10 with -ms-touch-action: none or manipulation, which disables double-tap-to-zoom (issue #97)
if (layer.style.msTouchAction === 'none' || layer.style.touchAction === 'manipulation') { if (layer.style.msTouchAction === 'none' || layer.style.touchAction === 'manipulation') {
return true; return true
} }
// Firefox version - zero for other browsers // Firefox version - zero for other browsers
firefoxVersion = +(/Firefox\/([0-9]+)/.exec(navigator.userAgent) || [,0])[1]; firefoxVersion = +(/Firefox\/([0-9]+)/.exec(navigator.userAgent) || [, 0])[1]
if (firefoxVersion >= 27) { if (firefoxVersion >= 27) {
// Firefox 27+ does not have tap delay if the content is not zoomable - https://bugzilla.mozilla.org/show_bug.cgi?id=922896 // Firefox 27+ does not have tap delay if the content is not zoomable - https://bugzilla.mozilla.org/show_bug.cgi?id=922896
metaViewport = document.querySelector('meta[name=viewport]'); metaViewport = document.querySelector('meta[name=viewport]')
if (metaViewport && (metaViewport.content.indexOf('user-scalable=no') !== -1 || document.documentElement.scrollWidth <= window.outerWidth)) { if (metaViewport && (metaViewport.content.indexOf('user-scalable=no') !== -1 || document.documentElement.scrollWidth <= window.outerWidth)) {
return true; return true
} }
} }
// IE11: prefixed -ms-touch-action is no longer supported and it's recomended to use non-prefixed version // IE11: prefixed -ms-touch-action is no longer supported and it's recomended to use non-prefixed version
// http://msdn.microsoft.com/en-us/library/windows/apps/Hh767313.aspx // http://msdn.microsoft.com/en-us/library/windows/apps/Hh767313.aspx
if (layer.style.touchAction === 'none' || layer.style.touchAction === 'manipulation') { if (layer.style.touchAction === 'none' || layer.style.touchAction === 'manipulation') {
return true; return true
}
return false
}
window.addEventListener('native.keyboardhide', function (e) {
if(scollFlag){
scollFlag=false
// clickElement.fastClickScrollParent.style.paddingBottom = contentPaddingBottom + 'px'
// clickElement.fastClickScrollParent.scrollTop =scrollTop
targetElement.fastClickScrollParent.style.paddingBottom = '0px'
/*clickElement.fastClickScrollParent.style.webkitTransform = "translate(0px, 0px) scale(1) translateZ(0px)"
clickElement.fastClickScrollParent.style.MozTransform = "translate(0px, 0px) scale(1) translateZ(0px)"
clickElement.fastClickScrollParent.style.msTransform = "translate(0px, 0px) scale(1) translateZ(0px)"
clickElement.fastClickScrollParent.style.OTransform = "translate(0px, 0px) scale(1) translateZ(0px)"*/
clickElement.fastClickScrollParent.style.transform = "translate(0px, 0px) scale(1) translateZ(0px)"
} }
})
return false; function scollContent (pointY,targetElement) {
}; innerHeight = window.innerHeight
var keyBoardHeight = getKeyBoardHeight()
clickElement = targetElement
scrollTop = targetElement.fastClickScrollParent.scrollTop
contentPaddingBottom = Number(targetElement.style.paddingBottom.replace('px', ''))
var scollHeight = (innerHeight - pointY) < keyBoardHeight ? (keyBoardHeight - (innerHeight - pointY)) : 0
if (scollHeight) {
scollFlag = true
targetElement.fastClickScrollParent.style.paddingBottom = (scollHeight+20) + 'px'
// targetElement.fastClickScrollParent.scrollTop= (scrollTop + scollHeight)
targetElement.fastClickScrollParent.style.transition = 'all .2s cubic-bezier(0.165, 0.84, 0.44, 1) 0s';
/*targetElement.fastClickScrollParent.style.webkitTransform = "translate(0px, -"+scollHeight+"px) scale(1) translateZ(0px)"
targetElement.fastClickScrollParent.style.MozTransform = "translate(0px, -"+scollHeight+"px) scale(1) translateZ(0px)"
targetElement.fastClickScrollParent.style.msTransform = "translate(0px, -"+scollHeight+"px) scale(1) translateZ(0px)"
targetElement.fastClickScrollParent.style.OTransform = "translate(0px, -"+scollHeight+"px) scale(1) translateZ(0px)"*/
targetElement.fastClickScrollParent.style.transform = "translate(0px, -"+scollHeight+"px) scale(1) translateZ(0px)"
}
}
function getKeyBoardHeight () {
var innerWidth = window.innerWidth
if (deviceIsIOS) {
if (innerWidth >= 375 && innerHeight >= 812) {
return 400
}
return 370
} else {
return 275
}
}
/** /**
* Factory method for creating a FastClick object * Factory method for creating a FastClick object
...@@ -826,21 +868,20 @@ ...@@ -826,21 +868,20 @@
* @param {Element} layer The layer to listen on * @param {Element} layer The layer to listen on
* @param {Object} [options={}] The options to override the defaults * @param {Object} [options={}] The options to override the defaults
*/ */
FastClick.attach = function(layer, options) { FastClick.attach = function (layer, options) {
return new FastClick(layer, options); return new FastClick(layer, options)
}; }
if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) { if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
// AMD. Register as an anonymous module. // AMD. Register as an anonymous module.
define(function() { define(function () {
return FastClick; return FastClick
}); })
} else if (typeof module !== 'undefined' && module.exports) { } else if (typeof module !== 'undefined' && module.exports) {
module.exports = FastClick.attach; module.exports = FastClick.attach
module.exports.FastClick = FastClick; module.exports.FastClick = FastClick
} else { } else {
window.FastClick = FastClick; window.FastClick = FastClick
} }
}()); }())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment