zoukankan      html  css  js  c++  java
  • 一些逼格略高的 js 片段

    // 一个接一个运行
    // timeout 不能直接放在 for 里面,暂时不知道为什么
    function functionOneByOne(fn, times, duration) {
      for(var i=0; i<times; i++) {
        timecout(i);
      }
      function timecout(index) {
        setTimeout(function(){
          if (fn) fn(index);
        }, duration*index);
      }
    }
    
    // 区间内持续时间的变化,比如可以做平滑动画什么的
    function smooth(start, end, duration, fn) {
      var start = start || 0,
        end = end || 0,
        offset = end - start,
        duration = duration || 1000,
        now = Date.now(); a();
    
      function a() {
        var x = Math.min(1, (Date.now() - now) / duration);
        if (fn) fn(offset * x + start);
        1 > x && setTimeout(a, 10);
      }
    }
    

      

    // 求数组 arr 中的最大最小值
    Math.max.apply(Math, arr); 
    Math.min.apply(Math, arr); 
    

      

    //字符串去首尾空格
    String.prototype.trim = function(){return this.replace(/^s+|s+$/g, "");};
    

      

    // 区间内的随机数
    function RandomNumber(min, max) {
      return (min||0) + Math.random() * ((max||1) - (min||0));
    }
    

      

    // 获取 search 中对应键的值
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    

      

    // 生成一个连续数字的数组
    function creatNumberArray(length, start) {
      var arr = []; start = start || 0;
      for(var i=start; arr.push(i++)<length;);
      return arr;
    }
    

      

    // 数字数组 arr 改成从小到大排序
    arr.sort(function(a,b){return Math.random()>0.5 ? -1 : 1;});
    

      

    // 获得 dom 元素,可传入 类名/索引/对象
    function getElem(o, box) {
        if (typeof o == "string") return document.querySelector(obj);
        else if (typeof o == "number") return getObj(box).eq(o)[0];
        else if (typeof o == "object") {
            if (typeof o.css == "function") return o[0];
            else return o;
        }
    }
    // 获取 jquery 元素,参数同上
    function getObj(o, box) {
        if (typeof o == "string") return $(o);
        else if (typeof o == "number") return getObj(box).eq(o);
        else if (typeof o == "object") {
            if (typeof o.css == "function") return o;
            else return $(o);
        }
    }
    

      

    // 将 html 编译成 text
    function encodeHTML( str ) {
      var elem = document.createElement('span');
      elem.appendChild( document.createTextNode( str ) );
      return elem.innerHTML;
    }
    // 将 text 编译成 html
    function decodeHTML( str ) {
      str = str.replace(/</g,'<').replace(/>/g,'>')
      var elem = document.createElement('span');
      elem.innerHTML = str;
      return elem.textContent || elem.innerText;
    }
    

      

    // 产生随机颜色
    function randomColor() {
      var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16);
      if (rand.length == 6) return '#' + rand;
      else return randomColor();
    }
    

      

    这些方法个人都用得挺多的,然后再分享一个最近发现的装逼技能。

    var x = i * i, i == 5 && fn();
    // “,” 虽然看上去和 “;” 类似,但更容易表明 i 和 fn() 有基情
    // 其次,&& 则类同于 if(i == 5) fn(); 是不是很带感, i < 10 || fn(); 则能等同于 if(i != 5) fn();
    
  • 相关阅读:
    Centos7源码安装mysql及读写分离,互为主从
    Linux简介及Ubuntu安装
    CentOS7 IP自动获取
    memcached总结
    CentOS7安装iptables防火墙
    centos 7.0 mono&Jexus V5.5.3安装
    设置背景模糊效果
    vue 动画过渡
    sticky footer
    设置最低高度为100%
  • 原文地址:https://www.cnblogs.com/foreverZ/p/5820782.html
Copyright © 2011-2022 走看看