2018-01-01

1.列舉不同的清除浮動的技巧

/* 1.添加新元素 */
<div class="outer">
  <div class="div1"></div>
  <div class="div2"></div>
  <div class="div3"></div>
  <div class="clearfix"></div>
</div>
.clearfix {
  clear: both;
}

/* 2.為父元素增加樣式 */
.clearfix {
  overflow: auto;
  zoom: 1; // 處理兼容性
}

/* 3.:after 偽元素方法 (作用于父元素) */
.outer {
  zoom: 1;
  &:after {
    display: block;
    height: 0;
    clear: both;
    content: '.';
    visibillity: hidden;
  }
}

2.移動端一像素邊框問題

/* 定義 */
@mixin border-1px ($color) {
    position: relative;
    &:after {
        display: block;
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        border-top: 1px solid $color;
        context: '';
    }
}

@media (-webkit-min-device-pixel-radio: 1.5), (min-device-pixel-radio: 1.5) {
    border-1px {
        &:after {
            -webkit-transform: scaleY(0.7);
            transform: scaleY(0.7);
        }
    }
}

@media (-webkit-min-device-pixel-radio: 2), (min-device-pixel-radio: 2) {
    border-1px {
        &:after {
            -webkit-transform: scaleY(0.5);
            transform: scaleY(0.5);
        }
    }
}

/* 使用方式 */
@inclue border-1px(rgba(7, 17, 27, .1));

3.左定寬右自適應(yīng)寬度,并且等高布局(最小高度200px)

/* HTML */
<div class="container">
  <div class="left">Left silder</div>
  <div class="content">Main content</div>
</div>

/* CSS */
.container {
  overflow: hidden;
}

.left {
  float: left;
  width: 200px;
  margin-bottom: -9999px;
  padding-bottom: 9999px;
  background-color: #eee;
}

.content {
  margin-left: 200px;
  margin-bottom: -9999px;
  padding-bottom: 9999px;
  background-color: #ccc;
}

.left, .content {
  min-height: 200px;
  height: auto !important;
}
4.location.replace()與location.assign()區(qū)別
location.replace()的url不會出現(xiàn)在history中
5.DOM 操作
// 創(chuàng)建節(jié)點
createDocumentFragment()
createElement()
createTextNode()

// 添加 移除 替換 插入
appendChild()
removeChild()
replaceChild()
insertBefore()

// 查找
getElementsByTagName()
getElementsByName()
getElementsByClassName()
getElementById()
querySelector()
querySelectorAll()
6.JS設(shè)置css樣式的幾種方式
/* 1.直接設(shè)置style屬性 */
element.style.height = '100px';

/* 2.直接設(shè)置屬性 */
element.setAttribute('height', '100px');

/* 3.使用setAttribute設(shè)置style屬性 */
element.setAttribute('style', 'height: 100px !important');

/* 4.使用setProperty設(shè)置屬性,通過第三個參數(shù)設(shè)置important */
element.style.setProperty('height', '300px', 'important');

/* 5.設(shè)置cssText */
element.style.cssText += 'height: 100px !important';
7.阻止默認行為
function stopDefault( e ) {
    // 阻止默認瀏覽器動作(W3C)
    if ( e && e.preventDefault ) {
        e.preventDefault();
    } else {
        // IE中阻止函數(shù)器默認動作的方式
        window.event.returnValue = false;
    }
    return false;
}
8.復(fù)制的js題
function Foo() {
    getName = function () { alert(1); }
    return this;
}
Foo.getName = function () { alert(2); }
Foo.prototype.getName = function () { alert(3); }
var getName = function () { alert(4); }
function getName () { alert(5); }

