DOFAR V4 MOBILE 爱课堂V4 (五)

一、设置body rem

(function flexible (window, document) {

  var docEl = document.documentElement

  var dpr = window.devicePixelRatio || 1

  const width = window.screen.width

  const height = window.screen.height

  var j = 10

  if(width/height > 0.74){

    j = 26.5

  }

  function setRemUnit () {

    var rem = 37.5

    if (rem > 75) rem = 75

    docEl.style.fontSize = rem + 'px'

  }

  setRemUnit()

  // reset rem unit on page resize

  window.addEventListener('resize', setRemUnit)

  window.addEventListener('pageshow', function (e) {

    if (e.persisted) {

      setRemUnit()

    }

  })

}(window, document))

二、字节转文件大小

export function renderSize (value) {

  if (!value || value === '0') {

    return '0 Bytes'

  }

  const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

  let index = 0

  const srcsize = parseFloat(value)

  index = Math.floor(Math.log(srcsize) / Math.log(1024))

  let size = srcsize / Math.pow(1024, index)

  size = size.toFixed(2)// 保留的小数位数

  size = parseFloat(size)

  return size + unitArr[index]

}

三、函数防抖

/**

* @description: 函数防抖(就是指触发事件后在 n 秒内函数只能执行一次,如果在 n 秒内又触发了事件,则会重新计算函数执行时间)

* 使用方法:在methods中:

* outInfo: debounce(function () {

*    this.$emit('outInfo')

*  }, 3000, true),

* @param {fun} func 函数

* @param {Number} delay 延迟执行毫秒数

* @param {Boolean} immediate true 表立即执行,false 表非立即执行

* @return: 执行函数

*/

export function debounce (func, delay, immediate, tipCall) {

  let timeout

  return function () {

    const context = this

    const args = arguments

    if (timeout) {

      if (tipCall) tipCall()

      clearTimeout(timeout)

    }

    if (immediate) {

      var callNow = !timeout

      timeout = setTimeout(() => {

        timeout = null

      }, delay)

      if (callNow) func.apply(context, args)

    } else {

      timeout = setTimeout(function () {

        func.apply(context, args)

      }, delay)

    }

  }

}

四、函数节流

/**

* @description: 函数节流(续触发事件但是在 n 秒中只执行一次函数)

* @param {fun} func 函数

* @param {Number} delay 延迟执行毫秒数

* @return: 执行函数

*/

export function throttle (func, delay) {

  let timer = null

  let startTime = Date.now()

  return function () {

    const curTime = Date.now()

    const remaining = delay - (curTime - startTime)

    const context = this

    const args = arguments

    clearTimeout(timer)

    if (remaining <= 0) {

      func.apply(context, args)

      startTime = Date.now()

    } else {

      timer = setTimeout(func, remaining)

    }

  }

}

五、获取最大zindex

/**

* @description: 获取最大zindex

*/

export function getMaxZIndex () {

  const arr = [...document.all].map(e => +window.getComputedStyle(e).zIndex || 0)

  return arr.length ? Math.max(...arr) + 1 : 0

}

六、将秒转换成合理的时间字符串

/**

* @description: 将秒转换成合理的时间字符串

* @param {Number} val 秒

*/

export function transfromSecond(value){

  if ((value / (60)) < 1) {

    return value + ' 秒'

  } else if ((value / (60 * 60)) < 1) {

    return (value / (60)).toFixed(2) + ' 分'

  } else if ((value / (60 * 60 * 24)) < 1) {

    return (value / (60 * 60)).toFixed(2) + ' 小时'

  } else {

    return (value / (24 * 60 * 60)).toFixed(2) + ' 天'

  }

}

七、生成uuid 重复性很小但并不为0

function S4 () {

  return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)

}

/**

* @description: 生成uuid 重复性很小但并不为0

*/

export function getUUID () {

  const uud = localStorage.getItem('UUID')

  if(uud && (uud !== "null")){

    return uud

  }

  else{

    let uuid = (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())

    localStorage.setItem('UUID',uuid)

    return uuid

  }

}

八、判断当前是否是pad

/**

* @description: 判断当前是否是pad

*/

export function judgePad(){

  const width = window.screen.width

  const height = window.screen.height

  if(width/height > 0.74){

    localStorage.setItem('IsPad',"YES")

  }else{

    localStorage.setItem('IsPad','NO')

  }

}

九、判断当前是否是iphoneX

/**

* @description: 判断当前是否是iphoneX

*/

export function judgeIphoneX(){

  if(localStorage.phoneType === 'ios'){

    const height = window.screen.height

    if(height >= 812){

      return true

    }

  }

  return false

}

十、对Date的扩展,将 Date 转化为指定格式的String

// 对Date的扩展,将 Date 转化为指定格式的String

// 月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 

// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 

// 例子: 

// (new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2006-07-02 08:09:04.423 

// (new Date()).Format("yyyy-M-d H:m:s.S")      ==> 2006-7-2 8:9:4.18

export function formatDate (date,fmt) {

  var o = { 

    "M+" : date.getMonth()+1,                //月份 

    "d+" : date.getDate(),                    //日 

    "H+" : date.getHours(),                  //小时 

    "m+" : date.getMinutes(),                //分 

    "s+" : date.getSeconds(),                //秒 

    "q+" : Math.floor((date.getMonth()+3)/3), //季度 

    "S"  : date.getMilliseconds()            //毫秒 

  }; 

  if(/(y+)/.test(fmt)) 

    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 

  for(var k in o) 

    if(new RegExp("("+ k +")").test(fmt)) 

  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 

  return fmt;

}

项目:https://iclass1.com:8099/stu/#/index

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容