判断是否支持 Intersection
export function isSupportIntersection() {
  return (
    'IntersectionObserver' in window &&
    'IntersectionObserverEntry' in window &&
    'intersectionRatio' in window.IntersectionObserverEntry.prototype
  )
}
判断是否IOS
export const isIOS = (() => {
  return /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase())
})()
判断是否安卓
export const isAndroid = (() {
  return /android/.test(navigator.userAgent.toLowerCase())
})()
判断是否微信内置浏览器
export function isWeixin() {
  var ua = navigator.userAgent.toLowerCase();
  return (ua.match(/MicroMessenger/i) == "micromessenger")
}
判断是否支持webp格式 2种方式
export function checkSupportWebp() {
  return (
    document
      .createElement('canvas')
      .toDataURL('image/webp')
      .indexOf('data:image/webp') === 0
  )
}
export function checkSupportWebp2() {
  var img = new Image();
  img.onload = img.onerror = (event) => {
    return event && event.type === "load" ? img.width == 1 : false;
  };
  img.src = "data:image/webp;base64,UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=";
}
判断浏览器是否是移动端
export function isMobile() {
    const agent = navigator.userAgent;
    const k = ["android", "iphone", "ipod", "ipad", "windows phone", "mqqbrowser"];
    let flag = false;
    // Windows
    if (agent.indexOf("Windows NT") < 0 || (agent.indexOf("Windows NT") >= 0 && agent.indexOf("compatible; MSIE 9.0;") >= 0)) {
        // Mac PC
        if (agent.indexOf("Windows NT") < 0 && agent.indexOf("Macintosh") < 0) {
            for (let item of k) {
                if (agent.indexOf(item) >= 0) {
                    flag = true;
                    break;
                }
            }
        }
    }
    return flag;
}
文件类型判断
export function checkFileName(fileName, list) {
    if (typeof fileName !== 'string') return;
    let name = fileName.toLowerCase();
    return list.some(i => name.endsWith(`.${i}`) === true)
}

export function isImage(fileName) {
    return checkFileName(fileName, ['png', 'jpeg', 'jpg', 'png', 'bmp'])
}

export function isH5Video(fileName) {
    return checkFileName(fileName, ['mp4', 'webm', 'ogg'])
}
export function isPdf(fileName) {
    return checkFileName(fileName, ['pdf'])
}

export function isWord(fileName) {
    return checkFileName(fileName, ['doc', 'docx'])
}

export function isExcel(fileName) {
    return checkFileName(fileName, ['xlsx', 'xls'])
}
数据类型判断
export function is(subject, type) {
  return Object.prototype.toString.call(subject).substr(8, type.length).toLowerCase() === type
}

export function isArray(subject) {
  return Array.isArray(subject)
}

export function isObject(subject) {
  return is(subject, 'object')
}

export function isNum(subject) {
  return !isNaN(subject) && is(subject, 'number')
}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