这些JS工具函数够你用到2020年底了

2022-06-28 17:07:45 浏览数 (1)

前言

活不多说,自己平时搜集的干货函数奉上。 干货函数 视频全屏

代码语言:javascript复制
  // 全屏
  function fullScreen(el) {
    const isFullscreen =
      document.fullScreen ||
      document.mozFullScreen ||
      document.webkitIsFullScreen;
    if (!isFullscreen) {
      //进入全屏,多重短路表达式
      (el.requestFullscreen && el.requestFullscreen()) ||
        (el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
        (el.webkitRequestFullscreen && el.webkitRequestFullscreen()) ||
        (el.msRequestFullscreen && el.msRequestFullscreen());
    } else {
      //退出全屏,三目运算符
      document.exitFullscreen
        ? document.exitFullscreen()
        : document.mozCancelFullScreen
        ? document.mozCancelFullScreen()
        : document.webkitExitFullscreen
        ? document.webkitExitFullscreen()
        : "";
    }
  }
1234567891011121314151617181920212223

找出数字在数组中下一个相邻的元素

let i = “”; let rr = [];

const name = (n, arr1)=>{ let num = Number(n); for (let i = 0; i < arr1.length; i ) { const element = arr1[i]; if (element != num) { rr.push(num–); } } return rr.find((el) => { let newel = String(el); return arr1.includes(newel); })}

let arr = [“2”, “4”, “6”, “8”, “10”, “12”, “14”, “16”, “18”, “20”, “22”, “24”, “27”, “30”, “33”, “36”, “42”, “48”, “54”, “60”] console.log(name(‘5’,arr)); //4

格式化时间

/**

  • @param {number} time
  • @param {string} option
  • @returns {string} */ function formatTime(time, option) { if ((’’ time).length === 10) { time = parseInt(time) * 1000 } else { time = time } const d = new Date(time) const now = Date.now()

const diff = (now - d) / 1000

if (diff < 30) { return ‘刚刚’ } else if (diff < 3600) { // less 1 hour return Math.ceil(diff / 60) ‘分钟前’ } else if (diff < 3600 * 24) { return Math.ceil(diff / 3600) ‘小时前’ } else if (diff < 3600 * 24 * 2) { return ‘1天前’ } if (option) { return parseTime(time, option) } else { return ( d.getMonth()

更多内容请见原文,原文转载自:http://www.mark-to-win.com/tutorial/50895.html

0 人点赞