tools.js

作者: Sasoli | 来源:发表于2019-06-24 16:59 被阅读0次
import { parseUrl } from 'query-string';
//获取元素的真实offsetLeft
export const getElementLeft = (element) => {
    let actualLeft = element.offsetLeft;
    let current = element.offsetParent;

    while (current !== null) {
        actualLeft += current.offsetLeft;
        current = current.offsetParent;
    }

    return actualLeft;
};
//获取元素的真实offsetTop
export const getElementTop = (element) => {
    let actualTop = element.offsetTop;
    let current = element.offsetParent;

    while (current !== null) {
        actualTop += current.offsetTop;
        current = current.offsetParent;
    }

    return actualTop;
};
//url防止缓存,添加?cors=
export const addNoCacheParam = (urlString) => {
    const url = new URL(urlString);
    url.searchParams.append('cors', '');
    return url.href;
}

// 取字符串?前面的内容
// str = www.baidu.com?uname=xx&uid=123
export const getFrontStr = (str) => {
  const frontStr = str;
  frontStr.split('?')[0];
  return frontStr;
}

//获取url携带的参数
export const getQuery = (param) => {
  const url = param || window.location.search;
  return parseUrl(url).query;
};

// 格式化播放时间
export const getSecondTimeFomate = (time, isShowHour = false) => {
  time = Math.ceil(time);
  let h = (parseInt(time / 3600, 10));
  if (h.toString().length === 1) {
    h = '0' + h;
  }
  let m = (parseInt((time % 3600) / 60, 10));
  if (m.toString().length === 1) {
    m = '0' + m;
  }
  let s = (time - 3600 * h - 60 * m);
  if (s.toString().length === 1) {
    s = '0' + s;
  }
  return isShowHour || time >= 3600 ? (h + ':' + m + ':' + s) : (m + ':' + s);
};

// 对象数组去重
import _ from 'lodash';

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.uniqWith(objects, _.isEqual);

// 金钱格式化
var test1 = '1234567890'
var format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',')

console.log(format) // 1,234,567,890

相关文章

网友评论

      本文标题:tools.js

      本文链接:https://www.haomeiwen.com/subject/wbvoqctx.html