JS小技巧

  • CSS 穿透覆蓋默認(rèn)樣式

    input標(biāo)簽 上傳type="file",使用img遮蓋,防止img阻隔點擊事件
    
    img {
      pointer-events: none;
    }
    
  • 自定義原生 select 控件的樣式

    select {
      -webkit-appearance: none;
    }
    
  • 文本溢出處理

    //單行
    .single {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    //多行
    .more {
      display: -webkit-box !important;
      overflow: hidden;
      text-overflow: ellipsis;
      work-break: break-all;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2; //指定行數(shù)
    }
    
  • 全屏居中 JS 函數(shù)

    function autoCenter(el) {
      var bodyX = document.documentElement.offsetWidth || document.body.offsetWidth;
      var bodyY = document.documentElement.offsetHeight || document.body.offsetHeight;
    
      var elementX = el.offsetWidth;
      var elementY = el.offsetHeight;
    
      el.style.left = (bodyX - elementX) / 2 + "px";
      el.style.top = (bodyY - elementY) / 2 + "px";
    }
    
  • 防止鼠標(biāo)選中事件

    <div class="mask" onselectstart="return false"></div>
    
  • 一像素邊框設(shè)置

    .folder li {
      position: relative;
      padding: 5px;
    }
    .folder li + li:before {
      position: absolute;
      top: -1px;
      left: 0;
      content: " ";
      width: 100%;
      height: 1px;
      border-top: 1px solid #ccc;
      -webkit-transform: scaleY(0.5);
    }
    
  • 徹底屏蔽鼠標(biāo)右鍵

    oncontextmenu=”window.event.returnValue=false”
    
  • 取消選取、防止復(fù)制

    < body onselectstart=”return false”>
    
  • JS不允許粘貼

    onpaste=”return false”
    
  • JS防止復(fù)制

    oncopy=”return false;” oncut=”return false;”
    
  • 禁用輸入法

    < input style=”ime-mode:disabled”>
    
  • 防止被人 frame

    if (top.location != self.location)top.location=self.location;
    
  • 網(wǎng)頁禁用另存為

    < no>< iframe src=*.html>< /iframe>< /no>
    
  • 判斷兩個數(shù)組是否相同

    export function scalarArrayEquals(array1, array2) {
      return array1.length === array2.length && array1.every(function(v, i) { return v ===array2[i]})
    }
    
  • 對象轉(zhuǎn)換為數(shù)組

     var obj = {
        0: 'a',
        1: 'b',
        2: 'c',
        3: 'd',
        length: 4
    }
    var objArr = Array.prototype.slice.call(obj);
    
  • 使用擴展運算符可以快速扁平化二維數(shù)組

    const arr = [1,[2,3],[4,5]]
    const flatArr = [].concat(...arr)
    console.log(flatArr)//[1, 2, 3, 4, 5]
    
  • 扁平化任意緯度數(shù)組

    function flattenArray(arr){
      const flatArr = [].concat(...arr)
      return flatArr.some(item=>Array.isArray(item))?flattenArray(flatArr):flatArr
    }
    const arr = [1,[2,3],[4,5,[6,7,[8,9]]]]
    console.log(flattenArray(arr))//[1, 2, 3, 4, 5, 6, 7, 8, 9]
    
  • 對數(shù)組中的所有值求和

    var numbers = [3, 5, 7, 2];
    var sum = numbers.reduce((x, y) => x + y);
    console.log(sum); // returns 17
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評論 0 2
  • 轉(zhuǎn)載來源:編程學(xué)習(xí)網(wǎng) 1. 將徹底屏蔽鼠標(biāo)右鍵oncontextmenu=”window.event.return...
    努力的河馬君閱讀 439評論 0 3
  • 1、將徹底屏蔽鼠標(biāo)右鍵 oncontextmenu=”window.event.returnValue=false...
    無名小碼農(nóng)閱讀 420評論 0 1
  • <template> 打印二維碼 X 已選構(gòu)件列表 ...
    天街夜雨閱讀 843評論 0 0
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,822評論 28 54

友情鏈接更多精彩內(nèi)容