ailingangel / web-performance Goto Github PK
View Code? Open in Web Editor NEW前端性能问题
前端性能问题
图片懒加载和图片预加载是常见的两种进行性能优化的手段
图片预加载就是提前把要用到的图片下载下来,然后等到需要查看的图片的时候浏览器直接从缓存中读取
实现原理: 利用 new Image(), 当新建一个图片对象时,并且给这个对象赋值src属性浏览器就会自动将图片缓存下来; 然后后续用到相同src路径的Img标签会自动从缓存中读取图片文件。缓存时间的话,我测试了一下感觉就是浏览器当前的会话~~~~~
方法一: 使用js预加载图片
function loadImage(url, callback) {
var img = new Image(); // 创建一个Image对象,实现图片的预下载
img.onload = function(){
img.onload = null; // 解决闭包的问题
callback(img);
}
img.src = url;
}
注意事项:
方法二:使用css预加载图片
// 放在onload事件回调里是为了不方案html的下载,而是等到html文件下载完成再开始预加载
window.onload = function() {
let style = document.getElementById("preload-01").style
style.display = 'none';
style.background = "url(http://domain.tld/image-01.png) no-repeat";
}
方法三:使用ajax预加载(js, css)
window.onload = function() {
setTimeout(function() {
// XHR to request a JS and a CSS
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.js');
xhr.send('');
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.css');
xhr.send('');
// preload image
new Image().src = "http://domain.tld/preload.png";
}, 1000);
};
图片懒加载就是一开始访问一个页面的时候并不会下载所有的图片,而是等到满足一定条件(条件应该根据具体的需求来定,通常应该是图片元素出现在可是区域中),才开始下载图片。
实现原理: 不给Img标签赋值src属性,浏览器就不会开始下载图片,利用html元素的data自定义属性来设置图片的下载地址,比如data-url,等到满足条件,就从data-url中取出图片的地址然后赋值给图片的src属性
<img src="" data-url="http://your.image.png" />
下载图片的函数
function loadImg(el){
let url = el.dataset.url;
el.src = url;
}
方法一(不推荐!这种方式是在是太麻烦了!各种算算算!!!!)
方法二
function isInViewPort(element) {
let windowHeight = window.innerHeight || document.documentElement.clientHeight;
let windowWidth = window.innerWidth || document.documentElement.clientWidth;
let rect = element.getBoundingClientRect();
if (rect.top > 0 && (rect.top + rect.height) <= windowHeight && rect.left > 0 && ((rect.left + rect.width) < windowWidth)) {
return true;
}
return false;
}
window.addEventListener('wheel', throttle(function() {
let imgs = document.getElementsByTagName('img');
imgs.forEach(img => {
if (isInViewPort(img) && !img.hasLoaded) {
loadImg(img);
img.onload = img.onerror = () => img.hasLoaded = true;
}
})
}, 500));
方法三
let imgs = document.getElementsByTagName('img');
var ob = new IntersectionObserver(function(entries) {
entries.forEach(entry=>{
let el = entry.target;
if(entry.intersectionRatio > 0) {
loadImg(el);
entry.onload = entry.onerror = ()=>ob.unobserve(el);
}
});
});
Array.from(imgs).forEach(img=>ob.observe(img))
事件流:指元素接收事件的顺序;
事件捕获: 不那么具体的元素(父元素)先接收到事件,具体的元素后接收到事件
事件冒泡:具体的元素(子元素)先接受事件,然后父元素再接受事件
事件代理利用了事件冒泡的机智,在父元素指定一个事件处理程序就可以管理一类事件
优点:
浏览器会预先加载声明在html中的资源,但是可以通过在js或者css的标签中添加preload让浏览器也预先加载css或者js资源,如下:
<link href=/wa/js/chunk-vendors.44474edb.js rel=preload as=script>
浏览器在后台空闲时获取将来可能用到的资源,并将他们存储在浏览器的缓存中
<link href=/wa/js/slot.546afd12.js rel=prefetch>
<link rel="dns-prefetch" href="//img.alicdn.com"></link>
浏览器在一个http请求正式发送给服务器前执行一些操作,包括dns解析,tcp握手等
<link href="https://cdn.domain.com" rel="preconnect" crossorigin>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.