H5标签-浏览器兼容
对于h5标签的浏览器兼容行,我们可以查看一个网站can i use,例如搜索header,可以看到他所支持的浏览器兼容。
或者,下载html5shiv,在代码中引入js文件。
JS-浏览器兼容
做特性检测,不要做浏览器检测
加入各个浏览器的前缀window.requestAnimationFrame
必须放第一位
var requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (fn) {
setTimeout(fn, 16);
};
requestAnimationFrame(function () {
console.log(1);
});
CSS-浏览器兼容
加上浏览器前缀,或者下载Modernizr
click300ms延迟
造成的原因:由于double click双击放大造成
解决方法:
- 在meta标签中加入width=device-width
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
- 引入
<script src="js/fastclick.js"></script>
,下载地址fastclick
<script>
if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function() {
FastClick.attach(document.body);
}, false);
}
</script>
单行和多行文字溢出省略
单行文字溢出省略
若没有使用flex布局,可以将属性加在一起。直接使用
.text-ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
若使用了flex布局,将flex布局里面嵌套.text-ellipsis
.recommend-name { display: flex; justify-content: center; align-items: center; }
.text-ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
<p class="recommend-name">
<span class="text-ellipsis">欧派整体橱柜定制简约现代 欧派整体橱柜定制简约现代欧派整体橱柜定制简约现代欧派整体橱柜定制简约现代</span>
</p>
多行文字溢出省略
.multiline-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
white-space: normal !important;
word-wrap: break-word;
}
水平居中和垂直居中
水平居中
text-align:center;有固定宽度
margin-left:auto;margin-right:auto;只争对block块级元素,有固定宽高
position+margin-left/translate
flex
垂直居中
line-height:有固定高度,只适合单行文本,不适合多行
position+margin-top/translate
flex
优先选择flex。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>2.5 水平居中和垂直居中</title>
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.modal-wrapper {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1000;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
/*display: flex;
justify-content: center;
align-items: center;*/
}
.modal {
overflow: hidden;
background-color: #fff;
border-radius: 10px;
font-size: 16px;
/*1 容器宽高自适应 没有指定宽高 内容撑开*/
/*1-1 内联元素 不能设置宽高 内容撑开*/
/*1-1-1 文字水平垂直居中 多行文字*/
/*display: inline;*/
/*padding: 30px 20px;*/
/*padding: 0 20px;*/
/*1-1-2 容器水平垂直居中 内联元素*/
/*position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);*/
/*1-2 内联块元素 不能设置宽高 内容撑开*/
/*1-2-1 文字水平垂直居中 多行文字*/
/*display: inline-block;
padding: 30px 20px;*/
/*1-2-2 容器水平垂直居中 内联块元素*/
/*position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);*/
/*1-3 块元素 不能设置宽高*/
/*1-3-1 文字水平垂直居中 多行文字*/
/*display: block;
text-align: center;
padding: 30px 0;*/
/*1-3-2 容器水平垂直居中 块元素*/
/*position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);*/
/*2 指定容器宽高宽高*/
/*2-1 内联元素 不能设置宽高 内容撑开*/
/*2-2 内联块元素 设置宽高*/
/*2-2-1 文字水平垂直居中 多行文字*/
/*display: inline-block;
width: 300px;
height: 100px;
text-align: center;
line-height: 100px;*/
/*多行文字*/
/*display: flex;
justify-content: center;
align-items: center;*/
/*2-2-2 容器水平垂直居中 内联块元素*/
/*position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);*/
/*margin-left: -150px;
margin-top: -50px;*/
/*2-3 块元素 设置宽高*/
/*2-3-1 文字水平垂直居中 多行文字*/
display: block;
width: 300px;
height: 100px;
text-align: center;
line-height: 100px;
/*2-3-2 容器水平垂直居中 块元素*/
/*margin: 0 auto;*/
position: absolute;
left: 50%;
top: 50%;
/*transform: translate(-50%, -50%);*/
margin-left: -150px;
margin-top: -50px;
}
</style>
</head>
<body>
<div class="modal-wrapper">
<span class="modal">单行文字水平垂直居中</span>
<!-- <span class="modal">多行文字水平垂直居中 多行文字水平垂直居中多行文字水平垂直居中</span> -->
</div>
</body>
</html>