1. 程式人生 > >rem適配的兩種方式(css媒體查詢和js控制)

rem適配的兩種方式(css媒體查詢和js控制)

css媒體查詢:(根字型大小100px,如果10px大小,寫成0.1rem)
html {
font-size : 100px;
}

@media only screen and (min-width: 320px) and (max-width: 479px){
html {
font-size: 42.67px !important;
}
}
@media only screen and (min-width: 480px) and (max-width: 639px){
html {
font-size: 64px !important;
}
}
@media only screen and (min-width: 640px) and (max-width: 749px){
html {
font-size: 85.34px !important;
}
}

@media only screen and (min-width: 750px) and (max-width: 959px){
html {
font-size: 100px !important;
}
}
@media only screen and (min-width: 960px) and (max-width: 1241px){
html {
font-size: 128px !important;
}
}

@media only screen and (min-width: 1242px){
html {
font-size: 165.6px !important;
}
}

js控制(zepto / jQuery)(根字型大小100px,如果10px大小,寫成0.1rem)
function setFont() {
let window_width = window.innerWidth;
let font_size = parseFloat(window_width / 3.75);
$(‘html’).css(‘font-size’, font_size);
}

$(window).on(‘resize’, function () {
setFont();
});