Skip to content

Instantly share code, notes, and snippets.

@xuanfeng
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuanfeng/d0113546e80cb54f01d8 to your computer and use it in GitHub Desktop.
Save xuanfeng/d0113546e80cb54f01d8 to your computer and use it in GitHub Desktop.
JS逼格(实用)代码-移动端
// 1. 设置变量为undefined
var UNDEFINED = void 0;
// 2. 判断支不支持touch事件
var IsTouch = 'ontouchend' in window;
// 3. 各端事件兼容
START_EVENT = IsTouch ? 'touchstart' : 'mousedown';
MOVE_EVENT = IsTouch ? 'touchmove' : 'mousemove';
END_EVENT = IsTouch ? 'touchend' : 'mouseup';
// 4. 判断数字正则(正数、负数、浮点数)
var NUMBER_REG = /\-?[0-9]+\.?[0-9]*/g;
// 5. CSS3私有属性
var CSS_PREFIX_MAP = ["webkit", "moz", "ms", "o", ""];
// 6. 移动端event
var e, touches;
touches = event.touches && (event.touches.length ? event.touches : [event]);
e = (event.changedTouches && event.changedTouches[0]) || (event.originalEvent && event.originalEvent.changedTouches && event.originalEvent.changedTouches[0]) || touches[0].originalEvent || touches[0];
e.clientX // X坐标
e.clientX // Y坐标
// 7. 空函数
var loop = function() {}
// 8. 自执行匿名函数(严格模式)
;(function(win, doc, $){
// code
})(window, document, window.Zepto || window.jQuery);
// 9. 判断IE10、11
var browser = {
ie10: window.navigator.msPointerEnabled,
ie11: window.navigator.pointerEnabled
}
// 10. 检测touch事件
// 检测
var support = {
// 触摸
touch : (win.Modernizr && Modernizr.touch === true) || (function () {
return !!(('ontouchstart' in win) || win.DocumentTouch && document instanceof DocumentTouch);
})()
};
// 11.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment