乐趣区

关于javascript:36个工作中常用的JavaScript函数片段

如果文章和笔记能带您一丝帮忙或者启发,请不要悭吝你的赞和珍藏,你的必定是我后退的最大能源😁

  • 附笔记链接,浏览往期更多优质文章可移步查看,喜爱的能够给我点赞激励哦:https://github.com/Wscats/CV/issues/32

数组 Array

数组去重

function noRepeat(arr) {return [...new Set(arr)];
}

查找数组最大

function arrayMax(arr) {return Math.max(...arr);
}

查找数组最小

function arrayMin(arr) {return Math.min(...arr);
}

返回已 size 为长度的数组宰割的原数组

function chunk(arr, size = 1) {
  return Array.from(
    {length: Math.ceil(arr.length / size),
    },
    (v, i) => arr.slice(i * size, i * size + size)
  );
}

查看数组中某元素呈现的次数

function countOccurrences(arr, value) {return arr.reduce((a, v) => (v === value ? a + 1 : a + 0), 0);
}

扁平化数组

  • 默认 depth 全副开展
function flatten(arr, depth = -1) {if (depth === -1) {return [].concat(...arr.map((v) => (Array.isArray(v) ? this.flatten(v) : v))
    );
  }
  if (depth === 1) {return arr.reduce((a, v) => a.concat(v), []);
  }
  return arr.reduce((a, v) => a.concat(Array.isArray(v) ? this.flatten(v, depth - 1) : v),
    []);
}

比照两个数组并且返回其中不同的元素

function diffrence(arrA, arrB) {return arrA.filter((v) => !arrB.includes(v));
}

返回两个数组中雷同的元素

function intersection(arr1, arr2) {return arr2.filter((v) => arr1.includes(v));
}

从右删除 n 个元素

function dropRight(arr, n = 0) {return n < arr.length ? arr.slice(0, arr.length - n) : [];}

截取第一个符合条件的元素及其当前的元素

function dropElements(arr, fn) {while (arr.length && !fn(arr[0])) arr = arr.slice(1);
  return arr;
}

返回数组中下标距离 nth 的元素

function everyNth(arr, nth) {return arr.filter((v, i) => i % nth === nth - 1);
}

返回数组中第 n 个元素

  • 反对正数
function nthElement(arr, n = 0) {return (n >= 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
}

返回数组头元素

function head(arr) {return arr[0];
}

返回数组开端元素

function last(arr) {return arr[arr.length - 1];
}

数组乱排

function shuffle(arr) {
  let array = arr;
  let index = array.length;

  while (index) {
    index -= 1;
    let randomInedx = Math.floor(Math.random() * index);
    let middleware = array[index];
    array[index] = array[randomInedx];
    array[randomInedx] = middleware;
  }

  return array;
}

浏览器对象 BOM

判读浏览器是否反对 CSS 属性

/**
 * 告知浏览器反对的指定 css 属性状况
 * @param {String} key - css 属性,是属性的名字,不须要加前缀
 * @returns {String} - 反对的属性状况
 */
function validateCssKey(key) {const jsKey = toCamelCase(key); // 有些 css 属性是连字符号造成
  if (jsKey in document.documentElement.style) {return key;}
  let validKey = "";
  // 属性名为前缀在 js 中的模式,属性值是前缀在 css 中的模式
  // 经尝试,Webkit 也可是首字母小写 webkit
  const prefixMap = {
    Webkit: "-webkit-",
    Moz: "-moz-",
    ms: "-ms-",
    O: "-o-",
  };
  for (const jsPrefix in prefixMap) {const styleKey = toCamelCase(`${jsPrefix}-${jsKey}`);
    if (styleKey in document.documentElement.style) {validKey = prefixMap[jsPrefix] + key;
      break;
    }
  }
  return validKey;
}

/**
 * 把有连字符号的字符串转化为驼峰命名法的字符串
 */
function toCamelCase(value) {return value.replace(/-(\w)/g, (matched, letter) => {return letter.toUpperCase();
  });
}

/**
 * 查看浏览器是否反对某个 css 属性值(es6 版)* @param {String} key - 查看的属性值所属的 css 属性名
 * @param {String} value - 要查看的 css 属性值(不要带前缀)* @returns {String} - 返回浏览器反对的属性值
 */
function valiateCssValue(key, value) {const prefix = ["-o-", "-ms-", "-moz-", "-webkit-", ""];
  const prefixValue = prefix.map((item) => {return item + value;});
  const element = document.createElement("div");
  const eleStyle = element.style;
  // 利用每个前缀的状况,且最初也要利用上没有前缀的状况,看最初浏览器起效的何种状况
  // 这就是最好在 prefix 里的最初一个元素是 ''
  prefixValue.forEach((item) => {eleStyle[key] = item;
  });
  return eleStyle[key];
}

/**
 * 查看浏览器是否反对某个 css 属性值
 * @param {String} key - 查看的属性值所属的 css 属性名
 * @param {String} value - 要查看的 css 属性值(不要带前缀)* @returns {String} - 返回浏览器反对的属性值
 */
function valiateCssValue(key, value) {var prefix = ["-o-", "-ms-", "-moz-", "-webkit-", ""];
  var prefixValue = [];
  for (var i = 0; i < prefix.length; i++) {prefixValue.push(prefix[i] + value);
  }
  var element = document.createElement("div");
  var eleStyle = element.style;
  for (var j = 0; j < prefixValue.length; j++) {eleStyle[key] = prefixValue[j];
  }
  return eleStyle[key];
}

function validCss(key, value) {const validCss = validateCssKey(key);
  if (validCss) {return validCss;}
  return valiateCssValue(key, value);
}
  • 摘自 https://juejin.im/post/5e58f398f265da574a1eb569

返回以后网页地址

function currentURL() {return window.location.href;}

获取滚动条地位

function getScrollPosition(el = window) {
  return {
    x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
    y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop,
  };
}

获取 url 中的参数

function getURLParameters(url) {
  return url
    .match(/([^?=&]+)(=([^&]*))/g)
    .reduce((a, v) => ((a[v.slice(0, v.indexOf("="))] = v.slice(v.indexOf("=") + 1)), a
      ),
      {});
}

页面跳转,是否记录在 history 中

function redirect(url, asLink = true) {asLink ? (window.location.href = url) : window.location.replace(url);
}

滚动条回到顶部动画

function scrollToTop() {
  const scrollTop =
    document.documentElement.scrollTop || document.body.scrollTop;
  if (scrollTop > 0) {window.requestAnimationFrame(scrollToTop);
    window.scrollTo(0, c - c / 8);
  } else {window.cancelAnimationFrame(scrollToTop);
  }
}

复制文本

function copy(str) {const el = document.createElement("textarea");
  el.value = str;
  el.setAttribute("readonly", "");
  el.style.position = "absolute";
  el.style.left = "-9999px";
  el.style.top = "-9999px";
  document.body.appendChild(el);
  const selected =
    document.getSelection().rangeCount > 0
      ? document.getSelection().getRangeAt(0)
      : false;
  el.select();
  document.execCommand("copy");
  document.body.removeChild(el);
  if (selected) {document.getSelection().removeAllRanges();
    document.getSelection().addRange(selected);
  }
}

检测设施类型

function detectDeviceType() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
    ? "Mobile"
    : "Desktop";
}

