由於移動端是觸摸事件,所以要用到H5的屬性touchstart/touchmove/touched,但是PC端只支持鼠標事件,所以此時可以這樣轉換 var touchEvents = { touchstart:"touchstart", touchmove:"touchmove", touchend:"touchend", initTouchEvents:function () {
var self = this; if (self.isPC()) { self.touchstart = "mousedown"; self.touchmove = "mousemove"; self.touchend = "mouseup"; } },
isPC:function(){ //判斷pc端與移動端
var userAgentInfo = navigator.userAgent;
var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"); //判斷用戶代理頭信息
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) != -1) { flag = false; break; }
}
return flag; //true為pc端,false為非pc端
}
};