4

1.原生JS封装Jsonp

function createScript(src,callback) {
    let script = document.createElement("script");
    script.src = src+"callback";
    document.appendChild(script)
}

function callback(json) {
    console.log(json)
}

2.JS发送Ajax

let xhr = new XMLHttpRequest();
xhr.open("GET", "url",false)//false异步,true同步
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            console.log(xhr.responseText)
        }
    }
}
xhr.send(null)

3.JS实现图片懒加载

var aImg = document.querySelectorAll('img');
        var len = aImg.length;
        var n = 0;//存储图片加载到的位置,避免每次都从第一张图片开始遍历
        window.onscroll = function() {
            var seeHeight = document.documentElement.clientHeight;
            var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
            for (var i = n; i < len; i++) {
                if (aImg[i].offsetTop < seeHeight + scrollTop) {
                    if (aImg[i].getAttribute('src') == '') {
                        aImg[i].src = aImg[i].getAttribute('data-src');
                    }
                    n = i + 1;
                    console.log('n = ' + n);
                }
            }
        };

Healy
204 声望0 粉丝

前端新人学习ing!