因爲移動端是觸摸事件,因此要用到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端
} };