/* 寫出輸出 */
Foo.getName(); //2
getName(); //4
Foo().getName(); // 1
getName(); //1
new Foo.getName(); // 2
new Foo().getName(); // 3
new new Foo().getName(); //3
9.JS數(shù)組深淺拷貝
//淺拷貝
var new_arr = arr.slice();
var new_arr = arr.concat();
// 推薦 淺拷貝
var shallowCopy = function (obj) {
    // 判斷是否是數(shù)組或者對象
    if (typeof obj !== 'object') {
        return
    }
    var newObj = obj instanceof Array ? [] : {};
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            newObj[key] = obj[key];
        }
    }
    return newObj;
}
//深拷貝
var deepCopy = function (obj) {
    if (typeof obj !== 'object') {
        return
    }
    var newObj = obj instanceof Array ? [] : {};
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            newObj[key] = typeof obj[key] === 'object' ? deepCopy(obj[key]) : obj[key];
        }
    }
    return newObj
}
10.數(shù)組去重
//filter + indexOf
function unique1 (arr) {
    var res = arr.filter(function (item, index, array) {
        return array.indexOf(item) === index;
    })
    return res;
}
///filter + sort
function unique2 (arr) {
    return arr.concat().sort().filter(function (item, index, array) {
        return !index || item !== array[index - 1];
    })
}
//es6
function uniqu3 (arr) {
    return [... new Set(arr)];
}
11.找出數(shù)組中的最大值
//reduce
var arr = [6, 4, 1, 8, 2, 11, 3];
function max (prev, next) {
    return Math.max(prev, next)
}
console.log(arr.reduce(max));
//apply
console.log(Math.max.apply(null, arr));
//ES6
function max (arr) {
    return Math.max(...arr);
}
console.log(max(arr));
12.數(shù)組扁平化
var arr = [1, [2, [3, 4]]];
function flatten(arr) {
    while (arr.some(item => Array.isArray(item))) {
        arr = [].concat(...arr);
    }
    return arr;
}
console.log(flatten(arr))
13.數(shù)字格式化 1234567890 -> 1,234,567,890
const priceSubstr = (num = '0', gap = ',') => {
  return num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, `$1${gap}`)
}
14.打亂數(shù)組的方法
var arr = [1,2,3,4,5,6]
arr.sort(function () {
    return 0.5 - Math.random();
})
15.柯里化
function add () {
    let sum = 0;
    Array.prototype.forEach.call(arguments, function (item, index){
        if (typeof item !== 'number') {
            return false;
        } else {
            sum += item;
        }
    })
    var tmp = function () {
        Array.prototype.forEach.call(arguments, function (item, index){
            if (typeof item !== 'number') {
                return false;
            } else {
               sum += item;
            }
        })
        return tmp;
    }
    tmp.toString = function () {
        return sum
    }
    return tmp;
}
add(1, 2); // 3
add(1)(2); // 3
add(1, 2, 3)(1, 4)(2, 2)(1) // 16
16.簡單的字符串模板
var TemplateEngine = function(tpl, data) {
    var re = /(?:\{)(\w*)(?:\})/g, match;
    while(match = re.exec(tpl)) {
        tpl = tpl.replace(match[0], data[match[1]])
    }
    return tpl;
}

var template = '<p>Hello, my name is {name}. I\'m {age} years old.</p>';
console.log(TemplateEngine(template, {
    name: "Yeaseon",
    age: 24
}));
最后編輯于
?著作權(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)容

  • oncontextmune事件 當用戶在某元素上點擊鼠標右鍵時觸發(fā)此事件。 如何獲取鼠標位置 當用戶點擊某元素,并...
    加加大叔閱讀 228評論 0 0
  • CSS中將每一個元素都設(shè)置為了一個矩形的盒子,便于方便的頁面布局。 盒子的組成部分 內(nèi)容區(qū),內(nèi)邊距,邊框,外邊距。...
    胸懷大海的小魚缸閱讀 280評論 0 0
  • 2018的頭一天依然用自己慣用的日記形式做標題,依然是隨手記。 早上被昨晚吃下的油膩的火鍋催著起來上了個廁所,再躺...
    坐看云起的狒狒閱讀 193評論 0 4
  • VTR CAD 流程 Odin II將Verilog硬件描述語言轉(zhuǎn)換為代表異構(gòu)塊的邏輯門和黑盒組成的扁平網(wǎng)表。 A...
    ATPX_39a2閱讀 351評論 0 0
  • 胡某是云南昆明一家做農(nóng)產(chǎn)品批發(fā)公司的老板,在當?shù)匾菜闶呛蘸沼忻娜宋锪?,資產(chǎn)早已過千萬,在當?shù)刂糜?處房產(chǎn),家中育...
    胡燕紅閱讀 806評論 0 1

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