1. 获取 URL 参数
function getParameterByName(name) {
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
      results = regex.exec(location.search);
  return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
  1. 检查是否为数组
function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]';
}
  1. 检查是否为对象
function isObject(obj) {
  return Object.prototype.toString.call(obj) === '[object Object]';
}
  1. 检查是否为函数
function isFunction(obj) {
  return typeof obj === 'function';
}
  1. 检查是否为空
function isEmpty(obj) {
  if (obj === null || obj === undefined) return true;
  if (isArray(obj) || typeof obj === 'string') return obj.length === 0;
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) return false;
  }
  return true;
}
  1. 检查是否为数字
function isNumber(obj) {
  return typeof obj === 'number' && !isNaN(obj);
}
  1. 检查是否为字符串
function isString(obj) {
  return typeof obj === 'string';
}
  1. 获取元素的位置
function getOffset(el) {
  var rect = el.getBoundingClientRect(),
      scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
      scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  return { top: rect.top + scrollTop, left: rect.left + scrollLeft };
}
  1. 获取元素的样式
function getStyle(el, styleProp) {
  if (el.currentStyle) {
    return el.currentStyle[styleProp];
  } else if (window.getComputedStyle) {
    return document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
  }
}
  1. 获取滚动条的位置
function getScrollPosition() {
  return {
    x: window.pageXOffset || document.documentElement.scrollLeft,
    y: window.pageYOffset || document.documentElement.scrollTop
  };
}
  1. 滚动到顶部
function scrollToTop() {
  var c = document.documentElement.scrollTop || document.body.scrollTop;
  if (c > 0) {
    window.requestAnimationFrame(scrollToTop);
    window.scrollTo(0, c - c / 8);
  }
}
  1. 检查是否支持触摸事件
function isTouchDevice() {
  return 'ontouchstart' in window || navigator.maxTouchPoints;
}
  1. 获取页面高度
function getPageHeight() {
  return Math.max(
    document.body.scrollHeight, document.documentElement.scrollHeight,
    document.body.offsetHeight, document.documentElement.offsetHeight,
    document.body.clientHeight, document.documentElement.clientHeight
  );
}
  1. 获取页面宽度
function getPageWidth() {
  return Math.max(
    document.body.scrollWidth, document.documentElement.scrollWidth,
    document.body.offsetWidth, document.documentElement.offsetWidth,
    document.body.clientWidth, document.documentElement.clientWidth
  );
}
  1. 获取窗口高度
function getWindowHeight() {
  return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
  1. 获取窗口宽度
function getWindowWidth() {
  return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}
  1. 获取鼠标位置
function getMousePosition(e) {
  return {
    x: e.clientX,
    y: e.clientY
  };
}
  1. 检查是否为移动设备
function isMobileDevice() {
  return typeof window.orientation !== 'undefined' || navigator.userAgent.indexOf('IEMobile') !== -1;
}
  1. 检查是否为 iOS 设备
function isIosDevice() {
  return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
}
  1. 检查是否为 Android 设备
function isAndroidDevice() {
  return navigator.userAgent.toLowerCase().indexOf("android") > -1;
}
``
20个好用的 js方法代码

原文地址: http://www.cveoy.top/t/topic/e3mG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录