移动端的屏幕适配问题
1、视口
未添加语句前:
添加语句后:
显示效果如下:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>
<style type="text/css">
.box{
width: 300px;
height: 300px;
border: 2px solid black;
background-color: pink;
}
</style>
</head>
<body>
<div class="box">div里面的文字</div>
</body>
</html>
2、retina屏幕的适配
未适配前:
显示效果:
进行retina屏幕适配:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>retina屏幕适配</title>
<style type="text/css">
/*关于通过样式将尺寸设置为原先的一半retina屏幕的适配*/
.shipei{
width: 300px;
height: 215px;
}
</style>
</head>
<body>
<img src="./images/pic1x.jpg" alt="pic1x">
<img src="./images/pic2x.jpg" alt="pic2x" class="shipei">
</body>
</html>
显示效果如下所示:
----------------------------华丽的分割线----------------------------------
3、适配布局类型
1、流体布局
方式1:calc()函数
未使用流体布局前:
显示效果如下
使用流体布局后:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>68_流体布局</title>
<style type="text/css">
body{
margin: 0;
}
.con a{
float: left;
display: block;
/*width: 25%; !*使用流体布局*!*/
width: calc(25% - 4px); /*使用流体布局*/
height: 100px;
background-color: gold;
border: 2px solid black;
text-decoration: none;
text-align: center;
line-height: 100px;
color: black;
font-size: 14px;
}
</style>
</head>
<body>
<div class="con">
<a href="#">菜单文字</a>
<a href="#">菜单文字</a>
<a href="#">菜单文字</a>
<a href="#">菜单文字</a>
</div>
</body>
</html>
显示效果如下:
方式2:使用box-sizing属性
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>68_流体布局</title>
<style type="text/css">
body{
margin: 0;
}
.con a{
float: left;
display: block;
width: 25%; /*使用流体布局*/
/*width: calc(25% - 4px); !*使用流体布局:方式一*!*/
height: 100px;
background-color: gold;
border: 2px solid black;
text-decoration: none;
text-align: center;
line-height: 100px;
color: black;
font-size: 14px;
box-sizing: border-box; /*方式二:设置此属性后,宽度就会从边框计算,加边框、加尺寸宽度是不变的*/
}
</style>
</head>
<body>
<div class="con">
<a href="#">菜单文字</a>
<a href="#">菜单文字</a>
<a href="#">菜单文字</a>
<a href="#">菜单文字</a>
</div>
</body>
</html>
显示效果如下:
2、响应式布局
代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>69_响应式布局</title>
<style type="text/css">
body{
margin: 0;
}
.con div{
/*每个盒子刚好占25%*/
width: 23%;
margin: 1%;
border: 2px solid black;
background-color: gold;
height: 200px;
float: left;
box-sizing: border-box;/*流体布局方式,利用此属性将边框算入到23%内*/
}
/*即屏幕缩小到800时,*/
@media (max-width: 800px) {
.con div{
width: 46%;
margin: 2%;
}
}
@media (max-width: 400px) {
.con div {
width: 94%;
margin: 3%;
}
}
</style>
</head>
<body>
<div class="con">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
</body>
</html>
效果如下所示:
3、基于rem布局
情形一:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>rem布局原理</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.box01{
font-size: 15px;
width: 20em;
height: 10em;
background-color: gold;
}
.box02{
font-size: 20px;
width: 20em;
height: 10em;
background-color: green;
}
p{
font-size: 20px;
text-indent: 2em;
}
</style>
</head>
<body>
<div class="box01"></div>
<br>
<br>
<div class="box02"></div>
<p>尤其是白伟拓还和张多磐打赌了,谁在新生晚会的节目上得分最高,拿了好名次,就可以去追求何美月,谁得分低,那就自动出局</p>
</body>
</html>
情形二:
<!DOCTYPE html>
<html lang="en" style="font-size: 20px">
<head>
<meta charset="UTF-8">
<title>rem布局原理</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.box01{
font-size: 15px;
width: 20rem;
height: 10rem;
background-color: gold;
}
.box02{
font-size: 20px;
width: 20rem;
height: 10rem;
background-color: green;
}
p{
font-size: 20px;
text-indent: 2rem;
}
</style>
</head>
<body>
<div class="box01"></div>
<br>
<br>
<div class="box02"></div>
<p>尤其是白伟拓还和张多磐打赌了,谁在新生晚会的节目上得分最高,拿了好名次,就可以去追求何美月,谁得分低,那就自动出局</p>
</body>
</html>
显示效果如下所示:
利用rem实现等比缩放
html代码:
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入js文件,实现等比缩放-->
<script type="text/javascript" src="js/set_root.js"></script>
<title>rem布局——头部文字效果</title>
<style type="text/css">
body{
margin: 0;
}
.header{
height: 2.5rem;
background-color: gold;
text-align: center;
line-height: 2.5rem;
font-size: 20px;
}
</style>
</head>
<body>
<div class="header">头部文字效果</div>
</body>
</html>
js:代码
(function(){
var calc = function(){
var docElement = document.documentElement;
var clientWidthValue = docElement.clientWidth > 750 ? 750 : docElement.clientWidth;
docElement.style.fontSize = 20*(clientWidthValue/375) + 'px';
}
calc();
window.addEventListener('resize',calc);
})();
显示效果如下所示: