- 相關(guān)推薦
Javascript高級手勢使用介紹
在IE10中新加入的對高級用戶(hù)輸入的識別支持,舉例說(shuō)明:注冊一個(gè)點(diǎn)擊操作,通過(guò)一句addEventListener 就能夠知道當前用戶(hù)的點(diǎn)擊是哪種設備,是手指的點(diǎn)擊,是鼠標的單擊還是觸控筆的點(diǎn)擊(平板設備都會(huì )帶有觸控筆)。
MyCanvas.addEventListener("MSPointerDown",MyBack,false);
functionMyBack(e){
alert(e.pointerType.toString());
}
以上這段代碼就是能夠識別出當前用戶(hù)的點(diǎn)擊是哪種設備,通過(guò)回調的方法中 e.pointerType 還進(jìn)行判斷。鼠標是4,觸控筆是3,手指是2。至于值為1是何種設備還有待研究。
還有需要注意的就是 想在javascript中添加對輸入設備的識別,注冊的方法事件也是有點(diǎn)點(diǎn)區別。
addEventListener 添加的事件為 MSPointerDown
而在IE10中對于這樣的多種設備識別中優(yōu)先處理的手指的點(diǎn)擊,前提是不影響功能正常單擊的情況下。然而IE10不僅僅能識別用戶(hù)的輸入設備還支持非常多的高級手勢
以下為IE10高級手勢支持的演示
創(chuàng )建手勢對象
在您的網(wǎng)站中處理手勢的第一步是實(shí)例化手勢對象。
var myGesture = new MSGesture();
接下來(lái),為該手勢提供一個(gè)目標元素。瀏覽器將對該元素觸發(fā)手勢事件。同時(shí),該元素還可以確定事件的坐標空間。
elm = document.getElementById("someElement");
myGesture.target = elm;
elm.addEventListener("MSGestureChange", handleGesture);
最后,告知手勢對象在手勢識別期間處理哪些指針。
elm.addEventListener("MSPointerDown", function (evt) {
// adds the current mouse, pen, or touch contact for gesture recognition
myGesture.addPointer(evt.pointerId);
});
注意:請不要忘記您需要使用 –ms-touch-action 來(lái)配置元素以防止其執行默認觸摸操作(例如,平移和縮放),并為輸入提供指針事件。
處理手勢事件
一旦手勢對象具有有效目標并至少添加了一個(gè)指針,則其將開(kāi)始觸發(fā)手勢事件。手勢事件可分為兩種:靜態(tài)手勢(例如,點(diǎn)擊或保持)和動(dòng)態(tài)手勢(例如,收縮、旋轉和輕掃)。
點(diǎn)擊
最基本的手勢識別是點(diǎn)擊。當檢測到點(diǎn)擊時(shí),將會(huì )在手勢對象的目標元素觸發(fā) MSGestureTap 事件。不同于單擊事件,點(diǎn)擊手勢只能在用戶(hù)觸摸、按鼠標按鈕或使用手寫(xiě)筆觸控而不移動(dòng)時(shí)觸發(fā)。如果您要區分用戶(hù)點(diǎn)擊元素和拖動(dòng)元素的操作,這一點(diǎn)通常會(huì )顯得十分有用。
長(cháng)按
長(cháng)按手勢是指用戶(hù)使用一個(gè)手指觸摸屏幕,并保持片刻并抬起而不移動(dòng)的操作。在長(cháng)按交互期間,MSGestureHold 事件會(huì )針對手勢的各種狀態(tài)而多次觸發(fā):
element.addEventListener("MSGestureHold", handleHold);
function handleHold(evt) {
if (evt.detail & evt.MSGESTURE_FLAG_BEGIN) {
// Begin signals the start of a gesture. For the Hold gesture, this means the user has been holding long enough in place that the gesture will become a complete press & hold if the finger is lifted.
}
if (evt.detail & evt.MSGESTURE_FLAG_END) {
// End signals the end of the gesture.
}
if (evt.detail & evt.MSGESTURE_FLAG_CANCEL) {
// Cancel signals the user started the gesture but cancelled it. For hold, this occurs when the user drags away before lifting. This flag is sent together with the End flag, signaling the gesture recognition is complete.
}
}
動(dòng)態(tài)手勢(收縮、旋轉、輕掃和拖動(dòng))
動(dòng)態(tài)手勢(例如,收縮或旋轉)將以轉換的形式報告,這與 CSS 2D 轉換頗為類(lèi)似。動(dòng)態(tài)手勢可觸發(fā)三種事件:MSGestureStart、MSGestureChange(隨著(zhù)手勢的持續而重復觸發(fā))和 MSGestureEnd。每個(gè)事件都包含縮放(收縮)、旋轉、轉換和速度等相關(guān)信息。
由于動(dòng)態(tài)手勢以轉換的形式報告,因此使用包含 CSS 2D 轉換的 MSGesture 來(lái)操作諸如照片或拼圖等元素將變得十分輕松。例如,您可以通過(guò)下列方式啟用縮放、旋轉和拖動(dòng)元素的操作:
targetElement.addEventListener("MSGestureChange", manipulateElement);
function manipulateElement(e) {
// Uncomment the following code if you want to disable the built-in inertia provided by dynamic gesture recognition
// if (e.detail == e.MSGESTURE_FLAG_INERTIA)
// return;
var m = new MSCSSMatrix(e.target.style.transform); // Get the latest CSS transform on the element
e.target.style.transform = m
.translate(e.offsetX, e.offsetY) // Move the transform origin under the center of the gesture
.rotate(e.rotation * 180 / Math.PI) // Apply Rotation
.scale(e.scale) // Apply Scale
.translate(e.translationX, e.translationY) // Apply Translation
.translate(-e.offsetX, -e.offsetY); // Move the transform origin back
}
縮放和旋轉等動(dòng)態(tài)手勢可支持鼠標操作,具體可通過(guò)在旋轉鼠標滾輪的同時(shí)分別使用 CTRL 或 SHIFT 修飾鍵來(lái)實(shí)現。
【Javascript高級手勢使用介紹】相關(guān)文章:
詳解JavaScript中的splice()使用方法08-20
javascript中for/in循環(huán)以及常見(jiàn)的使用技巧06-24
JavaScript高級程序設計:本地對象Array10-22
JavaScript數組常用方法介紹09-04
javascript克隆對象深度介紹07-25
Javascript中arguments對象的詳解和使用方法08-20
Word使用高級技巧大全07-19
excel高級篩選的使用方法11-25