八個(gè)移動(dòng)端適配技巧,兼容性問(wèn)題減少90%
移動(dòng)端適配一直是前端開(kāi)發(fā)中的重點(diǎn)難題,分享下常見(jiàn)的移動(dòng)端兼容處理方案。
1. 使用viewport配置,確保完美視口
移動(dòng)端開(kāi)發(fā)首先要設(shè)置正確的viewport,這是適配的基礎(chǔ)。
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
關(guān)鍵屬性解析:
- width=device-width:將視口寬度設(shè)置為設(shè)備寬度
- initial-scale=1.0:初始縮放比例為1
- user-scalable=no:禁用用戶(hù)縮放
- viewport-fit=cover:適配劉海屏
2. 使用rem實(shí)現(xiàn)彈性布局
rem是相對(duì)于根元素(html)的字體大小的單位,可以實(shí)現(xiàn)整體布局的彈性縮放。
// 設(shè)置 rem 基準(zhǔn)值
(function flexible() {
const docEl = document.documentElement;
function setRemUnit() {
const rem = docEl.clientWidth / 10;
docEl.style.fontSize = rem + 'px';
}
setRemUnit();
window.addEventListener('resize', setRemUnit);
window.addEventListener('orientationchange', setRemUnit);
})();
配套的CSS使用:
.container {
width: 7.5rem; /* 750px / 100 */
height: 1rem; /* 100px / 100 */
font-size: 0.28rem; /* 28px / 100 */
}
3. CSS媒體查詢(xún)處理不同尺寸
使用媒體查詢(xún)針對(duì)不同屏幕尺寸定制樣式。
/* iPhone SE */
@media screen and (max-width: 374px) {
.container {
font-size: 14px;
}
}
/* iPhone 6/7/8/X */
@media screen and (min-width: 375px) and (max-width: 413px) {
.container {
font-size: 16px;
}
}
/* iPhone 6/7/8 Plus */
@media screen and (min-width: 414px) {
.container {
font-size: 18px;
}
}
4. 1px邊框問(wèn)題解決方案
在高清屏幕下1px邊框顯示過(guò)粗的解決方案。
.border-1px {
position: relative;
&::after {
content: '';
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 1px;
background-color: #000;
transform: scaleY(0.5);
transform-origin: bottom;
}
}
// 2x屏
@media (-webkit-min-device-pixel-ratio: 2) {
.border-1px::after {
transform: scaleY(0.5);
}
}
// 3x屏
@media (-webkit-min-device-pixel-ratio: 3) {
.border-1px::after {
transform: scaleY(0.33);
}
}
5. 安全區(qū)域適配
適配iPhone X等帶有劉海的機(jī)型。
/* 適配劉海屏 */
.safe-area-inset {
padding-top: constant(safe-area-inset-top);
padding-top: env(safe-area-inset-top);
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
/* 底部固定導(dǎo)航適配 */
.fixed-bottom {
position: fixed;
bottom: 0;
bottom: constant(safe-area-inset-bottom);
bottom: env(safe-area-inset-bottom);
}
6. 圖片適配方案
針對(duì)不同分辨率設(shè)備的圖片適配策略。
<!-- 使用srcset適配不同分辨率 -->
<img srcset="image-320w.jpg 320w,
image-480w.jpg 480w,
image-800w.jpg 800w"
sizes="(max-width: 320px) 280px,
(max-width: 480px) 440px,
800px"
src="image-800w.jpg" alt="Responsive image">
配合CSS的處理:
.responsive-image {
max-width: 100%;
height: auto;
display: block;
}
7. 橫屏適配處理
處理橫屏模式下的布局適配。
/* 檢測(cè)橫屏 */
@media screen and (orientation: landscape) {
.landscape-container {
display: flex;
flex-direction: row;
}
}
/* 檢測(cè)豎屏 */
@media screen and (orientation: portrait) {
.portrait-container {
display: flex;
flex-direction: column;
}
}
JavaScript監(jiān)聽(tīng)屏幕旋轉(zhuǎn):
window.addEventListener('orientationchange', function() {
if (window.orientation === 180 || window.orientation === 0) {
// 豎屏
console.log('豎屏');
}
if (window.orientation === 90 || window.orientation === -90) {
// 橫屏
console.log('橫屏');
}
});
8. 軟鍵盤(pán)彈出處理
處理軟鍵盤(pán)彈出時(shí)的頁(yè)面適配問(wèn)題。
// 監(jiān)聽(tīng)軟鍵盤(pán)
const originalHeight = document.documentElement.clientHeight;
window.addEventListener('resize', () => {
const currentHeight = document.documentElement.clientHeight;
const input = document.activeElement;
if (originalHeight > currentHeight) {
// 軟鍵盤(pán)彈出
if (input.tagName === 'INPUT' || input.tagName === 'TEXTAREA') {
input.scrollIntoView({ block: 'center' });
}
} else {
// 軟鍵盤(pán)收起
window.scrollTo(0, 0);
}
});
CSS處理:
/* 防止鍵盤(pán)頂起頁(yè)面 */
.container {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
移動(dòng)端適配是一個(gè)系統(tǒng)工程,需要在項(xiàng)目開(kāi)始時(shí)就建立完整的適配方案,而不是在遇到問(wèn)題時(shí)臨時(shí)處理。