首先是html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script src="./rem.js"></script>
<script src="./promise封装.js"> </script>
<style>
*{
margin: 0;
padding: 0;
}
</style>
<body>
<div style="width: 2rem; height: 2rem; background-color: #409EFF;">
<div style="width: 1rem; height: 1rem; background-color: #c00;">
<div style="font-size: 0.3rem;">页面</div>
</div>
</div>
</body>
<script>
window.onload = function(){
console.log(request);
request("https://elm.cangdu.org/v1/cities?type=guess").then((res)=>{
console.log(res);
})
}
</script>
</html>
其次是rem.js
window.onresize = function() {
console.log("尺寸发生改变");
getRem()
}
getRem()
function getRem(){
let html = document.querySelector("html")
let oWidth = document.documentElement.clientWidth
// 375页面的宽度
html.style.fontSize = oWidth/375*100 + "px"
}
最后是promise封装的ajax
promise 是用来解决异步操作的 含有三个属性 成功,失败 和进行中
function request (url,motheds="GET"){
return new Promise((resolve, reject) => {
//原生ajax
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest()
}else{
xhr = new ActiveXObject("Microsoft.XMLHTTP")
}
xhr.onreadystatechange = function(){
resolve('先')
if(xhr.readyState == 4 && xhr.status == 200){
let data = xhr.responseText
console.log(data);
}
}
xhr.open(motheds,url,true)
xhr.send(null)
})
}