JS常用

  1. 立即执行函数

    (function () {})()
    (function () {}())
    
  2. rem值初始化

     (function () {
         var width = document.documentElement.clientWidth;
         document.documentElement.style.fontSize = (width <= 750) ? width / 7.5 + "px" : "100px";
     }());
     window.onresize = function () {
         var width = document.documentElement.clientWidth;
         document.documentElement.style.fontSize = (width <= 750) ? width / 7.5 + "px" : "100px";
     };
    
  3. foreach

    var arr = [
        a = 1,
        b = 2
    ];
    var sum = 0;
    arr.forEach(function(val,key,arr){
      console.log(arr[key] === val);
      sum += val;
    });
    console.log(sum);
    
  4. 对象转json字符串

    var obj = [{"name":"dog","age":10},{"name":"cat","age":8}];
    var str = JSON.stringify(obj);
    console.log(obj[0].name);
    console.log(obj[1].age);
    console.log(str);
    
  5. json字符串转对象

    var str = '[{"name":"dog","age":10},{"name":"cat","age":8}]';
    var obj = JSON.parse(str);
    console.log(obj[0].name);
    console.log(obj[1].name);
    console.log(str);
    
  6. js时间转日期(自动处理为用户系统设定时区的时间)

    var i,str;
    var date=new Date(1488002361000);
    var arr=[
    date.getFullYear(),
    date.getMonth()+1,
    date.getDate(),
    date.getHours(),
    date.getMinutes(),
    date.getSeconds()
    ];
    for(i=1;i<=5;i++){
        arr[i]=("0"+arr[i]).slice(-2);
    }
    str=arr[0]+'-'+arr[1]+'-'+arr[2]+' '+arr[3]+':'+arr[4]+':'+arr[5];
    console.log(str);
    
  7. js文本转声音

    var text = "我应该说点什么?";
    var speech = new window.SpeechSynthesisUtterance();
    speech.lang = "zh-CN";          //语言
    speech.text = text;             //内容
    speech.rate = 4;                //语速[0,10]
    speech.pitch = 1;               //音频[0,2]
    speech.volume = 1;              //音量[0,1]
    
    speech.onmark = null;
    speech.onpause = null;
    speech.onresume = null;
    speech.onboundary = null;
    
    speech.onstart = null;
    speech.onerror = null;
    speech.onend = null;
    
    window.speechSynthesis.speak(speech);
    
  8. js区块拖动

    <style>
        * {
            margin: 0;
            padding: 0
        }
        #user_cover {
            position: fixed;
            top: 40%;
            right: 0;
            width: 4rem;
            height: 4rem;
            background-color: rgba(0, 0, 0, 0.3);
        }
    </style>
    <div id="user_cover"></div>
    <script>
        //手机拖动
        function onTouchMove(point) {
            document.getElementById('user_cover').style.top = (point.changedTouches[0].clientY) + "px";
            document.getElementById('user_cover').style.left = (point.changedTouches[0].clientX) + "px";
        }
        document.getElementById('user_cover').addEventListener('touchmove', onTouchMove, false);
        //电脑拖动
        function onMouseDown(point) {
            document.onmousemove = function (event) {
                document.getElementById('user_cover').style.top = event.clientY + "px";
                document.getElementById('user_cover').style.left = event.clientX + "px";
                console.log(point);
                console.log(event);
            };
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        }
        document.getElementById('user_cover').addEventListener('mousedown', onMouseDown, false);
    </script>
    

results matching ""

    No results matching ""