utils->js->bomObject->bom函数集合(工具类函数)

/**
 *打开浏览器
 * @param {Sting} url
 * @param {Sting} title
 * @param {Number} w
 * @param {Number} h
 */

export function openWindow(url, title, w, h) {
  // Fixes dual-screen position                            Most browsers       Firefox
  const dualScreenLeft =
    window.screenLeft !== undefined ? window.screenLeft : screen.left;
  const dualScreenTop =
    window.screenTop !== undefined ? window.screenTop : screen.top;

  const width = window.innerWidth
    ? window.innerWidth
    : document.documentElement.clientWidth
    ? document.documentElement.clientWidth
    : screen.width;
  const height = window.innerHeight
    ? window.innerHeight
    : document.documentElement.clientHeight
    ? document.documentElement.clientHeight
    : screen.height;

  const left = width / 2 - w / 2 + dualScreenLeft;
  const top = height / 2 - h / 2 + dualScreenTop;
  const newWindow = window.open(
    url,
    title,
    "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=" +
      w +
      ", height=" +
      h +
      ", top=" +
      top +
      ", left=" +
      left
  );

  // Puts focus on the newWindow
  if (window.focus) {
    newWindow.focus();
  }
}
//判断是否启用flash
export function hasUsableFlash() {
  var flashObj;
  //普通的浏览器,可以直接使用window.ActiveXObject来检查浏览器是否安装或启用了相关的控件
  if (typeof window.ActiveXObject != "undefined") {
    // 如果,window.ActiveXObject不等于undefined,使用new ActiveXObject("ShockwaveFlash.ShockwaveFlash")返回flash控件对象。
    flashObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
  } else {
    //谷歌、火狐、微软Edge、Safari等现代浏览器不支持ActiveXObject,它们支持navigator.plugins检查浏览器插件
    //否则,使用navigator.plugins来判断谷歌、火狐、微软Edge、Safari等浏览器
    flashObj = navigator.plugins["Shockwave Flash"];
  }
  //这里使用三目运算符
  return flashObj ? true : false;
}

// 页面跳转方式下载
export function downExcel(url, options) {
  options.timeout = 180000; //毫秒
  options.responseType = "blob"; //毫秒
  return request(url, options)
    .then((res) => {
      if ("msSaveOrOpenBlob" in navigator) {
        //兼容ie
        var data = res; //获取响应
        var blob = new Blob([data], { type: "application/vnd.ms-excel" });
        window.navigator.msSaveOrOpenBlob(blob, options.data.fileName + ".xls");
      } else {
        var blob = new Blob([res], { type: "text/plain,charset=UTF-8" });
        var url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.style.display = "none";
        link.download = options.data.fileName + ".xls";
        link.href = url;
        link.click();
        document.body.removeChild(link);
      }
    })
    .catch((err) => ({ err }));
}
// 流方式下载
export function downPhoto(item) {
  let xhr = new XMLHttpRequest();
  xhr.open("GET", item, true);
  xhr.responseType = "arraybuffer";
  xhr.onload = function () {
    if (this.status === 200) {
      let type = xhr.getResponseHeader("Content-Type");
      if (type === "application/json;charset=UTF-8") {
        let uint8 = new Uint8Array(this.response);
        let resToString = decodeURIComponent(
          escape(String.fromCharCode(...uint8))
        );
        let message = JSON.parse(resToString).message;
        console.log(message);
        return;
      }

      var blob = new Blob([this.response], { type: type });
      if (typeof window.navigator.msSaveBlob !== "undefined") {
        window.navigator.msSaveBlob(blob, fileName);
      } else {
        let URL = window.URL || window.webkitURL;
        let objectUrl = URL.createObjectURL(blob);
        if (fileName) {
          var a = document.createElement("a");

          if (typeof a.download === "undefined") {
            window.location = objectUrl;
          } else {
            a.href = objectUrl;
            a.download = fileName;
            document.body.appendChild(a);
            a.click();
            a.remove();
          }
        } else {
          window.location = objectUrl;
        }
        URL.revokeObjectURL(objectUrl);
      }
    }
  };
  xhr.send();
}
// 获取设备的 pixel ratio
export function getPixelRatio(context) {
  var backingStore =
    context.backingStorePixelRatio ||
    context.webkitBackingStorePixelRatio ||
    context.mozBackingStorePixelRatio ||
    context.msBackingStorePixelRatio ||
    context.oBackingStorePixelRatio ||
    context.backingStorePixelRatio ||
    1;
  return (window.devicePixelRatio || 1) / backingStore;
}
/*
判断环境
//判断是否IE内核
if(browser.versions.trident){ alert("is IE"); }
//判断是否webKit内核
if(browser.versions.webKit){ alert("is webKit"); }
//判断是否移动端
if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); }
*/
export const browser = {
  versions: (function () {
    var u = navigator.userAgent,
      app = navigator.appVersion;
    return {
      core_trident: u.indexOf("Trident") > -1, //IE内核
      core_presto: u.indexOf("Presto") > -1, //opera内核
      core_webKit: u.indexOf("AppleWebKit") > -1, //苹果、谷歌内核
      core_gecko: u.indexOf("Gecko") > -1 && u.indexOf("KHTML") == -1, //火狐内核

      mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
      mobile_ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
      mobile_android: u.indexOf("Android") > -1 || u.indexOf("Adr") > -1, //android终端

      apple_iPhone: u.indexOf("iPhone") > -1, //是否为iPhone或者QQHD浏览器
      apple_iPad: u.indexOf("iPad") > -1, //是否iPad
      apple_webApp: u.indexOf("Safari") == -1, //是否web应该程序,没有头部与底部
      wechat_weixin: u.indexOf("MicroMessenger") > -1, //是否微信 (2015-01-22新增)
      qq: u.match(/\sQQ/i) == " qq", //是否QQ
    };
  })(),
  language: (navigator.browserLanguage || navigator.language).toLowerCase(),
};


/**项目上下滚轮位置设置
 * @param {number} to
 * @param {number} duration
 * @param {Function} callback
 */
export function scrollTo(to, duration, callback) {
  const start = position()
  const change = to - start
  const increment = 20
  let currentTime = 0
  duration = (typeof (duration) === 'undefined') ? 500 : duration
  var animateScroll = function() {
    // increment the time
    currentTime += increment
    // find the value with the quadratic in-out easing function
    var val = Math.easeInOutQuad(currentTime, start, change, duration)
    // move the document.body
    move(val)
    // do the animation unless its over
    if (currentTime < duration) {
      requestAnimFrame(animateScroll)
    } else {
      if (callback && typeof (callback) === 'function') {
        // the animation is done so lets callback
        callback()
      }
    }
  }
  animateScroll()
}


Math.easeInOutQuad = function(t, b, c, d) {
  t /= d / 2
  if (t < 1) {
    return c / 2 * t * t + b
  }
  t--
  return -c / 2 * (t * (t - 2) - 1) + b
}

// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
var requestAnimFrame = (function() {
  return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) }
})()

/**
 * Because it's so fucking difficult to detect the scrolling element, just move them all
 * @param {number} amount
 */
function move(amount) {
  document.documentElement.scrollTop = amount
  document.body.parentNode.scrollTop = amount
  document.body.scrollTop = amount
}

function position() {
  return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值