Cookie

function setCookie(key, value, expiredays) {var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie =
    key +
    "=" +
    escape(value) +
    (expiredays == null ? "":";expires=" + exdate.toGMTString());
}

function delCookie(name) {var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval != null) {document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
  }
}

function getCookie(name) {
  var arr,
    reg = new RegExp("(^|)" + name + "=([^;]*)(;|$)");
  if ((arr = document.cookie.match(reg))) {return arr[2];
  } else {return null;}
}

日期 Date

工夫戳转换为工夫

  • 默认为以后工夫转换后果
  • isMs 为工夫戳是否为毫秒
function timestampToTime(timestamp = Date.parse(new Date()), isMs = true) {const date = new Date(timestamp * (isMs ? 1 : 1000));
  return `${date.getFullYear()}-${date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}

  文档对象 DOM

固定滚动条

/**
 * 性能形容:一些业务场景,如弹框呈现时,须要禁止页面滚动,这是兼容安卓和 iOS 禁止页面滚动的解决方案
 */

let scrollTop = 0;

function preventScroll() {
  // 存储以后滚动地位
  scrollTop = window.scrollY;

  // 将可滚动区域固定定位,可滚动区域高度为 0 后就不能滚动了
  document.body.style["overflow-y"] = "hidden";
  document.body.style.position = "fixed";
  document.body.style.width = "100%";
  document.body.style.top = -scrollTop + "px";
  // document.body.style['overscroll-behavior'] = 'none'
}

function recoverScroll() {document.body.style["overflow-y"] = "auto";
  document.body.style.position = "static";
  // document.querySelector('body').style['overscroll-behavior'] = 'none'

  window.scrollTo(0, scrollTop);
}

判断以后地位是否为页面底部

  • 返回值为 true/false
function bottomVisible() {
  return (
    document.documentElement.clientHeight + window.scrollY >=
    (document.documentElement.scrollHeight ||
      document.documentElement.clientHeight)
  );
}

判断元素是否在可视范畴内

  • partiallyVisible 为是否为齐全可见
function elementIsVisibleInViewport(el, partiallyVisible = false) {const { top, left, bottom, right} = el.getBoundingClientRect();

  return partiallyVisible
    ? ((top > 0 && top < innerHeight) ||
        (bottom > 0 && bottom < innerHeight)) &&
        ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
    : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
}

获取元素 css 款式

function getStyle(el, ruleName) {return getComputedStyle(el, null).getPropertyValue(ruleName);
}

进入全屏

function launchFullscreen(element) {if (element.requestFullscreen) {element.requestFullscreen();
  } else if (element.mozRequestFullScreen) {element.mozRequestFullScreen();
  } else if (element.msRequestFullscreen) {element.msRequestFullscreen();
  } else if (element.webkitRequestFullscreen) {element.webkitRequestFullScreen();
  }
}

launchFullscreen(document.documentElement);
launchFullscreen(document.getElementById("id")); // 某个元素进入全屏 

退出全屏

function exitFullscreen() {if (document.exitFullscreen) {document.exitFullscreen();
  } else if (document.msExitFullscreen) {document.msExitFullscreen();
  } else if (document.mozCancelFullScreen) {document.mozCancelFullScreen();
  } else if (document.webkitExitFullscreen) {document.webkitExitFullscreen();
  }
}

exitFullscreen();

全屏事件

document.addEventListener("fullscreenchange", function (e) {if (document.fullscreenElement) {console.log("进入全屏");
  } else {console.log("退出全屏");
  }
});

数字 Number

数字千分位宰割

function commafy(num) {return num.toString().indexOf(".") !== -1
    ? num.toLocaleString()
    : num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
}

生成随机数

function randomNum(min, max) {switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * min + 1, 10);
    case 2:
      return parseInt(Math.random() * (max - min + 1) + min, 10);
    default:
      return 0;
  }
}

交换

文章同步继续更新,往期文章收录在 https://github.com/Wscats/art…
欢迎您的关注和交换😁

退出移动版